{"id":859,"date":"2013-02-11T07:54:29","date_gmt":"2013-02-11T07:54:29","guid":{"rendered":"http:\/\/www.osd.net\/blog\/?p=859"},"modified":"2013-02-11T09:45:23","modified_gmt":"2013-02-11T09:45:23","slug":"dynamic-jquery-slider-search","status":"publish","type":"post","link":"https:\/\/www.osd.net\/blog\/web-development\/php\/dynamic-jquery-slider-search\/","title":{"rendered":"Dynamic jQuery Slider search"},"content":{"rendered":"<p>In this article, I will show you how to make a search within intervals using <strong>AJAX <\/strong>and <a title=\"jQuery Slider\" href=\"http:\/\/jqueryui.com\/slider\/\" target=\"_blank\">jQuery Slider<\/a>. I will use for example purpose a search between a minimum and a maximum price of some estates that will look like the one you can see in these images:<\/p>\n<p class=\"aligncenter\"><a href=\"https:\/\/www.osd.net\/blog\/wp-content\/uploads\/2013\/02\/blog1.jpg\"><img loading=\"lazy\" class=\"size-medium wp-image-906 aligncenter\" title=\"jQuery Slider Search Example\" alt=\"jQuery Slider Search Example\" src=\"https:\/\/www.osd.net\/blog\/wp-content\/uploads\/2013\/02\/blog1-300x112.jpg\" width=\"300\" height=\"112\" srcset=\"https:\/\/www.osd.net\/blog\/wp-content\/uploads\/2013\/02\/blog1-300x112.jpg 300w, https:\/\/www.osd.net\/blog\/wp-content\/uploads\/2013\/02\/blog1.jpg 391w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/a><br \/>\n<a href=\"https:\/\/www.osd.net\/blog\/wp-content\/uploads\/2013\/02\/blog2.jpg\"><img loading=\"lazy\" class=\"size-medium wp-image-905 aligncenter\" title=\"jQuery Slider Search Example\" alt=\"jQuery Slider Search Example\" src=\"https:\/\/www.osd.net\/blog\/wp-content\/uploads\/2013\/02\/blog2-300x112.jpg\" width=\"300\" height=\"112\" srcset=\"https:\/\/www.osd.net\/blog\/wp-content\/uploads\/2013\/02\/blog2-300x112.jpg 300w, https:\/\/www.osd.net\/blog\/wp-content\/uploads\/2013\/02\/blog2.jpg 391w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<p>We will use a <strong>HTML <\/strong>file, a <strong>Javascript <\/strong>file and a <strong>PHP <\/strong>file requested with <strong>AJAX<\/strong>, just as in the previous article, \u00a0&#8220;<a href=\"https:\/\/www.osd.net\/blog\/web-development\/javascript\/dynamic-jquery-datepicker-calendar\/\" target=\"_blank\">Dynamic jQuery Datepicker calendar<\/a>&#8221;<\/p>\n<p>1. The <strong>html<\/strong> file<br \/>\nIn this file you have to add the link to the jQuery libraries and the stylesheets.<\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\n&lt;!-- stylesheets --&gt;\r\n&lt;link href=&quot;http:\/\/code.jquery.com\/ui\/1.9.1\/themes\/base\/jquery-ui.css&quot; rel=&quot;stylesheet&quot; \/&gt;\r\n\r\n&lt;!-- js files --&gt;\r\n&lt;script type=&quot;text\/javascript&quot; src=&quot;http:\/\/code.jquery.com\/jquery-1.8.2.js&quot;&gt;&lt;\/script&gt;&lt;script type=&quot;text\/javascript&quot; src=&quot;http:\/\/code.jquery.com\/ui\/1.9.1\/jquery-ui.js&quot;&gt;&lt;\/script&gt;\r\n\r\n&lt;script type=&quot;text\/javascript&quot; src=&quot;script.js&quot;&gt;&lt;\/script&gt;\r\n<\/pre>\n<p>With the libraries and the stylesheet included, we&#8217;ll need a div tag for the slider (&#8220;slider_price&#8221;), 2 span tags for minimum price (&#8220;app_min_price&#8221;) and maximum price (&#8220;app_max_price&#8221;) and a span tag for the total number of estates found within the selected interval of prices (&#8220;number_results&#8221;):<\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\n&lt;span id=&quot;app_min_price&quot;&gt;&lt;\/span&gt;\r\n&lt;span id=&quot;app_max_price&quot;&gt;&lt;\/span&gt;&lt;\/pre&gt;\r\n&lt;div id=&quot;slider_price&quot;&gt;&lt;\/div&gt;\r\n&lt;pre&gt;&lt;span id=&quot;number_results&quot;&gt;&lt;\/span&gt; results found.\r\n<\/pre>\n<p>2. The <strong>javascript<\/strong> file<br \/>\nYou can also put this code in the html file between the &#8220;&lt;script&gt;&#8221; tags. This <strong>script<\/strong> will have 2 parts:<\/p>\n<ul>\n<li>First of all, we have to initialize the slider. The explanation for each of the options can be found at in the <a href=\"http:\/\/api.jqueryui.com\/slider\/\" target=\"_blank\">API documentation<\/a> of the plugin.\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\n$(document).ready(function()\r\n{\r\n\t$( &quot;#slider_price&quot; ).slider({\r\n\t\t\t\trange: true,\r\n\t\t\t\tmin: 0,\r\n\t\t\t\tmax: 2500000,\r\n\t\t\t\tstep:5000,\r\n\t\t\t\tvalues: [ 0, 2500000 ],\r\n\t\t\t\tslide: function( event, ui ) {\r\n\t\t\t\t\t$( &quot;#app_min_price&quot; ).text(ui.values[0] + &quot;$&quot;);\r\n\t\t\t\t\t$( &quot;#app_max_price&quot; ).text(ui.values[1] + &quot;$&quot;);\r\n\t\t\t\t},\r\n\t\t\t\tstop: function( event, ui ) {\r\n\t\t\t\t\tvar nr_total = getEstatesNumber(ui.values[0], ui.values[1]);\r\n\t\t\t\t\t$(&quot;#number_results&quot;).text(nr_total);\r\n\t\t\t\t},\r\n\t});\r\n\t$(&quot;#app_min_price&quot;).text( $(&quot;#slider_price&quot;).slider(&quot;values&quot;, 0) + &quot;$&quot;);\r\n\t$(&quot;#app_max_price&quot;).text( $(&quot;#slider_price&quot;).slider(&quot;values&quot;, 1) + &quot;$&quot;);\r\n});\r\n<\/pre>\n<p>I&#8217;ve set a minimum and maximum and also added the same values for the initial state (see lines 5-6 and 8 in the javascript file). To make things work better and smoother the step is 5000 and<br \/>\nyou can refine your search experience by setting it as needed depending on your lowest and highest value.<\/p>\n<p>Next, in the javascript file you can see the way two important events, slide and stop, are handled:<br \/>\n<strong>Slide<\/strong>: This event is triggered while the user drags one of the sliders and we will use it to update the values in the two span elements that show the minimum and maximum.<br \/>\n<strong>Stop<\/strong>: When this event is fired when the visitor releases the slider and it is suitable for making the AJAX call that will count the database records (&#8220;Estates&#8221; in our example).<\/p>\n<p>The new values of minimum and maximum will be put in the span tags with id &#8220;app_min_price&#8221; and &#8220;app_max_price&#8221; from the html file.\n<\/li>\n<li>The second part of the javascript file is the function that makes the AJAX call to get the new number of estates within that interval of prices.\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\nfunction getEstatesNumber(min_price, max_price)\r\n{\r\n\tvar number_of_estates = 0;\r\n    $.ajax(\r\n    {\r\n        type: &quot;POST&quot;,\r\n\t\turl: 'estates.php',\r\n        dataType: 'json',\r\n\t\tdata: {'minprice': min_price, 'maxprice':max_price},\r\n        async: false,\r\n        success: function(data)\r\n        {\r\n\t\t\tnumber_of_estates = data;\r\n        }\r\n    });\r\n    return number_of_estates;\r\n}\r\n<\/pre>\n<p><strong>Obs.:<\/strong> Even if I referred to the server call as an AJAX it was set up to be synchronous because I wanted to be sure that the responses come in the some order as the requests sent.\n<\/li>\n<\/ul>\n<p>3. In the AJAX call from the <strong>javascript<\/strong> file, we have a <strong>PHP <\/strong> document requested. For the purpose of this example I chose to count the elements saved in a database table defined as:<\/p>\n<pre class=\"brush: sql; title: ; notranslate\" title=\"\">\r\nCREATE TABLE IF NOT EXISTS `Estates` (\r\n  `estate_id` int(11) NOT NULL auto_increment,\r\n  `estate_price` int(5) NOT NULL,\r\n  PRIMARY KEY  (`estate_id`)\r\n)\r\n<\/pre>\n<p>Now in the <em>estates.php<\/em> file, you&#8217;ll need to count the estates with price within the given interval (the parameters sent with the AJAX call). To accomplish this you can use something similar to:<\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\n$result = 0;\r\n\/\/ take the estates from the table named &quot;Estates&quot;\r\nif(isset($_POST['minprice']) &amp;&amp; isset($_POST['maxprice']))\r\n{\r\n\t$minprice\t= filter_var($_POST['minprice']\t, FILTER_VALIDATE_INT);\r\n\t$maxprice\t= filter_var($_POST['maxprice']\t, FILTER_VALIDATE_INT);\r\n\t$query = '\r\n\t\t\t\t\t\t\tSELECT\r\n\t\t\t\t\t\t\t\tcount(*) as nr_estates\r\n\t\t\t\t\t\t\tFROM\r\n\t\t\t\t\t\t\t\tEstates\r\n\t\t\t\t\t\t\tWHERE\r\n\t\t\t\t\t\t\t\testate_price\r\n\t\t\t\t\t\t\tBETWEEN\r\n\t\t\t\t\t\t\t\t:minprice\r\n\t\t\t\t\t\t\tAND\r\n\t\t\t\t\t\t\t\t:maxprice\r\n\t\t\t\t\t\t';\r\n\t$stmt = $dbh-&gt;prepare($query);\r\n\ttry\r\n\t{\r\n\t\t$stmt-&gt;bindParam(':minprice', $minprice);\r\n\t\t$stmt-&gt;bindParam(':maxprice', $maxprice);\r\n\t\t$stmt-&gt;execute();\r\n\t}\r\n\tcatch (PDOException $e)\r\n\t{\r\n\t\tprint($e-&gt;getMessage());\r\n\t\tdie;\r\n\t}\r\n\t$row = $stmt-&gt;fetch(PDO::FETCH_ASSOC);\r\n\t$result = $row['nr_estates'];\r\n}\r\necho json_encode($result);\r\n<\/pre>\n<p>You can download an archive with the code <a href=\"https:\/\/www.osd.net\/blog\/wp-content\/uploads\/2013\/02\/slider.zip\">here<\/a><br \/>\nIf you have questions or suggestions please, don\u2019t hesitate to tell me.<\/p>\n<div id=\"share-and-beer-container\">\t<div id=\"buy_me_a_beer_div\" class=\"single beer\">\n\t \t\n\t\t<div class=\"buy-beer\" onclick=\"document.getElementById('buy_me_a_beer_form').submit();\">\n\t\t\t<form action=\"https:\/\/www.paypal.com\/cgi-bin\/webscr\" id=\"buy_me_a_beer_form\" method=\"post\">\n\t\t\t\t<input type=\"hidden\" name=\"cmd\" value=\"_xclick\" \/>\n\t\t\t\t<input type=\"hidden\" name=\"business\" value=\"info@directaccess.ro\" \/>  \n\t\t\t\t<input type=\"hidden\" name=\"item_name\" value=\"A Beer For Dynamic jQuery Slider search\" \/>  \n\t\t\t\t<input type=\"hidden\" name=\"item_number\" value=\"1\" \/>  \n\t\t\t\t<input type=\"hidden\" name=\"return\" value=\"https:\/\/www.osd.net\/blog\/web-development\/php\/dynamic-jquery-slider-search\/\" \/>  \n\t\t\t\t<input type=\"hidden\" name=\"amount\" value=\"5\" \/>  \n\t\t\t\t<input type=\"hidden\" name=\"undefined_quantity\" value=\"1\" \/>  \n\t\t\t\t<input type=\"hidden\" name=\"currency_code\" value=\"USD\" \/>  \n\t\t\t<\/form>\n\t\t\t<p class=\"buy-beer-text\">If you liked this post <br \/> you can <strong>buy me a beer<\/strong><\/p>\n\t\t<\/div>\n\t<\/div><\/div>","protected":false},"excerpt":{"rendered":"<p>In this article, I will show you how to make a search within intervals using AJAX and jQuery Slider. I will use for example purpose a search between a minimum and a maximum price of some estates that will look like the one you can see in these images: We will use a HTML file, &hellip;<\/p>\n<div class=\"cta1\"><a href=\"https:\/\/www.osd.net\/blog\/web-development\/php\/dynamic-jquery-slider-search\/\">Read more<\/a><\/div>\n<div class=\"like-excerpt\"><div\n        class=\"fb-like\"\n        data-href=\"https:\/\/www.osd.net\/blog\/web-development\/php\/dynamic-jquery-slider-search\/\"\n        data-layout=\"button_count\"\n        data-action=\"like\"\n        data-show-faces=\"false\"\n        data-share=\"false\">\n        <\/div><\/div>","protected":false},"author":5,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[19,18,14],"tags":[57,55,86,35,116],"_links":{"self":[{"href":"https:\/\/www.osd.net\/blog\/wp-json\/wp\/v2\/posts\/859"}],"collection":[{"href":"https:\/\/www.osd.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.osd.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.osd.net\/blog\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/www.osd.net\/blog\/wp-json\/wp\/v2\/comments?post=859"}],"version-history":[{"count":64,"href":"https:\/\/www.osd.net\/blog\/wp-json\/wp\/v2\/posts\/859\/revisions"}],"predecessor-version":[{"id":964,"href":"https:\/\/www.osd.net\/blog\/wp-json\/wp\/v2\/posts\/859\/revisions\/964"}],"wp:attachment":[{"href":"https:\/\/www.osd.net\/blog\/wp-json\/wp\/v2\/media?parent=859"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.osd.net\/blog\/wp-json\/wp\/v2\/categories?post=859"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.osd.net\/blog\/wp-json\/wp\/v2\/tags?post=859"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}