<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Steve Reynolds Blog &#187; Live Search</title>
	<atom:link href="http://www.reynoldsftw.com/tag/live-search/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.reynoldsftw.com</link>
	<description>Being Generalist.</description>
	<lastBuildDate>Fri, 03 Sep 2010 19:00:35 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Live MySQL Database Search with jQuery</title>
		<link>http://www.reynoldsftw.com/2009/03/live-mysql-database-search-with-jquery/</link>
		<comments>http://www.reynoldsftw.com/2009/03/live-mysql-database-search-with-jquery/#comments</comments>
		<pubDate>Thu, 19 Mar 2009 13:59:21 +0000</pubDate>
		<dc:creator>Steve Reynolds</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Technology]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[jQuery UI]]></category>
		<category><![CDATA[Live Search]]></category>
		<category><![CDATA[MySQL]]></category>
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://www.reynoldsftw.com/?p=1055</guid>
		<description><![CDATA[Live search is starting to become more and more mainstream. With the advent of Ajax the ability to carry out searches without leaving the page have obviously started to appear more and more. The trouble with live searching though, is it can be quite resource intensive. Every keystroke in theory returns a bunch of search [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.reynoldsftw.com%2F2009%2F03%2Flive-mysql-database-search-with-jquery%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.reynoldsftw.com%2F2009%2F03%2Flive-mysql-database-search-with-jquery%2F&amp;source=SteveReynolds&amp;style=normal&amp;service=bit.ly" height="61" width="50" /><br />
			</a>
		</div>
<p>Live search is starting to become more and more mainstream. With the advent of Ajax the ability to carry out searches without leaving the page have obviously started to appear more and more. The trouble with live searching though, is it can be quite resource intensive. Every keystroke in theory returns a bunch of search results, perhaps relevant, perhaps not. On a busy site this could have quite an impact.</p>
<p>In this tutorial I will demonstrate the same basic theory, live searching <strong>(<a href="http://demos.reynoldsftw.com/live-search/" target="_blank">demo</a>)</strong> &#8211; however I am <span style="text-decoration: underline;">not</span> showing the user any results until they execute the search. Instead, I am responding with <strong>a search result count</strong> in a live manner (after every key stroke), but actually returning the results on a button click or [ENTER] key press.</p>
<div id="attachment_1058" class="wp-caption aligncenter" style="width: 490px"><a href="http://demos.reynoldsftw.com/live-search/"><img class="size-full wp-image-1058" title="picture-2" src="http://www.reynoldsftw.com/wp-content/uploads/2009/03/picture-2.png" alt="Live MySQL searching with jQuery" width="480" height="87" /></a><p class="wp-caption-text">Live MySQL searching with jQuery (Click for demo)</p></div>
<p>What this does is cut down on the amount of data whizzing around after every keystroke, cutting down database activity and more importantly bandwidth reduction. It also gives the user some relevant feedback on their search so they can see if their search term actually matches any results in the first place before submitting it in a very economical fashion.</p>
<h2 style="text-align: center;"><a href="http://demos.reynoldsftw.com/live-search/" target="_blank"><strong>Click Here For Demo</strong></a></h2>
<h3><span id="more-1055"></span>The HTML</h3>
<p>First up, lets create the basic HTML page. A couple of containers for the form elements and a container for the results.</p>
<pre class="brush: xml;">&lt;div id=&quot;form&quot;&gt;

&lt;input type=&quot;text&quot; id=&quot;searchbox&quot; name=&quot;searchbox&quot; /&gt;

&lt;div id=&quot;buttonContainer&quot;&gt;

&lt;a class=&quot;button&quot; id=&quot;submitbutton&quot; href=&quot;#&quot;&gt;&lt;span id=&quot;buttontext&quot;&gt;Search&lt;/span&gt;&lt;/a&gt;

&lt;/div&gt;

&lt;/div&gt;

&lt;div id=&quot;resultsContainer&quot;&gt;&lt;/div&gt;
</pre>
<h3>The PHP</h3>
<p>The PHP part of this is very simplistic in its approach, it is returning HTML data rather than XML or JSON objects &#8211; it&#8217;s up to you to take that next step. The script has two roles, 1 &#8211; To return a result count number, and 2 &#8211; to return search results. You could seperate these into two scripts if you want, but I&#8217;ve put them into one for now&#8230;</p>
<pre class="brush: php;">if(isset($_GET['query'])) { $query = $_GET['query']; } else { $query = &quot;&quot;; }

if(isset($_GET['type'])) { $type = $_GET['type']; } else { $query = &quot;count&quot;; }

if($type == &quot;count&quot;)

{

$sql = mysql_query(&quot;SELECT count(url_id)

FROM urls

WHERE MATCH(url_url, url_title, url_desc)

AGAINST('$query' IN BOOLEAN MODE)&quot;);

$total = mysql_fetch_array($sql);

$num = $total[0];

echo $num;

}

if($type == &quot;results&quot;)

{

$sql = mysql_query(&quot;SELECT url_url, url_title, url_desc

FROM urls

WHERE MATCH(url_url, url_title, url_desc)

AGAINST('$query' IN BOOLEAN MODE)&quot;);

while($array = mysql_fetch_array($sql)) {

$url_url = $array['url_url'];

$url_title = $array['url_title'];

$url_desc = $array['url_desc'];

echo &quot;&lt;div class=\&quot;url-holder\&quot;&gt;&lt;a href=\&quot;&quot; . $url_url . &quot;\&quot; class=\&quot;url-title\&quot; target=\&quot;_blank\&quot;&gt;&quot; . $url_title . &quot;&lt;/a&gt;

&lt;div class=\&quot;url-desc\&quot;&gt;&quot; . $url_desc . &quot;&lt;/div&gt;&lt;/div&gt;&quot;;

}

}</pre>
<p><center><script type="text/javascript"><!--
google_ad_client = "pub-6415056921376217";
google_ad_channel = "in-ad-unit-hor";
google_ui_features = "rc:0";
google_ad_width = 468;
google_ad_height = 60;
google_ad_format = "468x60_as";
google_ad_type = "text_image";
google_color_border = "FFFFFF";
google_color_bg = "FFFFFF";
google_color_link = "0066CC";
google_color_text = "";
google_color_url = "0066CC";

//--></script>
<script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
</center></p>
<p>You&#8217;ll need to ignore the table and column names in the MySQL query, however you get the idea. For the &#8220;count&#8221; section the query is simply returning a result count to the browser, e.g. &#8220;3&#8243;. For the &#8220;results&#8221; section the script is returning chunks of HTML that our jQuery will consume and display on the page.</p>
<p>You also need to consider tightening up security on the form input, so validate what is coming in to ensure there is no SQL injections going on etc &#8211; I&#8217;ve really tried to just give you the basics here.</p>
<p><strong>The jQuery</strong></p>
<p>Finally, the jQuery. I am also using a snipsy bit of jQuery UI which you will need to include (but you can get all that from the download link at the end of this article).</p>
<pre class="brush: jscript;">$(document).ready(function() {

$(&quot;#searchbox&quot;).keyup(function(){

$.get(&quot;search.php&quot;,{query: $(&quot;#searchbox&quot;).val(), type: &quot;count&quot;}, function(data){

$(&quot;#buttontext&quot;).html(data + &quot; Results Available&quot;);

});
});

$(&quot;#searchbox&quot;).keyup(function(event){

if(event.keyCode == &quot;13&quot;)
{
getResults();
}

});

$(&quot;#submitbutton&quot;).click(function(){

getResults();

});

function getResults()
{

$.get(&quot;search.php&quot;,{query: $(&quot;#searchbox&quot;).val(), type: &quot;results&quot;}, function(data){

$(&quot;#resultsContainer&quot;).html(data);
$(&quot;#resultsContainer&quot;).show(&quot;blind&quot;);
});
}

});</pre>
<p>So on the keyup event on the search box, my live search count is being fired and returning this count number into my form button. Depending on the performance of your hardware this will be instant, or a bit laggy. Once you are satisfied with your search term and you know that there will be results, you can hit the &#8220;Enter&#8221; key or the button to display the results. At that point, the PHP script is called again and returns the HTML chunks as described before.</p>
<h3>Download</h3>
<div id="attachment_86" class="wp-caption alignleft" style="width: 70px"><a href="http://www.reynoldsftw.com/wp-content/uploads/2009/03/live-jquery-search.zip"><img class="size-full wp-image-86" title="Download Icon" src="http://www.reynoldsftw.com/wp-content/uploads/2009/01/drop-box-icon.png" alt="Download" width="60" height="60" /></a><p class="wp-caption-text">Download</p></div>
<p>As usual, click the box icon on the left to download the code in full including CSS and supporting image files. Hope you enjoy it, you&#8217;ll need to change the MySQL database connection variables, as well as the queries against the relevant tables etc&#8230;<img class="alignnone size-full wp-image-937" title="empty" src="http://www.reynoldsftw.com/wp-content/uploads/2009/03/empty.png" alt="empty" width="75" height="54" /></p>
<h3>Tools to help you learn&#8230;</h3>
<table border="0" cellspacing="0" cellpadding="0" width="100%">
<thead></thead>
<tbody>
<tr>
<td width="33%" align="center" valign="top">
<div id="attachment_613" class="wp-caption aligncenter" style="width: 111px"><a href="http://www.amazon.com/gp/product/1847196705?ie=UTF8&amp;tag=stereyblo-20&amp;linkCode=as2&amp;camp=1789&amp;creative=9325&amp;creativeASIN=1847196705"><img class="size-full wp-image-613" title="jquery13" src="http://www.reynoldsftw.com/wp-content/uploads/2009/02/jquery13.jpg" alt="Learning jQuery 1.3" width="101" height="132" /></a><p class="wp-caption-text">Learning jQuery 1.3</p></div>
<p><img style="border:none !important; margin:0px !important;" src="http://www.assoc-amazon.com/e/ir?t=stereyblo-20&amp;l=as2&amp;o=1&amp;a=1847196705" border="0" alt="" width="1" height="1" /></td>
<td width="33%" align="center" valign="top">
<div id="attachment_966" class="wp-caption aligncenter" style="width: 111px"><a href="http://www.amazon.com/gp/product/0672329166?ie=UTF8&amp;tag=stereyblo-20&amp;linkCode=as2&amp;camp=1789&amp;creative=9325&amp;creativeASIN=0672329166"><img class="size-full wp-image-966" title="phpmysql" src="http://www.reynoldsftw.com/wp-content/uploads/2009/03/phpmysql.jpeg" alt="PHP MySQL Development" width="101" height="132" /></a><p class="wp-caption-text">PHP MySQL Development</p></div>
<p><img style="border:none !important; margin:0px !important;" src="http://www.assoc-amazon.com/e/ir?t=stereyblo-20&amp;l=as2&amp;o=1&amp;a=0672329166" border="0" alt="" width="1" height="1" /></td>
<td width="33%" align="center" valign="top">
<div id="attachment_612" class="wp-caption aligncenter" style="width: 111px"><a href="http://www.amazon.com/gp/product/1847195121?ie=UTF8&amp;tag=stereyblo-20&amp;linkCode=as2&amp;camp=1789&amp;creative=9325&amp;creativeASIN=1847195121"><img class="size-full wp-image-612" title="jquery-ui" src="http://www.reynoldsftw.com/wp-content/uploads/2009/02/jquery-ui.jpg" alt="jQuery UI 1.6" width="101" height="132" /></a><p class="wp-caption-text">jQuery UI 1.6</p></div>
<p><img style="border:none !important; margin:0px !important;" src="http://www.assoc-amazon.com/e/ir?t=stereyblo-20&amp;l=as2&amp;o=1&amp;a=1847195121" border="0" alt="" width="1" height="1" /></td>
</tr>
</tbody>
</table>
<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.reynoldsftw.com%2F2009%2F03%2Flive-mysql-database-search-with-jquery%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.reynoldsftw.com%2F2009%2F03%2Flive-mysql-database-search-with-jquery%2F&amp;source=SteveReynolds&amp;style=normal&amp;service=bit.ly" height="61" width="50" /><br />
			</a>
		</div>
]]></content:encoded>
			<wfw:commentRss>http://www.reynoldsftw.com/2009/03/live-mysql-database-search-with-jquery/feed/</wfw:commentRss>
		<slash:comments>35</slash:comments>
		</item>
	</channel>
</rss>
