<?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>html tutorials &#124; css tutorials &#124; php tutorials &#124; Nodstrum Blog &#187; SQL</title>
	<atom:link href="http://www.nodstrum.com/category/sql/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.nodstrum.com</link>
	<description>A coders playground.</description>
	<lastBuildDate>Wed, 23 Feb 2011 13:52:37 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1</generator>
		<item>
		<title>AutoCompleter Tutorial &#8211; jQuery(Ajax)/PHP/MySQL</title>
		<link>http://www.nodstrum.com/2007/09/19/autocompleter/</link>
		<comments>http://www.nodstrum.com/2007/09/19/autocompleter/#comments</comments>
		<pubDate>Wed, 19 Sep 2007 11:50:40 +0000</pubDate>
		<dc:creator>Jamie</dc:creator>
				<category><![CDATA[CSS]]></category>
		<category><![CDATA[Dev]]></category>
		<category><![CDATA[General]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[SQL]]></category>

		<guid isPermaLink="false">http://nodstrum.com/2007/09/19/autocompleter/</guid>
		<description><![CDATA[AutoCompleter Tutorial As always, links to a demo and ZIP at the bottom, Enjoy! I thought i would write this tutorial because most of the auto completer applications i have seen just dump the code into a zip and tell you how to use it rather than how and why it works, knowing about this [...]]]></description>
			<content:encoded><![CDATA[<p><strong>AutoCompleter Tutorial</strong></p>
<p><em><strong>As always, links to a demo and ZIP at the bottom, Enjoy!</strong></em></p>
<p>I thought i would write this tutorial because most of the auto completer applications i have seen just dump the code into a zip and tell you how to use it rather than how and why it works, knowing about this enables you to customise it a lot more (this has been demonstrated with the other apps i have written here)!</p>
<p><img src="http://res.nodstrum.com/autoComplete/screenshots/AutoComplete_1.png" alt="" width="250" height="148" /><br />
<span id="more-44"></span><br />
And so we begin:<br />
<strong>JavaScript</strong></p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">&lt;script src=&quot;jquery-1.2.1.pack.js&quot; type=&quot;text/javascript&quot;&gt;&lt;!--mce:0--&gt;&lt;/script&gt;<span style="color: #339933;">&lt;</span>script type<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;text/javascript&quot;</span><span style="color: #339933;">&gt;&lt;!--</span>mce<span style="color: #339933;">:</span><span style="color: #CC0000;">1</span><span style="color: #339933;">--&gt;&lt;/</span>script<span style="color: #339933;">&gt;</span></pre></div></div>

<p><strong>JS Explaniation</strong></p>
<p>Ok, the above code it the guts to the script, this allows us to hook into the rpc.php file which carries out all the processing.</p>
<p>The Lookup function takes the word from the input box and POSTS it to the rpc.php page using the jQuery Ajax POST call.</p>
<p><strong>IF</strong> the inputString is &#8217;0&#8242; (Zero), it hides the suggestion box, naturally you would not want this showing if there is nothing in the text box. to search for.</p>
<p><strong>ELSE </strong>we take the &#8216;inputString&#8217;  and post it to the rpc.php page, the jQuery $.post() function is used as follows&#8230;</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">$.<span style="color: #660066;">post</span><span style="color: #009900;">&#40;</span>url<span style="color: #339933;">,</span> <span style="color: #009900;">&#91;</span>data<span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#91;</span>callback<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span></pre></div></div>

<p>The &#8216;callback&#8217; part allows to hook in a function, this is where the real magic if thats what you can call it happens.</p>
<p><strong>IF</strong> the &#8216;data&#8217; returned length is more than zero (ie: there is actually something to show), show the suggestionBox and replace the HTML inside with the returned data.</p>
<p>SIMPLE!</p>
<p><strong>Now for the PHP Backend (RPC.php)</strong></p>
<p>As always my PHP Backend page is called rpc.php (Remote Procedure Call) not that it actually does that, but hey-ho.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// PHP5 Implementation - uses MySQLi.</span>
<span style="color: #000088;">$db</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> mysqli<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'localhost'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'root'</span> <span style="color: #339933;">,</span><span style="color: #0000ff;">''</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'autoComplete'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #000088;">$db</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #666666; font-style: italic;">// Show error if we cannot connect.</span>
    <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'ERROR: Could not connect to the database.'</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #666666; font-style: italic;">// Is there a posted query string?</span>
    <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">isset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'queryString'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$queryString</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'queryString'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
        <span style="color: #666666; font-style: italic;">// Is the string length greater than 0?</span>
        <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">strlen</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$queryString</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;</span>gt<span style="color: #339933;">;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #666666; font-style: italic;">// Run the query: We use LIKE '$queryString%'</span>
        <span style="color: #666666; font-style: italic;">// The percentage sign is a wild-card, in my example of countries it works like this...</span>
        <span style="color: #666666; font-style: italic;">// $queryString = 'Uni';</span>
        <span style="color: #666666; font-style: italic;">// Returned data = 'United States, United Kindom';</span>
&nbsp;
        <span style="color: #000088;">$query</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$db</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>query<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;SELECT value FROM countries WHERE value LIKE '<span style="color: #006699; font-weight: bold;">$queryString</span>%' LIMIT 10&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$query</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #666666; font-style: italic;">// While there are results loop through them - fetching an Object (i like PHP5 btw!).</span>
            <span style="color: #b1b100;">while</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$result</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$query</span> <span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>fetch_object<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                <span style="color: #666666; font-style: italic;">// Format the results, im using</span>
	<span style="color: #339933;">&lt;</span>li<span style="color: #339933;">&gt;</span> <span style="color: #b1b100;">for</span> the <span style="color: #990000;">list</span><span style="color: #339933;">,</span> you can change it<span style="color: #339933;">.</span>
                <span style="color: #666666; font-style: italic;">// The onClick function fills the textbox with the result.</span>
                <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'&lt;/li&gt;
	&lt;li onclick=&quot;fill('</span><span style="color: #0000ff;">'.$result-&amp;gt;value.'</span><span style="color: #0000ff;">');&quot;&gt;'</span><span style="color: #339933;">.</span><span style="color: #000088;">$result</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>value<span style="color: #339933;">.</span><span style="color: #0000ff;">'&lt;/li&gt;
'</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span>
        <span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'ERROR: There was a problem with the query.'</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #666666; font-style: italic;">// Dont do anything.</span>
    <span style="color: #009900;">&#125;</span> <span style="color: #666666; font-style: italic;">// There is a queryString.</span>
<span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'There should be no direct access to this script!'</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
?<span style="color: #339933;">&amp;</span>gt<span style="color: #339933;">;</span></pre></div></div>

<p><strong>PHP Explaination</strong></p>
<p>Im not going to go into much detail here because there are loads of comments in the code above.</p>
<p>Basically, it takes the  &#8216;QueryString&#8217; and does a query with a wildcard at the en.</p>
<p>This means that in this case of the code each key-press generates a new query, this is MySQL intensive if its always being done, but short of making it exceedingly complex it is fine for small scale applications.</p>
<p>The PHP code is the part you have to change to work in your system, and all the it is updating the &#8216;$query&#8217; to your database, you should be able to see where you put the table column name in etc&#8230;</p>
<p><strong>CSS Styling &#8211; im using CSS3, YEA BABY! much easier although limits the functionality to Firefox and Safari.</strong></p>

<div class="wp_syntax"><div class="code"><pre class="css" style="font-family:monospace;">&lt;!--
<span style="color: #6666ff;">.suggestionsBox</span> <span style="color: #00AA00;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">position</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">relative</span><span style="color: #00AA00;">;</span>
    <span style="color: #000000; font-weight: bold;">left</span><span style="color: #00AA00;">:</span> <span style="color: #933;">30px</span><span style="color: #00AA00;">;</span>
    <span style="color: #000000; font-weight: bold;">margin</span><span style="color: #00AA00;">:</span> <span style="color: #933;">10px</span> <span style="color: #933;">0px</span> <span style="color: #933;">0px</span> <span style="color: #933;">0px</span><span style="color: #00AA00;">;</span>
    <span style="color: #000000; font-weight: bold;">width</span><span style="color: #00AA00;">:</span> <span style="color: #933;">200px</span><span style="color: #00AA00;">;</span>
    <span style="color: #000000; font-weight: bold;">background-color</span><span style="color: #00AA00;">:</span> <span style="color: #cc00cc;">#212427</span><span style="color: #00AA00;">;</span>
    -moz-border-radius<span style="color: #00AA00;">:</span> <span style="color: #933;">7px</span><span style="color: #00AA00;">;</span>
    -webkit-border-radius<span style="color: #00AA00;">:</span> <span style="color: #933;">7px</span><span style="color: #00AA00;">;</span>
    <span style="color: #000000; font-weight: bold;">border</span><span style="color: #00AA00;">:</span> <span style="color: #933;">2px</span> <span style="color: #993333;">solid</span> <span style="color: #cc00cc;">#000</span><span style="color: #00AA00;">;</span>
    <span style="color: #000000; font-weight: bold;">color</span><span style="color: #00AA00;">:</span> <span style="color: #cc00cc;">#fff</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span>
&nbsp;
<span style="color: #6666ff;">.suggestionList</span> <span style="color: #00AA00;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">margin</span><span style="color: #00AA00;">:</span> <span style="color: #933;">0px</span><span style="color: #00AA00;">;</span>
    <span style="color: #000000; font-weight: bold;">padding</span><span style="color: #00AA00;">:</span> <span style="color: #933;">0px</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span>
&nbsp;
<span style="color: #6666ff;">.suggestionList</span> li <span style="color: #00AA00;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">margin</span><span style="color: #00AA00;">:</span> <span style="color: #933;">0px</span> <span style="color: #933;">0px</span> <span style="color: #933;">3px</span> <span style="color: #933;">0px</span><span style="color: #00AA00;">;</span>
    <span style="color: #000000; font-weight: bold;">padding</span><span style="color: #00AA00;">:</span> <span style="color: #933;">3px</span><span style="color: #00AA00;">;</span>
    <span style="color: #000000; font-weight: bold;">cursor</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">pointer</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span>
&nbsp;
<span style="color: #6666ff;">.suggestionList</span> li<span style="color: #3333ff;">:hover </span><span style="color: #00AA00;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">background-color</span><span style="color: #00AA00;">:</span> <span style="color: #cc00cc;">#659CD8</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span>
--<span style="color: #00AA00;">&gt;</span></pre></div></div>

<p>The CSS is pretty standard, nothing out of the ordinary.</p>
<p><strong>Main HTML</strong></p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">&amp;lt;div&amp;gt;
&nbsp;
       &amp;lt;div&amp;gt;
&nbsp;
 			Type your county (for the demo):
&lt;input id=&quot;inputString&quot; onkeyup=&quot;lookup(this.value);&quot; size=&quot;30&quot; type=&quot;text&quot; /&gt;
&nbsp;
 		&amp;lt;/div&amp;gt;			&amp;lt;div class=&quot;suggestionsBox&quot; id=&quot;suggestions&quot; style=&quot;display: none;&quot;&amp;gt;
&nbsp;
 			&lt;img style=&quot;position: relative; top: -12px; left: 30px;&quot; src=&quot;upArrow.png&quot; alt=&quot;upArrow&quot; /&gt;
&nbsp;
 			&amp;lt;div class=&quot;suggestionList&quot; id=&quot;autoSuggestionsList&quot;&amp;gt;
&nbsp;
&amp;lt;/div&amp;gt;
&nbsp;
 		&amp;lt;/div&amp;gt;
&nbsp;
&amp;lt;/div&amp;gt;</pre></div></div>

<p>That is the main bit of HTML, really all you need to run this is an input text box with the &#8216;onkeyup&#8217; function set to lookup(this.value); Also, i recommend NOT changing the ID of the input box, unless you change it in the Javascript section <img src='http://www.nodstrum.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p><strong>Screenshots <img src='http://www.nodstrum.com/wp-includes/images/smilies/icon_wink.gif' alt=';-)' class='wp-smiley' /> </strong></p>
<p>I suppose you want to see some more screen shots&#8230;. OK.</p>
<p><img src="http://res.nodstrum.com/autoComplete/screenshots/AutoComplete_2.png" alt="" width="262" height="311" /></p>
<p>And another one!</p>
<p><img src="http://res.nodstrum.com/autoComplete/screenshots/AutoComplete_3.png" alt="" width="252" height="214" /></p>
<p><strong>And now for the links&#8230; this is probably what you are all looking for!</strong></p>
<p>Demo: <a href="http://res.nodstrum.com/autoComplete/">Auto Complete Demo</a></p>
<p>Source ZIP: <a href="http://www.2big2send.com/shared.php?token=76e263f7b764dde59f485e9c38430567">AutoComplete Source ZIP</a></p>
<p>If you need a domain to get you off your local test box buy <a href="http://www.networksolutions.com">domains</a> at Network Solutions!</p>
<p>If you have any problems, just post them up here (as comments) and i am sure i can help you fix it <img src='http://www.nodstrum.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>Thanks for the continued support,</p>
<p>Jamie.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.nodstrum.com/2007/09/19/autocompleter/feed/</wfw:commentRss>
		<slash:comments>1318</slash:comments>
		</item>
	</channel>
</rss>

