<?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>AEXT.NET MAGAZINE &#187; PHP</title>
	<atom:link href="http://aext.net/tag/php/feed/" rel="self" type="application/rss+xml" />
	<link>http://aext.net</link>
	<description>How to Create a Website</description>
	<lastBuildDate>Wed, 30 May 2012 05:03:57 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
		<item>
		<title>Quick Tip: Highlight Author Comments in WordPress &#8211; The Right Way</title>
		<link>http://aext.net/2010/03/highlight-author-comments-wordpress-right-way/</link>
		<comments>http://aext.net/2010/03/highlight-author-comments-wordpress-right-way/#comments</comments>
		<pubDate>Tue, 02 Mar 2010 08:35:06 +0000</pubDate>
		<dc:creator>Lief</dc:creator>
				<category><![CDATA[Quick Tips]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[WordPress Comment]]></category>
		<category><![CDATA[WordPress Tips]]></category>

		<guid isPermaLink="false">http://aext.net/?p=4116</guid>
		<description><![CDATA[<p><a href="http://aext.net/theme/platinoom" title="Platinoom - Premium WordPress Themes"><img src="http://aext.net/wp-content/uploads/2011/04/platinoom_wordpress_theme.png" alt="ThemeKiss" /></a></p><p style="float: right;"><a href="http://api.tweetmeme.com/share?url=http://aext.net/2010/03/highlight-author-comments-wordpress-right-way/&service=su.pr&service_api=b57727e991c454bd2b2c62ff71462c79"><img src="http://api.tweetmeme.com/imagebutton.gif?url=http://aext.net/2010/03/highlight-author-comments-wordpress-right-way/" height="61" width="51" /></a></p>]]></description>
			<content:encoded><![CDATA[<p><a href="http://aext.net/theme/platinoom" title="Platinoom - Premium WordPress Themes"><img src="http://aext.net/wp-content/uploads/2011/04/platinoom_wordpress_theme.png" alt="ThemeKiss" /></a></p><p style="float: right;"><a href="http://api.tweetmeme.com/share?url=http://aext.net/2010/03/highlight-author-comments-wordpress-right-way/&service=su.pr&service_api=b57727e991c454bd2b2c62ff71462c79"><img src="http://api.tweetmeme.com/imagebutton.gif?url=http://aext.net/2010/03/highlight-author-comments-wordpress-right-way/" height="61" width="51" /></a></p><p>This is an old topic that has been covered by many other blogs and magazines&#8211;to the extreme, but it needs updating. I&#8217;m talking about how to highlight comments posted by the author of the article. The tutorials out there only work for one user ID for the whole site. That is a big mistake, as it didn&#8217;t allow for scalability &#8212; it only works when you are blogging on your own (and with only one account). We have given it an overhaul, and updated the code.</p>
<p><span id="more-4116"></span></p>
<p>First, we should take a look at the code from the tutorials which highlight the comment based on the user ID:</p>
<pre class="html">
.authcomment {
&nbsp;&nbsp;&nbsp;&nbsp;background-color: #B3FFCC !important;
}

&lt;li class=&rdquo;&lt;?php 

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if (1 == $comment-&gt;user_id)
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$oddcomment = &ldquo;authcomment&rdquo;;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo $oddcomment;?&gt;&rdquo; id=&rdquo;comment&hellip;&quot;&gt;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;...
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&lt;/li&gt;
</pre>
<p>To make it work on sites with multiple writers, we need to to get the ID of the author, then check if the ID of the person posting this comment is the same. If it is, we need to make it stand out. We still use the CSS class for post author comment.</p>
<pre class="css">
.authcomment {
&nbsp;&nbsp;&nbsp;&nbsp;background-color: #B3FFCC !important;
}
</pre>
<p>Now, we change the original code at the start of the article with this:</p>
<pre class="html">
&lt;li class=&quot;comment-container
&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&lt;?php
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$authID=get_the_author_meta('ID');
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if($authID == $comment-&gt;user_id)
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$oddcomment = 'authcomment';
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo $oddcomment;
&nbsp;&nbsp;&nbsp;&nbsp;?&gt;&quot; &nbsp;id=&quot;comment-&lt;?php comment_ID(); ?&gt;&quot;&gt;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;...
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&lt;/li&gt;
</pre>
<p>And, that&#8217;s it! We hope that you find it useful! </p>
]]></content:encoded>
			<wfw:commentRss>http://aext.net/2010/03/highlight-author-comments-wordpress-right-way/feed/</wfw:commentRss>
		<slash:comments>53</slash:comments>
		</item>
		<item>
		<title>WordPress Hack: Anything Can Be Added Anywhere In The Post Content</title>
		<link>http://aext.net/2009/10/wordpress-hack-anything-can-be-added-anywhere-in-the-post-content/</link>
		<comments>http://aext.net/2009/10/wordpress-hack-anything-can-be-added-anywhere-in-the-post-content/#comments</comments>
		<pubDate>Sat, 24 Oct 2009 06:07:05 +0000</pubDate>
		<dc:creator>Lief</dc:creator>
				<category><![CDATA[WordPress]]></category>
		<category><![CDATA[Hack]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[the_content]]></category>
		<category><![CDATA[Wordpress]]></category>
		<category><![CDATA[Wordpress Theme]]></category>

		<guid isPermaLink="false">http://aext.net/?p=1182</guid>
		<description><![CDATA[<p><a href="http://aext.net/theme/platinoom" title="Platinoom - Premium WordPress Themes"><img src="http://aext.net/wp-content/uploads/2011/04/platinoom_wordpress_theme.png" alt="ThemeKiss" /></a></p><p style="float: right;"><a href="http://api.tweetmeme.com/share?url=http://aext.net/2009/10/wordpress-hack-anything-can-be-added-anywhere-in-the-post-content/&service=su.pr&service_api=b57727e991c454bd2b2c62ff71462c79"><img src="http://api.tweetmeme.com/imagebutton.gif?url=http://aext.net/2009/10/wordpress-hack-anything-can-be-added-anywhere-in-the-post-content/" height="61" width="51" /></a></p>Some of wordpress plugins automatically add their display below your post content which is such as: YARPP (Related Posts) or Sociable .... They added their displays into your post content, but sometime, you want to add your own code before them. This article will tell you how to add anything to your wordpress theme just right before others plugin. <a href="http://aext.net/2009/10/wordpress-hack-anything-can-be-added-anywhere-in-the-post-content/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p><a href="http://aext.net/theme/platinoom" title="Platinoom - Premium WordPress Themes"><img src="http://aext.net/wp-content/uploads/2011/04/platinoom_wordpress_theme.png" alt="ThemeKiss" /></a></p><p style="float: right;"><a href="http://api.tweetmeme.com/share?url=http://aext.net/2009/10/wordpress-hack-anything-can-be-added-anywhere-in-the-post-content/&service=su.pr&service_api=b57727e991c454bd2b2c62ff71462c79"><img src="http://api.tweetmeme.com/imagebutton.gif?url=http://aext.net/2009/10/wordpress-hack-anything-can-be-added-anywhere-in-the-post-content/" height="61" width="51" /></a></p><p>Some of wordpress plugins automatically add their display below your post content which is such as: YARPP (Related Posts) or Sociable &#8230;. They added their displays into your post content, but sometime, you want to add your own code before them, even between the introduction and the main content. This article will tell you how to add anything to your wordpress theme just right before others plugin do.</p>
<p><span id="more-1182"></span></p>
<p>This is the first tutorial that I don&#8217;t offer the download link. Just follow these simple steps and you&#8217;ll get the nice results.</p>
<h3>Hack your theme functions.php file</h3>
<hr />
<p>The only thing you want to do is just hack your theme <em>functions.php</em>file.</p>
<p>Open this file.</p>
<p>In this article, I will add the author bio box into wordpress theme before the Related Posts displays. Write a function that return the author bio box, you can write anywhere, but at the end of your <em>functions.php</em> is avoid messing up your file:</p>
<pre class="php"><span style="color: #881350;">function</span> <span style="color: #003369;">get_author_bio </span>(<span style="color: #825900;">$content</span>=<span style="color: #eb7300;">''</span>){<span style="color: #881350;">Â </span><span style="color: #825900;">Â </span>

Â Â Â Â <span style="color: #881350;">if</span>(!<span style="color: #003369;">in_category</span>(<span style="color: #eb7300;">"Community News"</span>)) {
Â Â Â Â <span style="color: #825900;">$post_author_name</span>=<span style="color: #003369;">get_the_author_meta</span>(<span style="color: #eb7300;">"display_name"</span>);
Â Â Â Â <span style="color: #825900;">$post_author_description</span>=<span style="color: #003369;">get_the_author_meta</span>(<span style="color: #eb7300;">"description"</span>); Â Â Â Â Â 

Â Â Â Â <span style="color: #825900;">$html</span>=<span style="color: #eb7300;">"&lt;div class='clearfix' id='about_author'&gt;\n"</span>;
Â Â Â Â <span style="color: #825900;">$html</span>.=<span style="color: #eb7300;">"&lt;img width='80' height='80' class='avatar' src='http://www.gravatar.com/avatar.php?gravatar_id="</span>.<span style="color: #661aa9;">md5</span>(<span style="color: #003369;">get_the_author_email</span>()). <span style="color: #eb7300;">"&amp;default="</span>.<span style="color: #661aa9;">urlencode</span>(<span style="color: #825900;">$GLOBALS</span>[<span style="color: #eb7300;">'defaultgravatar'</span>]).<span style="color: #eb7300;">"&amp;size=80&amp;r=PG' alt='PG'/&gt;\n"</span>;

Â Â Â Â <span style="color: #825900;">$html</span>.=<span style="color: #eb7300;">"&lt;div class='author_text'&gt;\n"</span>;
Â Â Â Â <span style="color: #825900;">$html</span>.=<span style="color: #eb7300;">"&lt;h4&gt;Author: &lt;span&gt;"</span>.<span style="color: #825900;">$post_author_name</span>.<span style="color: #eb7300;">"&lt;/span&gt;&lt;/h4&gt;\n"</span>;
Â Â Â Â <span style="color: #825900;">$html</span>.= <span style="color: #825900;">$post_author_description</span>.<span style="color: #eb7300;">"\n"</span>;
Â Â Â Â <span style="color: #825900;">$html</span>.=<span style="color: #eb7300;">"&lt;/div&gt;\n"</span>;
Â Â Â Â <span style="color: #825900;">$html</span>.=<span style="color: #eb7300;">"&lt;div class='clear'&gt;&lt;/div&gt;\n"</span>;
Â Â Â Â <span style="color: #825900;">$html</span>.=<span style="color: #eb7300;">"&lt;/div&gt;"</span>;
Â Â Â Â <span style="color: #825900;">$content</span> .= <span style="color: #825900;">$html</span>;

Â Â Â Â }

Â Â Â Â <span style="color: #881350;">return</span> <span style="color: #825900;">$content</span>;

}</pre>
<p>In WordPress 2.8 and above, <em>the_author_description();</em> has been deprecated and replaced with another function: <em>the_author_meta(&#8216;description&#8217;);</em>. However, you have to use <em>get_the_author_meta()</em> if you need to return (not display) the information. Take a look at this function&#8217;s information at <a href="http://codex.wordpress.org/Template_Tags/the_author_meta">WordPress Codex</a>.</p>
<p>In this function, I set the condition that the bio box will not display if this post is in <strong>Community News</strong> category. I&#8217;m using the same as above. This function is not return the bio box, it joins the post content and bio box content into one.</p>
<p>Next, you need to hook this function to <em>the_content</em> with code:</p>
<pre class="html"><span style="color: #003369;">add_filter</span>(<span style="color: #eb7300;">'the_content'</span>, <span style="color: #eb7300;">'get_author_bio'</span>);</pre>
<p>For the example, before you use this hack, your bio box will display after the Related Posts, likes this:</p>
<div class="bigimage"><img class="aligncenter size-full wp-image-1193" title="wordpress hack bio box" src="http://aext.net/wp-contents/uploads/2009/10/wordpress-hack-bio-box.jpg" alt="wordpress hack bio box" width="500" height="335" /></div>
<p>But after you did some hacks in <em>functions.php</em> file, you will get a result likes mine at the end of this post.</p>
<p>To display something at the begin, easy as adding it normally into your post theme file: <em>single.php</em></p>
<p>Sometime, you want to display your ads right below your post&#8217;s introduction. We will need to split the content into two parts, then insert ads code at the middle. Let&#8217;s write a function likes this:</p>
<pre class="php"><span style="color: #881350;">function</span> <span style="color: #003369;">put_middle_content</span>(<span style="color: #825900;">$content</span>=<span style="color: #eb7300;">''</span>) {

Â Â Â Â <span style="color: #881350;">if</span>( <span style="color: #661aa9;">preg_match</span>(<span style="color: #eb7300;">'/&lt;span id="(.*?)?"&gt;&lt;\/span&gt;/'</span>, <span style="color: #825900;">$content</span>, <span style="color: #825900;">$matches</span>) ) {

Â Â Â Â Â Â Â Â <span style="color: #825900;">$content</span> = <span style="color: #661aa9;">explode</span>(<span style="color: #825900;">$matches</span>[<span style="color: #0000ff;">0</span>], <span style="color: #825900;">$content</span>, <span style="color: #0000ff;">2</span>);

Â Â Â Â Â Â Â Â <span style="color: #825900;">$content</span> = <span style="color: #825900;">$content</span>[<span style="color: #0000ff;">0</span>] . <span style="color: #825900;">$matches</span>[<span style="color: #0000ff;">0</span>] . <span style="color: #eb7300;">'[YOUR ADS ARE HERE]'</span> . <span style="color: #825900;">$content</span>[<span style="color: #0000ff;">1</span>];
Â Â Â Â }

Â Â Â Â <span style="color: #881350;">return</span> <span style="color: #825900;">$content</span>;

}</pre>
<p>If your post has <em>More</em> tag, this function will split the content into 2 parts with delimiter is the <em>More</em> tag. Then, It will combine the content array again with the adding between 2 array strings.</p>
<p>Adding the code below to complete:</p>
<pre class="html"><span style="color: #003369;">add_filter</span>(<span style="color: #eb7300;">'the_content'</span>, <span style="color: #eb7300;">'put_middle_content'</span>);</pre>
<h3>That&#8217;s all</h3>
<hr />
<p>Yup, that&#8217;s all. Now, you can add anything you want to anywhere, such as: top, middle, and right after the post content that you could not do with the template display file. Your adding&#8217;s priority will be always the first.</p>
<div class="txtad-cont"></div>
]]></content:encoded>
			<wfw:commentRss>http://aext.net/2009/10/wordpress-hack-anything-can-be-added-anywhere-in-the-post-content/feed/</wfw:commentRss>
		<slash:comments>80</slash:comments>
		</item>
		<item>
		<title>CodeIgniter and jQuery &#8211; Real Live Search with Pagination</title>
		<link>http://aext.net/2009/10/codeigniter-and-jquery-real-live-search-with-pagination/</link>
		<comments>http://aext.net/2009/10/codeigniter-and-jquery-real-live-search-with-pagination/#comments</comments>
		<pubDate>Wed, 14 Oct 2009 05:00:31 +0000</pubDate>
		<dc:creator>Lief</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Popular]]></category>
		<category><![CDATA[Ajax]]></category>
		<category><![CDATA[CodeIgniter]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[jQuery Tutorial]]></category>
		<category><![CDATA[PHP Framework]]></category>
		<category><![CDATA[PHP Tutorial]]></category>
		<category><![CDATA[search]]></category>

		<guid isPermaLink="false">http://aext.net/?p=1015</guid>
		<description><![CDATA[<p><a href="http://aext.net/theme/platinoom" title="Platinoom - Premium WordPress Themes"><img src="http://aext.net/wp-content/uploads/2011/04/platinoom_wordpress_theme.png" alt="ThemeKiss" /></a></p><p style="float: right;"><a href="http://api.tweetmeme.com/share?url=http://aext.net/2009/10/codeigniter-and-jquery-real-live-search-with-pagination/&service=su.pr&service_api=b57727e991c454bd2b2c62ff71462c79"><img src="http://api.tweetmeme.com/imagebutton.gif?url=http://aext.net/2009/10/codeigniter-and-jquery-real-live-search-with-pagination/" height="61" width="51" /></a></p>In this tutorial, we will create a search page with CodeIgniter and jQuery. We're not gonna create only a default search page using CodeIgniter framework, but also a real time search with jQuery's support. And one more, enable GET method in CodeIginter that was stupidly disable by default. Enjoy the tut and don't forget the ... beer! <a href="http://aext.net/2009/10/codeigniter-and-jquery-real-live-search-with-pagination/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p><a href="http://aext.net/theme/platinoom" title="Platinoom - Premium WordPress Themes"><img src="http://aext.net/wp-content/uploads/2011/04/platinoom_wordpress_theme.png" alt="ThemeKiss" /></a></p><p style="float: right;"><a href="http://api.tweetmeme.com/share?url=http://aext.net/2009/10/codeigniter-and-jquery-real-live-search-with-pagination/&service=su.pr&service_api=b57727e991c454bd2b2c62ff71462c79"><img src="http://api.tweetmeme.com/imagebutton.gif?url=http://aext.net/2009/10/codeigniter-and-jquery-real-live-search-with-pagination/" height="61" width="51" /></a></p><p>In this tutorial, we will create a search page with CodeIgniter and jQuery. We&#8217;re not gonna create only a default search page using CodeIgniter framework, but also a real time search with jQuery&#8217;s support. And one more, enable GET method in CodeIginter that was stupidly disable by default. Enjoy the tut and don&#8217;t forget the &#8230; beer!<span id="more-1015"></span></p>
<p>You can take a look at the demo or download it right now, then close this page immediately if you want to. But I highly recommend that you should read the whole tutorial, so you will not miss any interesting in this entry.</p>
<p>Try to search with the keywords: jquery, html, code &#8230;</p>
<div class="demo-download">
<ul>
<li><a href="http://aext.net/Downloads/LiveSearch.zip" target="_blank"><span class="downloadit">Download</span></a></li>
<li><a class="demoit" href="http://aext.net/example/codeigniter/index.php/search">Demo</a></li>
</ul>
</div>
<h3>1. The stuffs I&#8217;m using</h3>
<hr />
<ul class="mylist">
<li><strong>PHP Framework:</strong> <a href="http://codeigniter.com/downloads/">CodeIgniter</a></li>
<li><strong>Javascript Framework:</strong> <a href="http://ajax.googleapis.com/ajax/libs/jquery/1.3/jquery.min.js">jQuery 1.3.2</a></li>
<li><strong>Debug tool:</strong> <a href="http://getfirebug.com/">Firebug</a></li>
</ul>
<h3>2. Configuring CodeIgniter</h3>
<hr />
<p>At first, download CodeIgniter (CI) with the link above and extract package to your workspace. Note, your web site will be located in CodeIginiter folder by default. Then, you need to make some changes to Codeiginter within the CI config section. In this tutorial, we&#8217;re using PHP to connect to MySQL database to get data.</p>
<p>Open the <strong>system/application/config/autoload.php</strong>, find the following code:</p>
<pre class="php"><em><span style="color: #236e25;">/* | ------------------------------------------------------------------- | Â Auto-load Libraries | ------------------------------------------------------------------- | These are the classes located in the system/libraries folder | or in your system/application/libraries folder. | | Prototype: | | Â Â $autoload['libraries'] = array('database', 'session', 'xmlrpc'); */</span></em>

<span style="color: #825900;">$autoload</span>[<span style="color: #eb7300;">'libraries'</span>] = <span style="color: #881350;">array</span>();</pre>
<p>Change the code above as below:</p>
<pre class="php"><span style="color: #825900;">$autoload</span>[<span style="color: #eb7300;">'libraries'</span>] = <span style="color: #881350;">array</span>(<span style="color: #eb7300;">'database'</span>);</pre>
<p>Because we&#8217;re gonna use Database to store and fetch from, so we load the <strong>database</strong> library. That&#8217;s all what it means. And each time you run your web page, it will automatically load the database connection.</p>
<p>The second, open up <strong>database.php</strong> in the same folder then find following code and change it to your database setting.</p>
<pre class="php"><span style="color: #825900;">$db</span>[<span style="color: #eb7300;">'default'</span>][<span style="color: #eb7300;">'hostname'</span>] = <span style="color: #eb7300;">"localhost"</span>;
<span style="color: #825900;">$db</span>[<span style="color: #eb7300;">'default'</span>][<span style="color: #eb7300;">'username'</span>] = <span style="color: #eb7300;">"root"</span>;
<span style="color: #825900;">$db</span>[<span style="color: #eb7300;">'default'</span>][<span style="color: #eb7300;">'password'</span>] = <span style="color: #eb7300;">"root"</span>;
<span style="color: #825900;">$db</span>[<span style="color: #eb7300;">'default'</span>][<span style="color: #eb7300;">'database'</span>] = <span style="color: #eb7300;">"aext"</span>;</pre>
<p>The next important part in CI Configuration is you have to config you default url of your website. This is a base url inside <strong>config.php</strong> file.<br />
Open this file in then find and edit your <strong>base_url</strong> as below:</p>
<pre class="php"><em><span style="color: #236e25;">/* |--------------------------------------------------------------------- | Base Site URL |--------------------------------------------------------------------- | | URL to your CodeIgniter root. Typically this will be your base URL, | WITH a trailing slash: | | Â Â http://example.com/ | */</span></em>
<span style="color: #825900;">$config</span>[<span style="color: #eb7300;">'base_url'</span>] = <span style="color: #eb7300;">"http://project/tutorial/"</span>;</pre>
<p>In CodeIgniter 1.7.2, there&#8217;re already some changes. Who follows the tutorial will need to activate the following, in <strong>autoload.php</strong> to make the base_url() function works:</p>
<pre class="php"><em><span style="color: #236e25;">/* | ------------------------------------------------------------------- | Â Auto-load Helper Files | ------------------------------------------------------------------- | Prototype: | | Â Â $autoload['helper'] = array('url', 'file'); */</span></em>

<span style="color: #825900;">$autoload</span>[<span style="color: #eb7300;">'helper'</span>] = <span style="color: #881350;">array</span>(<span style="color: #eb7300;">'url'</span>);</pre>
<p>Default using of <strong>base_url</strong> when you want to call it in your view is as example below:</p>
<pre class="html">Â Â Â Â <span style="color: #881280;">&lt;link </span><span style="color: #994500;">media</span><span style="color: #881280;">=</span><span style="color: #1a1aa6;">"screen"</span><span style="color: #994500;">type</span><span style="color: #881280;">=</span><span style="color: #1a1aa6;">"text/css"</span><span style="color: #881280;"> Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â </span><span style="color: #994500;">href</span><span style="color: #881280;">=</span><span style="color: #1a1aa6;">"</span><span style="color: #dd0000;">&lt;?php</span> <span style="color: #881350;">echo</span> <span style="color: #003369;">base_url</span>(); <span style="color: #dd0000;">?&gt;</span><span style="color: #1a1aa6;">css/style.css"</span><span style="color: #881280;"> /&gt;</span></pre>
<h3>2. Database</h3>
<hr />
<p>Wow, it&#8217;s a large database file size if you build a search engineer. So, I will use my old WordPress database and the search will fetch data from my posts table. If you are setting up a search for a series of articles or a site with lots of product-related content, a <strong>MySQL FULLTEXT</strong> search can make it very easy to find articles or products related to the keywords used by a searcher. Read <a href="http://aext.net/2008/07/using-a-mysql-full-text-search/">this entry</a> to know more about <strong>FULLTEXT</strong></p>
<p>.</p>
<p>Add <strong>FULLTEXT</strong> to the data fileds you want to index. In this tutorial, I will search through table wp_post and the result will be based on the search term within <strong>post_title</strong> and <strong>post_content </strong>fileds.</p>
<pre class="mysql">ALTER TABLE `wp_posts` ADD FULLTEXT (
`post_title` ,
`post_content`
);</pre>
<p>So, if I&#8217;m using <strong>FULLTEXT</strong> search, my search query is simple:</p>
<pre class="mysql">SELECT * FROM wp_posts WHERE  MATCH ( post_title, post_content ) AGAINST (?)  AND post_status='publish'</pre>
<p><em>(?) is your search term.</em></p>
<p>Download the sample data file in my attachment then import to your database.</p>
<h3>3. Understanding the CodeIgniter</h3>
<hr />
<p>CI is a framework was help php developers build a website base on <strong>M</strong>odelâ€“<strong>V</strong>iewâ€“<strong>C</strong>ontroller architecture.</p>
<p style="text-align: center;"><img class="size-full wp-image-1038 aligncenter" title="Model View Controller Diagram" src="http://aext.net/wp-contents/uploads/2009/10/350px-ModelViewControllerDiagram.svg.png" alt="Model View Controller Diagram" width="350" height="165" /></p>
<p><strong>MVC in CodeIginter:</strong></p>
<ul class="mylist">
<li><strong>Model:</strong> Collection of php classes and functions stored in <strong>system/application/models</strong></li>
<li><strong>View:</strong> The view is represented by PHP file. All the PHP files for displaying located in <strong>system/application/views</strong></li>
<li><strong>Controller:</strong> The controller communicates with the View and Model. These files located in <strong>system/application/controllers</strong></li>
</ul>
<p>Let&#8217;s begin to work with CodeIgniter!</p>
<h3>4. Building the Model</h3>
<hr />
<p>Go to <strong>system/application/models</strong> and create a new file called <strong>search_model.php</strong></p>
<p>.</p>
<p>The main function that used to get results from database by search keyword is:</p>
<pre class="php">Â Â Â Â <em><span style="color: #236e25;">// Get total posts by search query </span></em>Â Â Â Â <span style="color: #881350;">function</span> <span style="color: #003369;">get_posts</span>(<span style="color: #825900;">$query</span>, <span style="color: #825900;">$post_per_page</span>,<span style="color: #825900;">$current_page</span>) {

Â Â Â Â Â Â Â Â <span style="color: #825900;">$offset</span>=(<span style="color: #825900;">$current_page</span>-<span style="color: #0000ff;">1</span>)*<span style="color: #825900;">$post_per_page</span>;
Â Â Â Â Â Â Â Â <span style="color: #881350;">if</span>(<span style="color: #825900;">$current_page</span>==<span style="color: #0000ff;">1</span>) {
Â Â Â Â Â Â Â Â Â Â Â Â <span style="color: #825900;">$offset</span> = <span style="color: #0000ff;">0</span>;
Â Â Â Â Â Â Â Â }

Â Â Â Â Â Â Â Â <span style="color: #825900;">$sql</span> = <span style="color: #eb7300;">"SELECT * FROM wp_posts WHERE Â MATCH ( post_title, post_content ) AGAINST (?) Â AND post_status='publish' LIMIT ?,?"</span>;
Â Â Â Â Â Â Â Â <span style="color: #825900;">$query</span> = <span style="color: #825900;">$this</span>-&gt;db-&gt;<span style="color: #661aa9;">query</span>(<span style="color: #825900;">$sql</span>, <span style="color: #881350;">array</span>(<span style="color: #825900;">$query</span>, <span style="color: #825900;">$offset</span>, <span style="color: #825900;">$post_per_page</span>));

Â Â Â Â Â Â Â Â <span style="color: #881350;">return</span> <span style="color: #825900;">$query</span>-&gt;<span style="color: #003369;">result_array</span>();
Â Â Â Â }</pre>
<p>Of course, we only need the posts with status is published because wordpress stores a lot of posts in the table for revision.<br />
Next, create a function that will calculate the total of rows, we need that number for pagination.</p>
<pre class="php">Â Â Â Â <em><span style="color: #236e25;">// Get the number of rows, use for pagination </span></em>Â Â Â Â <span style="color: #881350;">function</span> <span style="color: #003369;">get_numrows</span>(<span style="color: #825900;">$query</span>) {

Â Â Â Â Â Â Â Â <span style="color: #825900;">$rows</span>=<span style="color: #0000ff;">0</span>;

Â Â Â Â Â Â Â Â <span style="color: #825900;">$sql</span> = <span style="color: #eb7300;">"SELECT * FROM wp_posts WHERE Â MATCH ( post_title, post_content ) AGAINST (?) Â AND post_status='publish'"</span>;
Â Â Â Â Â Â Â Â <span style="color: #825900;">$query</span> = <span style="color: #825900;">$this</span>-&gt;db-&gt;<span style="color: #661aa9;">query</span>(<span style="color: #825900;">$sql</span>, <span style="color: #881350;">array</span>(<span style="color: #825900;">$query</span>));

Â Â Â Â Â Â Â Â <span style="color: #825900;">$rows</span>=<span style="color: #825900;">$query</span>-&gt;<span style="color: #661aa9;">num_rows</span>();

Â Â Â Â Â Â Â Â <span style="color: #881350;">return</span> <span style="color: #825900;">$rows</span>;
Â Â Â Â }</pre>
<p>The next function highlight the search keywords. Continue to write some code in your Controller:</p>
<pre class="php">Â Â Â Â <span style="color: #881350;">function</span> <span style="color: #003369;">highlightWords</span>(<span style="color: #825900;">$string</span>,<span style="color: #825900;">$words</span>,<span style="color: #825900;">$ajax</span>=<span style="color: #881350;">false</span>){

Â Â Â Â Â Â Â Â <span style="color: #825900;">$words</span>=<span style="color: #661aa9;">explode</span>(<span style="color: #eb7300;">' '</span>,<span style="color: #825900;">$words</span>);

Â Â Â Â Â Â Â Â <span style="color: #881350;">for</span>(<span style="color: #825900;">$i</span>=<span style="color: #0000ff;">0</span>;<span style="color: #825900;">$i</span>&lt;<span style="color: #661aa9;">sizeOf</span>(<span style="color: #825900;">$words</span>);<span style="color: #825900;">$i</span>++) {

Â Â Â Â Â Â Â Â Â Â Â Â <span style="color: #881350;">if</span>(<span style="color: #825900;">$ajax</span>==<span style="color: #881350;">true</span>)
Â Â Â Â Â Â Â Â Â Â Â Â {
Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â <span style="color: #825900;">$string</span>=<span style="color: #661aa9;">str_ireplace</span>(<span style="color: #825900;">$words</span>[<span style="color: #825900;">$i</span>], <span style="color: #eb7300;">'&lt;strong class=\"highlight\"&gt;'</span>.<span style="color: #825900;">$words</span>[<span style="color: #825900;">$i</span>].<span style="color: #eb7300;">'&lt;\/strong&gt;'</span>, <span style="color: #825900;">$string</span>);
Â Â Â Â Â Â Â Â Â Â Â Â } <span style="color: #881350;">else</span> {
Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â <span style="color: #825900;">$string</span>=<span style="color: #661aa9;">str_ireplace</span>(<span style="color: #825900;">$words</span>[<span style="color: #825900;">$i</span>], <span style="color: #eb7300;">'&lt;strong class="highlight"&gt;'</span>.<span style="color: #825900;">$words</span>[<span style="color: #825900;">$i</span>].<span style="color: #eb7300;">'&lt;/strong&gt;'</span>, <span style="color: #825900;">$string</span>);
Â Â Â Â Â Â Â Â Â Â Â Â }

Â Â Â Â Â Â Â Â }

Â Â Â Â Â Â Â Â <span style="color: #881350;">return</span> <span style="color: #825900;">$string</span>;
Â Â Â Â }</pre>
<p>Because <strong>FULLTEXT</strong> will find the results by each word in the search phrase, so we need to split our search phrase first. This function will split the keyword into parts then highlight each word in the result.</p>
<p>Furthermore, you need a function that clean up the HTML format for displaying. The purpose of this function I will talk later in jQuery part.</p>
<pre class="php">Â Â Â Â <span style="color: #881350;">function</span> <span style="color: #003369;">cleanHTML</span>(<span style="color: #825900;">$input</span>, <span style="color: #825900;">$ending</span>=<span style="color: #eb7300;">'...'</span>) {

Â Â Â Â Â Â Â Â <span style="color: #825900;">$output</span> = <span style="color: #661aa9;">strip_tags</span>(<span style="color: #825900;">$input</span>);

Â Â Â Â Â Â Â Â <span style="color: #825900;">$output</span> = <span style="color: #661aa9;">substr</span>(<span style="color: #825900;">$output</span>, <span style="color: #0000ff;">0</span>, <span style="color: #0000ff;">100</span>);
Â Â Â Â Â Â Â Â <span style="color: #825900;">$output</span> .= <span style="color: #825900;">$ending</span>;

Â Â Â Â Â Â Â Â <span style="color: #881350;">return</span> <span style="color: #825900;">$output</span>;
Â Â Â Â }</pre>
<p>The completed code of the Model:</p>
<pre class="php"><span style="color: #dd0000;">&lt;?php</span>

<span style="color: #881350;">class</span> Search_model <span style="color: #881350;">extends</span> Model {

Â Â Â Â <em><span style="color: #236e25;">// Get total posts by search query </span></em>Â Â Â Â <span style="color: #881350;">function</span> <span style="color: #003369;">get_posts</span>(<span style="color: #825900;">$query</span>, <span style="color: #825900;">$post_per_page</span>,<span style="color: #825900;">$current_page</span>) {

Â Â Â Â Â Â Â Â <span style="color: #825900;">$offset</span>=(<span style="color: #825900;">$current_page</span>-<span style="color: #0000ff;">1</span>)*<span style="color: #825900;">$post_per_page</span>;
Â Â Â Â Â Â Â Â <span style="color: #881350;">if</span>(<span style="color: #825900;">$current_page</span>==<span style="color: #0000ff;">1</span>) {
Â Â Â Â Â Â Â Â Â Â Â Â <span style="color: #825900;">$offset</span> = <span style="color: #0000ff;">0</span>;
Â Â Â Â Â Â Â Â }

Â Â Â Â Â Â Â Â <span style="color: #825900;">$sql</span> = <span style="color: #eb7300;">"SELECT * FROM wp_posts WHERE Â MATCH ( post_title, post_content ) AGAINST (?) Â AND post_status='publish' LIMIT ?,?"</span>;
Â Â Â Â Â Â Â Â <span style="color: #825900;">$query</span> = <span style="color: #825900;">$this</span>-&gt;db-&gt;<span style="color: #661aa9;">query</span>(<span style="color: #825900;">$sql</span>, <span style="color: #881350;">array</span>(<span style="color: #825900;">$query</span>, <span style="color: #825900;">$offset</span>, <span style="color: #825900;">$post_per_page</span>));

Â Â Â Â Â Â Â Â <span style="color: #881350;">return</span> <span style="color: #825900;">$query</span>-&gt;<span style="color: #003369;">result_array</span>();
Â Â Â Â }

Â Â Â Â <em><span style="color: #236e25;">// Get the number of rows, use for pagination </span></em>Â Â Â Â <span style="color: #881350;">function</span> <span style="color: #003369;">get_numrows</span>(<span style="color: #825900;">$query</span>) {

Â Â Â Â Â Â Â Â <span style="color: #825900;">$rows</span>=<span style="color: #0000ff;">0</span>;

Â Â Â Â Â Â Â Â <span style="color: #825900;">$sql</span> = <span style="color: #eb7300;">"SELECT * FROM wp_posts WHERE Â MATCH ( post_title, post_content ) AGAINST (?) Â AND post_status='publish'"</span>;
Â Â Â Â Â Â Â Â <span style="color: #825900;">$query</span> = <span style="color: #825900;">$this</span>-&gt;db-&gt;<span style="color: #661aa9;">query</span>(<span style="color: #825900;">$sql</span>, <span style="color: #881350;">array</span>(<span style="color: #825900;">$query</span>));

Â Â Â Â Â Â Â Â <span style="color: #825900;">$rows</span>=<span style="color: #825900;">$query</span>-&gt;<span style="color: #661aa9;">num_rows</span>();

Â Â Â Â Â Â Â Â <span style="color: #881350;">return</span> <span style="color: #825900;">$rows</span>;
Â Â Â Â }

Â Â Â Â <span style="color: #881350;">function</span> <span style="color: #003369;">highlightWords</span>(<span style="color: #825900;">$string</span>,<span style="color: #825900;">$words</span>,<span style="color: #825900;">$ajax</span>=<span style="color: #881350;">false</span>){

Â Â Â Â Â Â Â Â <span style="color: #825900;">$words</span>=<span style="color: #661aa9;">explode</span>(<span style="color: #eb7300;">' '</span>,<span style="color: #825900;">$words</span>);

Â Â Â Â Â Â Â Â <span style="color: #881350;">for</span>(<span style="color: #825900;">$i</span>=<span style="color: #0000ff;">0</span>;<span style="color: #825900;">$i</span>&lt;<span style="color: #661aa9;">sizeOf</span>(<span style="color: #825900;">$words</span>);<span style="color: #825900;">$i</span>++) {

Â Â Â Â Â Â Â Â Â Â Â Â <span style="color: #881350;">if</span>(<span style="color: #825900;">$ajax</span>==<span style="color: #881350;">true</span>)
Â Â Â Â Â Â Â Â Â Â Â Â {
Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â <span style="color: #825900;">$string</span>=<span style="color: #661aa9;">str_ireplace</span>(<span style="color: #825900;">$words</span>[<span style="color: #825900;">$i</span>], <span style="color: #eb7300;">'&lt;strong class=\"highlight\"&gt;'</span>.<span style="color: #825900;">$words</span>[<span style="color: #825900;">$i</span>].<span style="color: #eb7300;">'&lt;\/strong&gt;'</span>, <span style="color: #825900;">$string</span>);
Â Â Â Â Â Â Â Â Â Â Â Â } <span style="color: #881350;">else</span> {
Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â <span style="color: #825900;">$string</span>=<span style="color: #661aa9;">str_ireplace</span>(<span style="color: #825900;">$words</span>[<span style="color: #825900;">$i</span>], <span style="color: #eb7300;">'&lt;strong class="highlight"&gt;'</span>.<span style="color: #825900;">$words</span>[<span style="color: #825900;">$i</span>].<span style="color: #eb7300;">'&lt;/strong&gt;'</span>, <span style="color: #825900;">$string</span>);
Â Â Â Â Â Â Â Â Â Â Â Â }

Â Â Â Â Â Â Â Â }

Â Â Â Â Â Â Â Â <span style="color: #881350;">return</span> <span style="color: #825900;">$string</span>;
Â Â Â Â }

Â Â Â Â <span style="color: #881350;">function</span> <span style="color: #003369;">cleanHTML</span>(<span style="color: #825900;">$input</span>, <span style="color: #825900;">$ending</span>=<span style="color: #eb7300;">'...'</span>) {

Â Â Â Â Â Â Â Â <span style="color: #825900;">$output</span> = <span style="color: #661aa9;">strip_tags</span>(<span style="color: #825900;">$input</span>);

Â Â Â Â Â Â Â Â <span style="color: #825900;">$output</span> = <span style="color: #661aa9;">substr</span>(<span style="color: #825900;">$output</span>, <span style="color: #0000ff;">0</span>, <span style="color: #0000ff;">100</span>);
Â Â Â Â Â Â Â Â <span style="color: #825900;">$output</span> .= <span style="color: #825900;">$ending</span>;

Â Â Â Â Â Â Â Â <span style="color: #881350;">return</span> <span style="color: #825900;">$output</span>;
Â Â Â Â }

}

<span style="color: #dd0000;">?&gt;</span></pre>
<h3>5.Building the Controller</h3>
<hr />
<p>Now go to <strong>system/application/controllers</strong> and create a new file. Name this file as <strong>search.php</strong>. After create the file, the first thing we must do with this file is create a constructor which loads the model of search. Our search model is <strong>sear_model</strong>that we already created above.</p>
<pre class="php"><span style="color: #881350;">class</span> Search <span style="color: #881350;">extends</span> Controller {

Â Â Â Â <span style="color: #881350;">function</span> <span style="color: #003369;">Search</span>()
Â Â Â Â {
Â Â Â Â Â Â Â Â <span style="color: #881350;">parent</span>::<span style="color: #003369;">Controller</span>();

Â Â Â Â Â Â Â Â <span style="color: #825900;">$this</span>-&gt;load-&gt;<span style="color: #003369;">model</span>(<span style="color: #eb7300;">'search_model'</span>);

Â Â Â Â }</pre>
<p>Here is the most important part for our search. Because we will create 2 search methods which are default HTTP request and ajax request; therefore, we have to create a function that identifies each other request is.</p>
<p>The function below is used for knowing that is an ajax request.</p>
<pre class="php">Â Â Â Â <span style="color: #881350;">function</span> <span style="color: #003369;">isAjax</span>() {
Â Â Â Â Â Â Â Â <span style="color: #881350;">return</span>(<span style="color: #003369;">isset</span>(<span style="color: #825900;">$_SERVER</span>[<span style="color: #eb7300;">'HTTP_X_REQUESTED_WITH'</span>]) &amp;&amp; <span style="color: #825900;">$_SERVER</span>[<span style="color: #eb7300;">'HTTP_X_REQUESTED_WITH'</span>]==<span style="color: #eb7300;">"XMLHttpRequest"</span>);
Â Â Â Â }</pre>
<p>You can add this function to your <strong>search_model</strong> then call it by:</p>
<pre class="php"><span style="color: #825900;">$this</span>-&gt;search_model-&gt;<span style="color: #003369;">isAjax</span>()</pre>
<p>But it&#8217;s in here is fine.</p>
<p>By default, <strong>GET</strong> data is simply disallowed by CodeIgniter since the system utilizes URI segments rather than traditional URL query strings. The following line will make GET supported.</p>
<pre class="php">Â Â Â Â <span style="color: #881350;">function</span> <span style="color: #003369;">index</span>()

Â Â Â Â { Â Â Â Â Â Â 

Â Â Â Â Â Â Â Â <span style="color: #661aa9;">parse_str</span>(<span style="color: #825900;">$_SERVER</span>[<span style="color: #eb7300;">'QUERY_STRING'</span>],<span style="color: #825900;">$_GET</span>);</pre>
<p>Put this line into your <strong>index()</strong> method. Now, continue working with the <strong>index()</strong> by separating each method request. We will process the default HTTP Request first. For displaying, we need 4 files. Actually, we just want to create 3 files: <strong>search.php</strong>, <strong>search_form.php</strong> and <strong>search_result.php</strong>. However, we need to separate the <strong>search.php</strong> into <strong>search_header.php</strong> and <strong>search_footer.php</strong> to make it easier to edit later.</p>
<ul class="mylist code">
<li><strong>search_header.php</strong>: Header HTML goes here</li>
<li><strong>search_form.php</strong>: Form search</li>
<li><strong>search_result.php</strong>: Even when it has results or not, you need to display here</li>
<li><strong>search_footer.php</strong>: HTML for footer goes here</li>
</ul>
<p>We&#8217;re gonna make a default search page when have no request <em>(It means no one submits search query)</em>. It&#8217;ll load the default view and pass variable <strong>search_term</strong> to search form. It&#8217;s anÂ <strong>empty value</strong>, but without it, your form will get error.</p>
<pre class="php">Â Â Â Â <span style="color: #881350;">function</span> <span style="color: #003369;">index</span>()
Â Â Â Â { Â Â Â Â Â Â 

Â Â Â Â Â Â Â Â <span style="color: #661aa9;">parse_str</span>(<span style="color: #825900;">$_SERVER</span>[<span style="color: #eb7300;">'QUERY_STRING'</span>],<span style="color: #825900;">$_GET</span>);

Â Â Â Â Â Â Â Â <span style="color: #881350;">if</span>(<span style="color: #825900;">$this</span>-&gt;<span style="color: #003369;">isAjax</span>()) {

Â Â Â Â Â Â Â Â Â Â Â Â <em><span style="color: #236e25;">// Calm down, I'll do it later !!!! </span></em>
Â Â Â Â Â Â Â Â } <span style="color: #881350;">else</span> {

Â Â Â Â Â Â Â Â Â Â Â Â <span style="color: #881350;">if</span>(!<span style="color: #003369;">empty</span>(<span style="color: #825900;">$_GET</span>[<span style="color: #eb7300;">'Query'</span>])) {

Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â ...........

Â Â Â Â Â Â Â Â Â Â Â Â } <span style="color: #881350;">else</span> {

Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â <span style="color: #825900;">$data</span>[<span style="color: #eb7300;">'search_term'</span>]=<span style="color: #eb7300;">""</span>;

Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â <em><span style="color: #236e25;">// load the default view </span></em>Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â <span style="color: #825900;">$this</span>-&gt;load-&gt;<span style="color: #003369;">view</span>(<span style="color: #eb7300;">'search_header'</span>);
Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â <span style="color: #825900;">$this</span>-&gt;load-&gt;<span style="color: #003369;">view</span>(<span style="color: #eb7300;">'search_form'</span>, <span style="color: #825900;">$data</span>);
Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â <span style="color: #825900;">$this</span>-&gt;load-&gt;<span style="color: #003369;">view</span>(<span style="color: #eb7300;">'search_footer'</span>);

Â Â Â Â Â Â Â Â Â Â Â Â }
Â Â Â Â Â Â Â Â }
Â Â Â Â }</pre>
<p>We don&#8217;t need to display the results in the default search page because it doesn&#8217;t have any result, right?<br />
Inside the condition block when it has <strong>GET</strong> value of query, we will process the default HTTP request method here.</p>
<pre class="php">Â Â Â Â <span style="color: #825900;">$query</span> = <span style="color: #661aa9;">trim</span>(<span style="color: #825900;">$_GET</span>[<span style="color: #eb7300;">'query'</span>]);
Â Â Â Â <span style="color: #825900;">$clean_query</span>= <span style="color: #661aa9;">mysql_real_escape_string</span>(<span style="color: #825900;">$query</span>);

Â Â Â Â <span style="color: #825900;">$post_per_page</span>=<span style="color: #0000ff;">5</span>;
Â Â Â Â <span style="color: #825900;">$current_page</span>=<span style="color: #0000ff;">1</span>;

Â Â Â Â <span style="color: #881350;">if</span>(!<span style="color: #003369;">empty</span>(<span style="color: #825900;">$_GET</span>[<span style="color: #eb7300;">'page'</span>])) {
Â Â Â Â Â Â Â Â <span style="color: #825900;">$current_page</span>=<span style="color: #661aa9;">intval</span>(<span style="color: #661aa9;">trim</span>(<span style="color: #825900;">$_GET</span>[<span style="color: #eb7300;">'page'</span>]));
Â Â Â Â }

Â Â Â Â <span style="color: #825900;">$totalrows</span>=<span style="color: #825900;">$this</span>-&gt;search_model-&gt;<span style="color: #003369;">get_numrows</span>(<span style="color: #825900;">$clean_query</span>);

Â Â Â Â <span style="color: #825900;">$totalpages</span>=<span style="color: #661aa9;">ceil</span>(<span style="color: #825900;">$totalrows</span>/<span style="color: #825900;">$post_per_page</span>);

Â Â Â Â <span style="color: #825900;">$posts</span> = <span style="color: #825900;">$this</span>-&gt;search_model-&gt;<span style="color: #003369;">get_posts</span>(<span style="color: #825900;">$clean_query</span>, <span style="color: #825900;">$post_per_page</span>, <span style="color: #825900;">$current_page</span>);

Â Â Â Â <span style="color: #825900;">$has_next</span>=<span style="color: #881350;">true</span>;
Â Â Â Â <span style="color: #881350;">if</span>(<span style="color: #825900;">$current_page</span>==<span style="color: #825900;">$totalpages</span>) {
Â Â Â Â Â Â Â Â <span style="color: #825900;">$has_next</span>=<span style="color: #881350;">false</span>;
Â Â Â Â }

Â Â Â Â <span style="color: #825900;">$has_prev</span>=<span style="color: #881350;">true</span>;
Â Â Â Â <span style="color: #881350;">if</span>(<span style="color: #825900;">$current_page</span>&lt;=<span style="color: #0000ff;">1</span>) {
Â Â Â Â Â Â Â Â <span style="color: #825900;">$has_prev</span>=<span style="color: #881350;">false</span>;
Â Â Â Â }</pre>
<p>You maybe knew the easiest way to do pagination in CodeIginter suchs by using Pagination class, take a look at the <a href="http://codeigniter.com/user_guide/libraries/pagination.html">CI Pagination Guide</a>. However, it&#8217;s not as they said. It&#8217;s hard to customize, you need to write another pagination class to fit your needs. That&#8217;s why I don&#8217;t like it. The following code to paginate the result is just a simple code. Because we&#8217;ve talked about that over forum to forum. Please change it to your one pagination. Thank you!</p>
<p>We are passing all variables to the $data array to display it in view.</p>
<pre class="php">Â Â Â Â <span style="color: #825900;">$data</span>[<span style="color: #eb7300;">'search_term'</span>]=<span style="color: #825900;">$clean_query</span>;
Â Â Â Â <span style="color: #825900;">$data</span>[<span style="color: #eb7300;">'total_posts'</span>]=<span style="color: #825900;">$totalrows</span>;
Â Â Â Â <span style="color: #825900;">$data</span>[<span style="color: #eb7300;">'current_page'</span>]=<span style="color: #825900;">$current_page</span>;
Â Â Â Â <span style="color: #825900;">$data</span>[<span style="color: #eb7300;">'next_page'</span>]=<span style="color: #825900;">$current_page</span>+<span style="color: #0000ff;">1</span>;

Â Â Â Â <span style="color: #825900;">$data</span>[<span style="color: #eb7300;">'has_next'</span>]=<span style="color: #825900;">$has_next</span>;
Â Â Â Â <span style="color: #825900;">$data</span>[<span style="color: #eb7300;">'has_prev'</span>]=<span style="color: #825900;">$has_prev</span>;

Â Â Â Â <span style="color: #825900;">$data</span>[<span style="color: #eb7300;">'prev_page'</span>]=<span style="color: #825900;">$current_page</span>-<span style="color: #0000ff;">1</span>;
Â Â Â Â <span style="color: #825900;">$data</span>[<span style="color: #eb7300;">'total_page'</span>]=<span style="color: #825900;">$totalpages</span>;
Â Â Â Â <span style="color: #825900;">$data</span>[<span style="color: #eb7300;">'posts'</span>]=<span style="color: #825900;">$posts</span>;

Â Â Â Â <em><span style="color: #236e25;">// load the default view </span></em>Â Â Â Â <span style="color: #825900;">$this</span>-&gt;load-&gt;<span style="color: #003369;">view</span>(<span style="color: #eb7300;">'search_header'</span>);
Â Â Â Â <span style="color: #825900;">$this</span>-&gt;load-&gt;<span style="color: #003369;">view</span>(<span style="color: #eb7300;">'search_form'</span>, <span style="color: #825900;">$data</span>);
Â Â Â Â <span style="color: #825900;">$this</span>-&gt;load-&gt;<span style="color: #003369;">view</span>(<span style="color: #eb7300;">'search_result'</span>, <span style="color: #825900;">$data</span>);
Â Â Â Â <span style="color: #825900;">$this</span>-&gt;load-&gt;<span style="color: #003369;">view</span>(<span style="color: #eb7300;">'search_footer'</span>);</pre>
<p>Now, we are on the topic of this entry. Build a ajax live search with jQuery and CodeIgniter. The code is similar to default HTTP request but may varies a little bit which is the results. The results in ajax respond are different. In this tutorial, I will make ajax response in <strong>JSON</strong> format.</p>
<p>A little bit thing but helpful to understand how is this form gonna get the result. It&#8217;s JSON format. The well-formatted JSON in this tutorial is:</p>
<pre class="json">Â Â Â Â {<span style="color: #760f15;">"results"</span>:[
Â Â Â Â Â Â Â Â {
Â Â Â Â Â Â Â Â Â Â Â Â <span style="color: #760f15;">"postid"</span>:<span style="color: #0000ff;">30</span>,
Â Â Â Â Â Â Â Â Â Â Â Â <span style="color: #760f15;">"summary"</span>:<span style="color: #760f15;">"Some summary of this post go here ..."</span>,
Â Â Â Â Â Â Â Â Â Â Â Â <span style="color: #760f15;">"title"</span>:<span style="color: #760f15;">"This is a title"</span>,
Â Â Â Â Â Â Â Â Â Â Â Â <span style="color: #760f15;">"url"</span>:<span style="color: #760f15;">"http://link-to-this-post"</span>,
Â Â Â Â Â Â Â Â },
Â Â Â Â Â Â Â Â {
Â Â Â Â Â Â Â Â Â Â Â Â <span style="color: #760f15;">"postid"</span>:<span style="color: #0000ff;">294</span>,
Â Â Â Â Â Â Â Â Â Â Â Â <span style="color: #760f15;">"summary"</span>:<span style="color: #760f15;">"Some summary of this post go here ..."</span>,
Â Â Â Â Â Â Â Â Â Â Â Â <span style="color: #760f15;">"title"</span>:<span style="color: #760f15;">"This is a title"</span>,
Â Â Â Â Â Â Â Â Â Â Â Â <span style="color: #760f15;">"url"</span>:<span style="color: #760f15;">"http://link-to-this-post"</span>,
Â Â Â Â Â Â Â Â },
Â Â Â Â Â Â Â Â {
Â Â Â Â Â Â Â Â Â Â Â Â <span style="color: #760f15;">"postid"</span>:<span style="color: #0000ff;">384</span>,
Â Â Â Â Â Â Â Â Â Â Â Â <span style="color: #760f15;">"summary"</span>:<span style="color: #760f15;">"Some summary of this post go here ..."</span>,
Â Â Â Â Â Â Â Â Â Â Â Â <span style="color: #760f15;">"title"</span>:<span style="color: #760f15;">"This is a title"</span>,
Â Â Â Â Â Â Â Â Â Â Â Â <span style="color: #760f15;">"url"</span>:<span style="color: #760f15;">"http://link-to-this-post"</span>,
Â Â Â Â Â Â Â Â },
Â Â Â Â Â Â Â ],
Â Â Â Â Â Â Â <span style="color: #760f15;">"paging"</span>:{
Â Â Â Â Â Â Â Â Â Â Â Â <span style="color: #760f15;">"start_idx"</span>:<span style="color: #0000ff;">1</span>,
Â Â Â Â Â Â Â Â Â Â Â Â <span style="color: #760f15;">"end_idx"</span>:<span style="color: #0000ff;">5</span>,
Â Â Â Â Â Â Â Â Â Â Â Â <span style="color: #760f15;">"total"</span>:<span style="color: #0000ff;">6</span>,
Â Â Â Â Â Â Â Â Â Â Â Â <span style="color: #760f15;">"current"</span>:<span style="color: #760f15;">"1"</span>,
Â Â Â Â Â Â Â Â Â Â Â Â <span style="color: #760f15;">"pages"</span>:<span style="color: #0000ff;">2</span>,
Â Â Â Â Â Â Â Â Â Â Â Â <span style="color: #760f15;">"has_next"</span>:<span style="color: #881350;">true</span>,
Â Â Â Â Â Â Â Â Â Â Â Â <span style="color: #760f15;">"has_prev"</span>:<span style="color: #881350;">false</span>,
Â Â Â Â Â Â Â }
Â Â Â Â }</pre>
<p>The values in array paging is used for pagination. The <strong>start_idx</strong> and <strong>end_idx</strong> are the values that shows us the range in total of posts displayed in current page. The <strong>has_next</strong> and <strong>has_prev</strong> are boolean values that the next and previous button depend on. Here below is the ajax part:</p>
<pre class="php">Â Â Â Â <span style="color: #881350;">function</span> <span style="color: #003369;">index</span>()
Â Â Â Â { Â Â Â Â Â Â 

Â Â Â Â Â Â Â Â <span style="color: #661aa9;">parse_str</span>(<span style="color: #825900;">$_SERVER</span>[<span style="color: #eb7300;">'QUERY_STRING'</span>],<span style="color: #825900;">$_GET</span>);

Â Â Â Â Â Â Â Â <span style="color: #881350;">if</span>(<span style="color: #825900;">$this</span>-&gt;<span style="color: #003369;">isAjax</span>()) {

Â Â Â Â Â Â Â Â Â Â Â Â <span style="color: #825900;">$query</span> = <span style="color: #661aa9;">trim</span>(<span style="color: #825900;">$_GET</span>[<span style="color: #eb7300;">'query'</span>]);
Â Â Â Â Â Â Â Â Â Â Â Â <span style="color: #825900;">$clean_query</span>= <span style="color: #661aa9;">mysql_real_escape_string</span>(<span style="color: #825900;">$query</span>);

Â Â Â Â Â Â Â Â Â Â Â Â <span style="color: #825900;">$post_per_page</span>=<span style="color: #0000ff;">5</span>;
Â Â Â Â Â Â Â Â Â Â Â Â <span style="color: #825900;">$current_page</span>=<span style="color: #0000ff;">1</span>;

Â Â Â Â Â Â Â Â Â Â Â Â <span style="color: #881350;">if</span>(!<span style="color: #003369;">empty</span>(<span style="color: #825900;">$_GET</span>[<span style="color: #eb7300;">'page'</span>])) {
Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â <span style="color: #825900;">$current_page</span>=<span style="color: #661aa9;">intval</span>(<span style="color: #661aa9;">trim</span>(<span style="color: #825900;">$_GET</span>[<span style="color: #eb7300;">'page'</span>]));
Â Â Â Â Â Â Â Â Â Â Â Â }

Â Â Â Â Â Â Â Â Â Â Â Â <span style="color: #825900;">$clean_query</span>= <span style="color: #661aa9;">mysql_real_escape_string</span>(<span style="color: #825900;">$query</span>); Â Â Â Â Â Â Â Â 

Â Â Â Â Â Â Â Â Â Â Â Â <span style="color: #825900;">$posts</span> = <span style="color: #825900;">$this</span>-&gt;search_model-&gt;<span style="color: #003369;">get_posts</span>(<span style="color: #825900;">$clean_query</span>, <span style="color: #825900;">$post_per_page</span>, <span style="color: #825900;">$current_page</span>);

Â Â Â Â Â Â Â Â Â Â Â Â <span style="color: #825900;">$totalrows</span>=<span style="color: #825900;">$this</span>-&gt;search_model-&gt;<span style="color: #003369;">get_numrows</span>(<span style="color: #825900;">$clean_query</span>);

Â Â Â Â Â Â Â Â Â Â Â Â <span style="color: #825900;">$totalpages</span>=<span style="color: #661aa9;">ceil</span>(<span style="color: #825900;">$totalrows</span>/<span style="color: #825900;">$post_per_page</span>);

Â Â Â Â Â Â Â Â Â Â Â Â <span style="color: #881350;">if</span>(<span style="color: #825900;">$current_page</span>==<span style="color: #0000ff;">1</span>) {
Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â <span style="color: #825900;">$start_idx</span>=<span style="color: #0000ff;">1</span>;
Â Â Â Â Â Â Â Â Â Â Â Â } <span style="color: #881350;">else</span> {
Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â <span style="color: #825900;">$start_idx</span>=(<span style="color: #825900;">$current_page</span>*<span style="color: #825900;">$post_per_page</span>)-<span style="color: #0000ff;">4</span>;
Â Â Â Â Â Â Â Â Â Â Â Â } Â Â 

Â Â Â Â Â Â Â Â Â Â Â Â <span style="color: #825900;">$end_idx</span>=<span style="color: #825900;">$start_idx</span>+<span style="color: #0000ff;">4</span>;
Â Â Â Â Â Â Â Â Â Â Â Â <span style="color: #881350;">if</span>(<span style="color: #825900;">$current_page</span>==<span style="color: #825900;">$totalpages</span>) {
Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â <span style="color: #825900;">$end_idx</span>=<span style="color: #825900;">$totalrows</span>;
Â Â Â Â Â Â Â Â Â Â Â Â }

Â Â Â Â Â Â Â Â Â Â Â Â <span style="color: #881350;">echo</span> <span style="color: #eb7300;">'{"results":['</span>;

Â Â Â Â Â Â Â Â Â Â Â Â <span style="color: #881350;">foreach</span>(<span style="color: #825900;">$posts</span> <span style="color: #881350;">as</span> <span style="color: #825900;">$post</span>) {

Â Â Â Â Â Â Â Â Â Â Â Â Â Â <span style="color: #881350;">echo</span> <span style="color: #eb7300;">'{ Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â "postid":'</span>.<span style="color: #825900;">$post</span>[<span style="color: #eb7300;">'ID'</span>].<span style="color: #eb7300;">', Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â "summary":"'</span>.<span style="color: #825900;">$this</span>-&gt;search_model-&gt;<span style="color: #003369;">highlightWords</span>(<span style="color: #825900;">$this</span>-&gt;search_model-&gt;<span style="color: #003369;">cleanHTML</span>(<span style="color: #825900;">$post</span>[<span style="color: #eb7300;">'post_content'</span>]), <span style="color: #825900;">$clean_query</span>, <span style="color: #881350;">true</span>).<span style="color: #eb7300;">'", Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â "title":"'</span>.<span style="color: #825900;">$this</span>-&gt;search_model-&gt;<span style="color: #003369;">highlightWords</span>(<span style="color: #825900;">$post</span>[<span style="color: #eb7300;">'post_title'</span>], <span style="color: #825900;">$clean_query</span>, <span style="color: #881350;">true</span>).<span style="color: #eb7300;">'", Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â "url":"'</span>.<span style="color: #825900;">$post</span>[<span style="color: #eb7300;">'guid'</span>].<span style="color: #eb7300;">'", Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â },'</span>;
Â Â Â Â Â Â Â Â Â Â Â Â }

Â Â Â Â Â Â Â Â Â Â Â Â <span style="color: #881350;">echo</span> <span style="color: #eb7300;">'],'</span>;

Â Â Â Â Â Â Â Â Â Â Â Â <span style="color: #825900;">$has_next</span>=<span style="color: #eb7300;">'false'</span>;
Â Â Â Â Â Â Â Â Â Â Â Â <span style="color: #825900;">$has_prev</span>=<span style="color: #eb7300;">'false'</span>;
Â Â Â Â Â Â Â Â Â Â Â Â <span style="color: #881350;">if</span>(<span style="color: #825900;">$current_page</span>&lt;<span style="color: #825900;">$totalpages</span>) {
Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â <span style="color: #825900;">$has_next</span>=<span style="color: #eb7300;">'true'</span>;
Â Â Â Â Â Â Â Â Â Â Â Â }

Â Â Â Â Â Â Â Â Â Â Â Â <span style="color: #881350;">if</span>(<span style="color: #825900;">$current_page</span>&gt;<span style="color: #0000ff;">1</span>)
Â Â Â Â Â Â Â Â Â Â Â Â {
Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â <span style="color: #825900;">$has_prev</span>=<span style="color: #eb7300;">'true'</span>;
Â Â Â Â Â Â Â Â Â Â Â Â }

Â Â Â Â Â Â Â Â Â Â Â Â <span style="color: #881350;">echo</span> <span style="color: #eb7300;">' Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â "paging":{ Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â "start_idx":'</span>.<span style="color: #825900;">$start_idx</span>.<span style="color: #eb7300;">', Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â "end_idx":'</span>.<span style="color: #825900;">$end_idx</span>.<span style="color: #eb7300;">', Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â "total":'</span>.<span style="color: #825900;">$totalrows</span>.<span style="color: #eb7300;">', Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â "current":"'</span>.<span style="color: #825900;">$current_page</span>.<span style="color: #eb7300;">'", Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â "pages":'</span>.<span style="color: #825900;">$totalpages</span>.<span style="color: #eb7300;">', Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â "has_next":'</span>.<span style="color: #825900;">$has_next</span>.<span style="color: #eb7300;">', Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â "has_prev":'</span>.<span style="color: #825900;">$has_prev</span>.<span style="color: #eb7300;">', Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â } Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â } Â Â Â Â Â Â Â Â Â Â Â Â '</span>; Â 

Â Â Â Â Â Â Â Â } <span style="color: #881350;">else</span> {

Â Â Â Â Â Â Â Â Â Â Â Â <em><span style="color: #236e25;">// blah blah blah blah blah blah </span></em>
Â Â Â Â Â Â Â Â }
Â Â Â Â }</pre>
<p>When you display the results as JSON format, you need to make sure your data is cleaned up. Because some HTML tags will break the JSON format, then your jQuery code can not read it. That&#8217;s why I created a function to clean up HTML format earlier.</p>
<h3>6. The View &#8211; Display your works</h3>
<hr />
<p>100% percent of people who are viewing the tut have already click to check the demo before reading, right? So, the demo you saw at the top of this entry is the result of all code below.</p>
<p>The <strong>search_header.php</strong> file: link to your css file and jQuery library, do some javascripts for the form, that&#8217;s all!</p>
<pre class="php"><em><span style="color: #236e25;">&lt;!DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;</span></em>
<span style="color: #881280;">&lt;html </span><span style="color: #994500;">xmlns</span><span style="color: #881280;">=</span><span style="color: #1a1aa6;">"http://www.w3.org/1999/xhtml"</span><span style="color: #881280;">&gt;&lt;head&gt;</span>

<span style="color: #881280;">&lt;meta </span><span style="color: #994500;">http-equiv</span><span style="color: #881280;">=</span><span style="color: #1a1aa6;">"Content-Type"</span><span style="color: #994500;">content</span><span style="color: #881280;">=</span><span style="color: #1a1aa6;">"text/html; charset=UTF-8"</span><span style="color: #881280;">&gt;</span>
<span style="color: #881280;">&lt;title&gt;</span>Search<span style="color: #881280;">&lt;/title&gt;</span>

<span style="color: #881280;">&lt;link </span><span style="color: #994500;">rel</span><span style="color: #881280;">=</span><span style="color: #1a1aa6;">"stylesheet"</span><span style="color: #994500;">media</span><span style="color: #881280;">=</span><span style="color: #1a1aa6;">"screen"</span><span style="color: #994500;">href</span><span style="color: #881280;">=</span><span style="color: #1a1aa6;">"</span><span style="color: #dd0000;">&lt;?php</span> <span style="color: #881350;">echo</span> <span style="color: #003369;">base_url</span>(); <span style="color: #dd0000;">?&gt;</span><span style="color: #1a1aa6;">css/reset.css"</span><span style="color: #881280;">&gt;</span>
<span style="color: #881280;">&lt;link </span><span style="color: #994500;">rel</span><span style="color: #881280;">=</span><span style="color: #1a1aa6;">"stylesheet"</span><span style="color: #994500;">media</span><span style="color: #881280;">=</span><span style="color: #1a1aa6;">"screen"</span><span style="color: #994500;">href</span><span style="color: #881280;">=</span><span style="color: #1a1aa6;">"</span><span style="color: #dd0000;">&lt;?php</span> <span style="color: #881350;">echo</span> <span style="color: #003369;">base_url</span>(); <span style="color: #dd0000;">?&gt;</span><span style="color: #1a1aa6;">css/global.css"</span><span style="color: #881280;">&gt;</span>

<span style="color: #881280;">&lt;script </span><span style="color: #994500;">type</span><span style="color: #881280;">=</span><span style="color: #1a1aa6;">"text/javascript"</span><span style="color: #994500;">src</span><span style="color: #881280;">=</span><span style="color: #1a1aa6;">"</span><span style="color: #dd0000;">&lt;?php</span> <span style="color: #881350;">echo</span> <span style="color: #003369;">base_url</span>(); <span style="color: #dd0000;">?&gt;</span><span style="color: #1a1aa6;">js/jquery.js"</span><span style="color: #881280;">&gt;&lt;/script&gt;</span>
<span style="color: #881280;">&lt;script </span><span style="color: #994500;">type</span><span style="color: #881280;">=</span><span style="color: #1a1aa6;">"text/javascript"</span><span style="color: #994500;">src</span><span style="color: #881280;">=</span><span style="color: #1a1aa6;">"</span><span style="color: #dd0000;">&lt;?php</span> <span style="color: #881350;">echo</span> <span style="color: #003369;">base_url</span>(); <span style="color: #dd0000;">?&gt;</span><span style="color: #1a1aa6;">js/search_pod.js"</span><span style="color: #881280;">&gt;&lt;/script&gt;</span>

<span style="color: #881280;">&lt;/head&gt;</span>
<span style="color: #881280;">&lt;body&gt;</span>

<em><span style="color: #236e25;">&lt;!-- Begin Content --&gt;</span></em>
<span style="color: #881280;">&lt;div </span><span style="color: #994500;">id</span><span style="color: #881280;">=</span><span style="color: #1a1aa6;">"content"</span><span style="color: #881280;">&gt;</span>

Â Â Â Â <span style="color: #881280;">&lt;div </span><span style="color: #994500;">class</span><span style="color: #881280;">=</span><span style="color: #1a1aa6;">"maincontent"</span><span style="color: #881280;">&gt;</span>

Â Â Â Â Â Â Â <em><span style="color: #236e25;">&lt;!-- Begin Search --&gt;</span></em></pre>
<p>All the javascript and css file will be located at root folder base on <strong>base_url</strong> that we declared at above. It&#8217;s not inside folder <strong>system/application/view</strong>.</p>
<p>Next, create <strong>search_footer.php</strong> file in <strong>system/application/view</strong> with content as:</p>
<pre class="php">Â Â Â Â Â Â Â <em><span style="color: #236e25;">&lt;!-- End Search --&gt;</span></em>

Â Â Â Â <span style="color: #881280;">&lt;/div&gt;</span>

Â Â Â Â <span style="color: #881280;">&lt;div </span><span style="color: #994500;">class</span><span style="color: #881280;">=</span><span style="color: #1a1aa6;">"clearfix"</span><span style="color: #881280;">&gt;&amp;nbsp;&lt;/div&gt;</span>

<span style="color: #881280;">&lt;/div&gt;</span>
<em><span style="color: #236e25;">&lt;!-- End Content --&gt;</span></em>

<span style="color: #881280;">&lt;/body&gt;&lt;/html&gt;</span></pre>
<p>The <strong>search_form.php</strong> contains the search form and the panel that data returned will be displayed in. Create a file name <strong>search_form.php</strong> in the same folder which contains following code below:</p>
<pre class="php">Â Â <em><span style="color: #236e25;">&lt;!-- Start SearchForm --&gt;</span></em>
Â Â <span style="color: #881280;">&lt;div </span><span style="color: #994500;">id</span><span style="color: #881280;">=</span><span style="color: #1a1aa6;">"search"</span><span style="color: #881280;">&gt;</span>
Â Â Â Â Â <span style="color: #881280;">&lt;form </span><span style="color: #994500;">id</span><span style="color: #881280;">=</span><span style="color: #1a1aa6;">"search_form"</span><span style="color: #994500;">method</span><span style="color: #881280;">=</span><span style="color: #1a1aa6;">"GET"</span><span style="color: #994500;">action</span><span style="color: #881280;">=</span><span style="color: #1a1aa6;">"</span><span style="color: #dd0000;">&lt;?php</span> <span style="color: #881350;">echo</span> <span style="color: #003369;">base_url</span>(); <span style="color: #dd0000;">?&gt;</span><span style="color: #1a1aa6;">search"</span><span style="color: #881280;">&gt;</span>
Â Â Â Â Â Â Â Â Â <span style="color: #881280;">&lt;fieldset&gt;</span>
Â Â Â Â Â Â Â Â Â Â Â Â <span style="color: #881280;">&lt;img </span><span style="color: #994500;">src</span><span style="color: #881280;">=</span><span style="color: #1a1aa6;">"</span><span style="color: #dd0000;">&lt;?php</span> <span style="color: #881350;">echo</span> <span style="color: #003369;">base_url</span>(); <span style="color: #dd0000;">?&gt;</span><span style="color: #1a1aa6;">/images/ajax-loader.gif"</span><span style="color: #881280;"> Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â </span><span style="color: #994500;">id</span><span style="color: #881280;">=</span><span style="color: #1a1aa6;">"ajaxloader"</span><span style="color: #994500;">alt</span><span style="color: #881280;">=</span><span style="color: #1a1aa6;">"Loading Results..."</span><span style="color: #994500;">style</span><span style="color: #881280;">=</span><span style="color: #1a1aa6;">"display: none;"</span><span style="color: #881280;">&gt;</span>
Â Â Â Â Â Â Â Â Â Â Â Â <span style="color: #881280;">&lt;input </span><span style="color: #994500;">name</span><span style="color: #881280;">=</span><span style="color: #1a1aa6;">"query"</span><span style="color: #994500;">id</span><span style="color: #881280;">=</span><span style="color: #1a1aa6;">"search_val"</span><span style="color: #994500;">value</span><span style="color: #881280;">=</span><span style="color: #1a1aa6;">"</span><span style="color: #dd0000;">&lt;?php</span> <span style="color: #661aa9;">printf</span>(<span style="color: #825900;">$search_term</span>); <span style="color: #dd0000;">?&gt;</span><span style="color: #1a1aa6;">"</span><span style="color: #881280;"> Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â </span><span style="color: #994500;">tabindex</span><span style="color: #881280;">=</span><span style="color: #1a1aa6;">"1"</span><span style="color: #994500;">autocomplete</span><span style="color: #881280;">=</span><span style="color: #1a1aa6;">"off"</span><span style="color: #994500;">type</span><span style="color: #881280;">=</span><span style="color: #1a1aa6;">"text"</span><span style="color: #881280;">&gt;</span>
Â Â Â Â Â Â Â Â Â <span style="color: #881280;">&lt;/fieldset&gt;</span>

Â Â Â Â Â Â Â Â Â <em><span style="color: #236e25;">&lt;!-- Begin Livesearch Panel --&gt;</span></em>
Â Â Â Â Â Â Â Â Â <span style="color: #881280;">&lt;div </span><span style="color: #994500;">id</span><span style="color: #881280;">=</span><span style="color: #1a1aa6;">"livesearch"</span><span style="color: #994500;">style</span><span style="color: #881280;">=</span><span style="color: #1a1aa6;">"display: none;"</span><span style="color: #881280;">&gt;</span>
Â Â Â Â Â Â Â Â Â Â Â Â <span style="color: #881280;">&lt;div </span><span style="color: #994500;">class</span><span style="color: #881280;">=</span><span style="color: #1a1aa6;">"loader"</span><span style="color: #994500;">id</span><span style="color: #881280;">=</span><span style="color: #1a1aa6;">"loader_div"</span><span style="color: #881280;">&gt;&lt;p&gt;</span>Loading Results...<span style="color: #881280;">&lt;/p&gt;&lt;/div&gt;</span>
Â Â Â Â Â Â Â Â Â Â Â Â <span style="color: #881280;">&lt;div </span><span style="color: #994500;">class</span><span style="color: #881280;">=</span><span style="color: #1a1aa6;">"livesearch_body"</span><span style="color: #881280;">&gt;</span>
Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â <span style="color: #881280;">&lt;dl </span><span style="color: #994500;">class</span><span style="color: #881280;">=</span><span style="color: #1a1aa6;">"livesearch_results"</span><span style="color: #881280;">&gt;</span>
Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â <span style="color: #881280;">&lt;dt&gt;&lt;span&gt;</span>Live Search Results<span style="color: #881280;">&lt;/span&gt;</span>
Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â <span style="color: #881280;">&lt;div </span><span style="color: #994500;">class</span><span style="color: #881280;">=</span><span style="color: #1a1aa6;">"livesearch_subnav"</span><span style="color: #881280;">&gt;</span>
Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â <span style="color: #881280;">&lt;a </span><span style="color: #994500;">href</span><span style="color: #881280;">=</span><span style="color: #1a1aa6;">"#"</span><span style="color: #994500;">class</span><span style="color: #881280;">=</span><span style="color: #1a1aa6;">"livesearch_close"</span><span style="color: #994500;">id</span><span style="color: #881280;">=</span><span style="color: #1a1aa6;">"livesearch_result_close_link"</span><span style="color: #881280;">&gt;</span>Close LiveSearch<span style="color: #881280;">&lt;/a&gt;</span>
Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â <span style="color: #881280;">&lt;div </span><span style="color: #994500;">class</span><span style="color: #881280;">=</span><span style="color: #1a1aa6;">"clearfix"</span><span style="color: #881280;">&gt;&amp;nbsp;&lt;/div&gt;</span>
Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â <span style="color: #881280;">&lt;/div&gt;</span>
Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â <span style="color: #881280;">&lt;div </span><span style="color: #994500;">class</span><span style="color: #881280;">=</span><span style="color: #1a1aa6;">"clearfix"</span><span style="color: #994500;">id</span><span style="color: #881280;">=</span><span style="color: #1a1aa6;">"subnavclear"</span><span style="color: #881280;">&gt;&amp;nbsp;&lt;/div&gt;</span>
Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â <span style="color: #881280;">&lt;/dt&gt;</span>
Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â <span style="color: #881280;">&lt;/dl&gt;</span>
Â Â Â Â Â Â Â Â Â Â Â Â Â <span style="color: #881280;">&lt;/div&gt;</span>
Â Â Â Â Â Â Â Â Â Â Â Â Â <span style="color: #881280;">&lt;div </span><span style="color: #994500;">class</span><span style="color: #881280;">=</span><span style="color: #1a1aa6;">"livesearch_nav"</span><span style="color: #881280;">&gt;</span>
Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â <span style="color: #881280;">&lt;div </span><span style="color: #994500;">class</span><span style="color: #881280;">=</span><span style="color: #1a1aa6;">"livesearch_navbody"</span><span style="color: #881280;">&gt;&lt;/div&gt;</span>
Â Â Â Â Â Â Â Â Â Â Â Â Â <span style="color: #881280;">&lt;/div&gt;</span>
Â Â Â Â Â Â Â Â Â <span style="color: #881280;">&lt;/div&gt;</span>
Â Â Â Â Â Â Â Â Â Â <em><span style="color: #236e25;">&lt;!-- End Livesearch Panel --&gt;</span></em>

Â Â Â Â Â Â <span style="color: #881280;">&lt;/form&gt;</span>
Â Â <span style="color: #881280;">&lt;/div&gt;</span>
Â Â <em><span style="color: #236e25;">&lt;!-- End SearchForm --&gt;</span></em></pre>
<p>The result after users input something is like image below:</p>
<div class="bigimage"><img class="aligncenter size-full wp-image-1077" title="CodeIgniter and jQuery Live Search - Slide Down" src="http://aext.net/wp-contents/uploads/2009/10/CodeIgniter-and-jQuery-Live-Search-Slide-Down.jpg" alt="CodeIgniter and jQuery Live Search - Slide Down" width="500" /></div>
<p>Your <strong>search_result.php</strong>:</p>
<pre class="php">Â Â Â Â Â Â Â Â <em><span style="color: #236e25;">&lt;!-- Begin search result --&gt;</span></em>
Â Â Â Â Â Â Â Â <span style="color: #881280;">&lt;dl </span><span style="color: #994500;">id</span><span style="color: #881280;">=</span><span style="color: #1a1aa6;">"search_results"</span><span style="color: #994500;">class</span><span style="color: #881280;">=</span><span style="color: #1a1aa6;">"results_browse"</span><span style="color: #881280;">&gt;</span>

Â Â Â Â Â Â Â Â <span style="color: #dd0000;">&lt;?php</span> <span style="color: #881350;">if</span>(<span style="color: #661aa9;">count</span>(<span style="color: #825900;">$posts</span>)&gt;<span style="color: #0000ff;">0</span>): <span style="color: #dd0000;">?&gt;</span>
Â Â Â Â Â Â Â Â <span style="color: #881280;">&lt;dt&gt;</span>

Â Â Â Â Â Â Â Â Â Â Â Â <span style="color: #881280;">&lt;div </span><span style="color: #994500;">class</span><span style="color: #881280;">=</span><span style="color: #1a1aa6;">"results_paging"</span><span style="color: #881280;">&gt;</span>
Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â <span style="color: #881280;">&lt;a </span><span style="color: #994500;">class</span><span style="color: #881280;">=</span><span style="color: #1a1aa6;">"livesearch_next"</span><span style="color: #994500;">href</span><span style="color: #881280;">=</span><span style="color: #1a1aa6;">"</span><span style="color: #dd0000;">&lt;?php</span> <span style="color: #881350;">echo</span> <span style="color: #003369;">base_url</span>(); <span style="color: #dd0000;">?&gt;</span><span style="color: #1a1aa6;">search?query=</span><span style="color: #dd0000;">&lt;?php</span> <span style="color: #881350;">echo</span> <span style="color: #825900;">$search_term</span>; <span style="color: #dd0000;">?&gt;</span><span style="color: #1a1aa6;">&amp;page=</span><span style="color: #dd0000;">&lt;?php</span> <span style="color: #881350;">if</span>(!<span style="color: #825900;">$has_next</span>) <span style="color: #661aa9;">printf</span>(<span style="color: #825900;">$current_page</span>.<span style="color: #eb7300;">"#"</span>); <span style="color: #881350;">else</span> <span style="color: #661aa9;">printf</span>(<span style="color: #825900;">$next_page</span>); <span style="color: #dd0000;">?&gt;</span><span style="color: #1a1aa6;">"</span><span style="color: #994500;">title</span><span style="color: #881280;">=</span><span style="color: #1a1aa6;">"Next results page"</span><span style="color: #881280;">&gt;&lt;em&gt;</span>Next<span style="color: #881280;">&lt;/em&gt;&lt;/a&gt;</span>

Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â <em><span style="color: #236e25;">&lt;!-- list markup is IMPORTANT for ie6 compatibility (no whitespace) --&gt;</span></em>
Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â <span style="color: #881280;">&lt;ul </span><span style="color: #994500;">class</span><span style="color: #881280;">=</span><span style="color: #1a1aa6;">"pagelist"</span><span style="color: #881280;">&gt;</span>
Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â <span style="color: #dd0000;">&lt;?php</span> <span style="color: #881350;">for</span>(<span style="color: #825900;">$i</span>=<span style="color: #0000ff;">1</span>; <span style="color: #825900;">$i</span>&lt;=<span style="color: #825900;">$total_page</span>; <span style="color: #825900;">$i</span>++) {

Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â <span style="color: #881350;">if</span>(<span style="color: #825900;">$i</span>==<span style="color: #825900;">$current_page</span>):

Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â <span style="color: #dd0000;">?&gt;</span>

Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â <span style="color: #881280;">&lt;li&gt;&lt;a </span><span style="color: #994500;">href</span><span style="color: #881280;">=</span><span style="color: #1a1aa6;">"</span><span style="color: #dd0000;">&lt;?php</span> <span style="color: #881350;">echo</span> <span style="color: #003369;">base_url</span>(); <span style="color: #dd0000;">?&gt;</span><span style="color: #1a1aa6;">search?query=</span><span style="color: #dd0000;">&lt;?php</span> <span style="color: #661aa9;">printf</span>(<span style="color: #825900;">$search_term</span>); <span style="color: #dd0000;">?&gt;</span><span style="color: #1a1aa6;">&amp;page=</span><span style="color: #dd0000;">&lt;?php</span> <span style="color: #661aa9;">printf</span>(<span style="color: #825900;">$i</span>); <span style="color: #dd0000;">?&gt;</span><span style="color: #1a1aa6;">"</span><span style="color: #994500;">class</span><span style="color: #881280;">=</span><span style="color: #1a1aa6;">"active"</span><span style="color: #881280;">&gt;</span><span style="color: #dd0000;">&lt;?php</span> <span style="color: #661aa9;">printf</span>(<span style="color: #825900;">$i</span>); <span style="color: #dd0000;">?&gt;</span><span style="color: #881280;">&lt;/a&gt;&lt;/li&gt;</span>
Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â <span style="color: #dd0000;">&lt;?php</span> Â Â <span style="color: #881350;">else</span>: <span style="color: #dd0000;">?&gt;</span>

Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â <span style="color: #881280;">&lt;li&gt;&lt;a </span><span style="color: #994500;">href</span><span style="color: #881280;">=</span><span style="color: #1a1aa6;">"</span><span style="color: #dd0000;">&lt;?php</span> <span style="color: #881350;">echo</span> <span style="color: #003369;">base_url</span>(); <span style="color: #dd0000;">?&gt;</span><span style="color: #1a1aa6;">search?query=</span><span style="color: #dd0000;">&lt;?php</span> <span style="color: #661aa9;">printf</span>(<span style="color: #825900;">$search_term</span>); <span style="color: #dd0000;">?&gt;</span><span style="color: #1a1aa6;">&amp;page=</span><span style="color: #dd0000;">&lt;?php</span> <span style="color: #661aa9;">printf</span>(<span style="color: #825900;">$i</span>); <span style="color: #dd0000;">?&gt;</span><span style="color: #1a1aa6;">"</span><span style="color: #881280;">&gt;</span><span style="color: #dd0000;">&lt;?php</span> <span style="color: #661aa9;">printf</span>(<span style="color: #825900;">$i</span>); <span style="color: #dd0000;">?&gt;</span><span style="color: #881280;">&lt;/a&gt;&lt;/li&gt;</span>

Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â <span style="color: #dd0000;">&lt;?</span>

Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â <span style="color: #881350;">endif</span>;

Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â }

Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â <span style="color: #dd0000;">?&gt;</span>
Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â <span style="color: #881280;">&lt;/ul&gt;</span>

Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â <span style="color: #881280;">&lt;a </span><span style="color: #994500;">class</span><span style="color: #881280;">=</span><span style="color: #1a1aa6;">"livesearch_prev"</span><span style="color: #994500;">href</span><span style="color: #881280;">=</span><span style="color: #1a1aa6;">"</span><span style="color: #dd0000;">&lt;?php</span> <span style="color: #881350;">echo</span> <span style="color: #003369;">base_url</span>(); <span style="color: #dd0000;">?&gt;</span><span style="color: #1a1aa6;">search?query=</span><span style="color: #dd0000;">&lt;?php</span> <span style="color: #881350;">echo</span> <span style="color: #825900;">$search_term</span>; <span style="color: #dd0000;">?&gt;</span><span style="color: #1a1aa6;">&amp;page=</span><span style="color: #dd0000;">&lt;?php</span> <span style="color: #881350;">if</span>(!<span style="color: #825900;">$has_prev</span>) <span style="color: #661aa9;">printf</span>(<span style="color: #825900;">$current_page</span>.<span style="color: #eb7300;">"#"</span>); <span style="color: #881350;">else</span> <span style="color: #661aa9;">printf</span>(<span style="color: #825900;">$prev_page</span>); <span style="color: #dd0000;">?&gt;</span><span style="color: #1a1aa6;">"</span><span style="color: #994500;">title</span><span style="color: #881280;">=</span><span style="color: #1a1aa6;">"Previous results page"</span><span style="color: #881280;">&gt;&lt;em&gt;</span>Previous<span style="color: #881280;">&lt;/em&gt;&lt;/a&gt;</span>

Â Â Â Â Â Â Â Â Â Â Â Â <span style="color: #881280;">&lt;/div&gt;</span>
Â Â Â Â Â Â Â Â Â Â Â Â <span style="color: #881280;">&lt;span </span><span style="color: #994500;">class</span><span style="color: #881280;">=</span><span style="color: #1a1aa6;">"results_meta"</span><span style="color: #881280;">&gt;</span>There were <span style="color: #dd0000;">&lt;?php</span> <span style="color: #881350;">echo</span> <span style="color: #825900;">$total_posts</span>; <span style="color: #dd0000;">?&gt;</span> articles found for your query: - Page <span style="color: #dd0000;">&lt;?php</span> <span style="color: #881350;">echo</span> <span style="color: #825900;">$current_page</span>; <span style="color: #dd0000;">?&gt;</span> of <span style="color: #dd0000;">&lt;?php</span> <span style="color: #881350;">echo</span> <span style="color: #825900;">$total_page</span>; <span style="color: #dd0000;">?&gt;</span><span style="color: #881280;">&lt;/span&gt;</span>
Â Â Â Â Â Â Â Â Â Â Â Â <span style="color: #881280;">&lt;div </span><span style="color: #994500;">class</span><span style="color: #881280;">=</span><span style="color: #1a1aa6;">"clearfix"</span><span style="color: #881280;">&gt;&amp;nbsp;&lt;/div&gt;</span>
Â Â Â Â Â Â Â Â <span style="color: #881280;">&lt;/dt&gt;</span>
Â Â Â Â Â Â Â Â <span style="color: #dd0000;">&lt;?php</span> <span style="color: #881350;">foreach</span>(<span style="color: #825900;">$posts</span> <span style="color: #881350;">as</span> <span style="color: #825900;">$post</span>):<span style="color: #dd0000;">?&gt;</span>
Â Â Â Â Â Â Â Â <span style="color: #881280;">&lt;dd </span><span style="color: #994500;">id</span><span style="color: #881280;">=</span><span style="color: #1a1aa6;">""</span><span style="color: #881280;">&gt;</span>
Â Â Â Â Â Â Â Â <span style="color: #881280;">&lt;a </span><span style="color: #994500;">href</span><span style="color: #881280;">=</span><span style="color: #1a1aa6;">"</span><span style="color: #dd0000;">&lt;?php</span> <span style="color: #881350;">echo</span> <span style="color: #825900;">$post</span>[<span style="color: #eb7300;">'guid'</span>]; <span style="color: #dd0000;">?&gt;</span><span style="color: #1a1aa6;">"</span><span style="color: #881280;">&gt;</span>
Â Â Â Â Â Â Â Â <span style="color: #881280;">&lt;p&gt;</span>
Â Â Â Â Â Â Â Â <span style="color: #881280;">&lt;b&gt;</span><span style="color: #dd0000;">&lt;?php</span> <span style="color: #881350;">echo</span> <span style="color: #825900;">$this</span>-&gt;search_model-&gt;<span style="color: #003369;">highlightWords</span>(<span style="color: #825900;">$post</span>[<span style="color: #eb7300;">'post_title'</span>], <span style="color: #825900;">$search_term</span>); <span style="color: #dd0000;">?&gt;</span><span style="color: #881280;">&lt;/b&gt;</span>

Â Â Â Â Â Â Â Â <span style="color: #dd0000;">&lt;?php</span> <span style="color: #881350;">echo</span> <span style="color: #825900;">$this</span>-&gt;search_model-&gt;<span style="color: #003369;">highlightWords</span>(<span style="color: #825900;">$this</span>-&gt;search_model-&gt;<span style="color: #003369;">cleanInput</span>(<span style="color: #825900;">$post</span>[<span style="color: #eb7300;">'post_content'</span>]), <span style="color: #825900;">$search_term</span>); <span style="color: #dd0000;">?&gt;</span>
Â Â Â Â Â Â Â Â <span style="color: #881280;">&lt;/p&gt;&lt;/a&gt;</span>
Â Â Â Â Â Â Â Â <span style="color: #881280;">&lt;/dd&gt;</span>
Â Â Â Â Â Â Â Â <span style="color: #dd0000;">&lt;?php</span> <span style="color: #881350;">endforeach</span>;<span style="color: #dd0000;">?&gt;</span>
Â Â Â Â Â Â Â Â <span style="color: #881280;">&lt;dt&gt;</span>
Â Â Â Â Â Â Â Â <span style="color: #881280;">&lt;div </span><span style="color: #994500;">class</span><span style="color: #881280;">=</span><span style="color: #1a1aa6;">"clearfix"</span><span style="color: #881280;">&gt;&amp;nbsp;&lt;/div&gt;</span>
Â Â Â Â Â Â Â Â <span style="color: #881280;">&lt;/dt&gt;</span>

Â Â Â Â Â Â Â Â <span style="color: #dd0000;">&lt;?php</span> <span style="color: #881350;">else</span>: <span style="color: #dd0000;">?&gt;</span>

Â Â Â Â Â Â Â Â <span style="color: #881280;">&lt;dt&gt;</span>
Â Â Â Â Â Â Â Â Â Â Â Â <span style="color: #881280;">&lt;span </span><span style="color: #994500;">class</span><span style="color: #881280;">=</span><span style="color: #1a1aa6;">"results_meta"</span><span style="color: #881280;">&gt;</span>There were no results<span style="color: #881280;">&lt;/span&gt;</span>
Â Â Â Â Â Â Â Â Â Â Â Â <span style="color: #881280;">&lt;div </span><span style="color: #994500;">class</span><span style="color: #881280;">=</span><span style="color: #1a1aa6;">"clearfix"</span><span style="color: #881280;">&gt;&amp;nbsp;&lt;/div&gt;</span>
Â Â Â Â Â Â Â Â <span style="color: #881280;">&lt;/dt&gt;</span>

Â Â Â Â Â Â Â Â <span style="color: #881280;">&lt;dt&gt;</span>
Â Â Â Â Â Â Â Â <span style="color: #881280;">&lt;div </span><span style="color: #994500;">class</span><span style="color: #881280;">=</span><span style="color: #1a1aa6;">"clearfix"</span><span style="color: #881280;">&gt;&amp;nbsp;&lt;/div&gt;</span>
Â Â Â Â Â Â Â Â <span style="color: #881280;">&lt;/dt&gt;</span>

Â Â Â Â Â Â Â Â <span style="color: #dd0000;">&lt;?php</span> <span style="color: #881350;">endif</span>; <span style="color: #dd0000;">?&gt;</span>

Â Â Â Â Â Â Â Â <span style="color: #881280;">&lt;/dl&gt;</span>
Â Â Â Â Â Â Â Â <em><span style="color: #236e25;">&lt;!-- End search result --&gt;</span></em></pre>
<p>Each variable you called in this result was passed by the controller:</p>
<pre class="php">Â Â Â Â <span style="color: #825900;">$data</span>[<span style="color: #eb7300;">'search_term'</span>]=<span style="color: #825900;">$clean_query</span>;
Â Â Â Â <span style="color: #825900;">$data</span>[<span style="color: #eb7300;">'total_posts'</span>]=<span style="color: #825900;">$totalrows</span>;
Â Â Â Â <span style="color: #825900;">$data</span>[<span style="color: #eb7300;">'current_page'</span>]=<span style="color: #825900;">$current_page</span>;
Â Â Â Â <span style="color: #825900;">$data</span>[<span style="color: #eb7300;">'next_page'</span>]=<span style="color: #825900;">$current_page</span>+<span style="color: #0000ff;">1</span>;

Â Â Â Â <span style="color: #825900;">$data</span>[<span style="color: #eb7300;">'has_next'</span>]=<span style="color: #825900;">$has_next</span>;
Â Â Â Â <span style="color: #825900;">$data</span>[<span style="color: #eb7300;">'has_prev'</span>]=<span style="color: #825900;">$has_prev</span>;

Â Â Â Â <span style="color: #825900;">$data</span>[<span style="color: #eb7300;">'prev_page'</span>]=<span style="color: #825900;">$current_page</span>-<span style="color: #0000ff;">1</span>;
Â Â Â Â <span style="color: #825900;">$data</span>[<span style="color: #eb7300;">'total_page'</span>]=<span style="color: #825900;">$totalpages</span>;
Â Â Â Â <span style="color: #825900;">$data</span>[<span style="color: #eb7300;">'posts'</span>]=<span style="color: #825900;">$posts</span>;

Â Â Â Â ....

Â Â Â Â <span style="color: #825900;">$this</span>-&gt;load-&gt;<span style="color: #003369;">view</span>(<span style="color: #eb7300;">'search_result'</span>, <span style="color: #825900;">$data</span>);</pre>
<p>The CSS code is in the download file. It&#8217;s a very long code of CSS, so I decided not to post it here.</p>
<h3>7. Work with jQuery</h3>
<hr />
<p>We need to use jquery to send request and display the data responded. Create javascript file name <strong>search.js</strong> in the <strong>js</strong> folder. Of course, this js folder is at root folder, not in <strong>views</strong> folder of Codeigniter. You can see the clear link of this file at the header which was created earlier.<br />
The jQuery code was modified from the Knowledge Base of <a href="http://mediatemple.net">Mediatemple</a>. I removed some unnecessary and complicated code. If we&#8217;re gonna make a live search, we&#8217;ll only need as enough as in this tutorial.</p>
<p>Declare some variables:</p>
<pre><span style="color: #881350;">var</span> SEARCH_BOX_DEFAULT_TEXT = <span style="color: #760f15;">'Enter the keywords:'</span>;

<span style="color: #881350;">var</span> AJAX_PENDING_TIMER;
<span style="color: #881350;">var</span> CURRENT_PAGE = <span style="color: #0000ff;">1</span>;
<span style="color: #881350;">var</span> CURRENT_LIMIT = <span style="color: #0000ff;">5</span>;</pre>
<p><strong>AJAX_PENDING_TIMER</strong> is the time which is used for the time delay before performing the search function.</p>
<p>Now, create the initial function. We will set up the value of search field, perform the search in condition and the button to close the livesearch panel.</p>
<pre class="javascript">function <span style="color: #003369;">init</span>() {

Â Â Â Â <span style="color: #881350;">var</span> sTextBox Â Â = $(<span style="color: #760f15;">"#search_val"</span>);

Â Â Â Â sTextBox.<span style="color: #003369;">focus</span>(<span style="color: #881350;">function</span>() {
Â Â Â Â Â Â Â Â <span style="color: #881350;">if</span>(<span style="color: #881350;">this</span>.value == SEARCH_BOX_DEFAULT_TEXT) {
Â Â Â Â Â Â Â Â Â Â Â Â <span style="color: #881350;">this</span>.value = <span style="color: #760f15;">''</span>;
Â Â Â Â Â Â Â Â }
Â Â Â Â });
Â Â Â Â sTextBox.<span style="color: #003369;">blur</span>(<span style="color: #881350;">function</span>() {
Â Â Â Â Â Â Â Â <span style="color: #881350;">if</span>(<span style="color: #881350;">this</span>.value == <span style="color: #760f15;">''</span>) {
Â Â Â Â Â Â Â Â Â Â Â Â <span style="color: #881350;">this</span>.value = SEARCH_BOX_DEFAULT_TEXT;
Â Â Â Â Â Â Â Â }
Â Â Â Â });
Â Â Â Â sTextBox.<span style="color: #003369;">blur</span>();

Â Â Â Â sTextBox.<span style="color: #003369;">keyup</span>(<span style="color: #881350;">function</span>() {
Â Â Â Â Â Â Â Â <span style="color: #881350;">var</span> q Â Â Â = $(<span style="color: #760f15;">"#search_val"</span>).<span style="color: #003369;">val</span>();
Â Â Â Â Â Â Â Â <span style="color: #881350;">if</span>( q == SEARCH_BOX_DEFAULT_TEXT || q == <span style="color: #760f15;">''</span> || q == undefined || q.length&lt;=<span style="color: #0000ff;">3</span>) {
Â Â Â Â Â Â Â Â Â Â Â Â <span style="color: #003369;">HideLiveSearch</span>();
Â Â Â Â Â Â Â Â }
Â Â Â Â Â Â Â Â <span style="color: #881350;">else</span> {
Â Â Â Â Â Â Â Â Â Â Â Â <span style="color: #003369;">clearTimeout</span>(AJAX_PENDING_TIMER);
Â Â Â Â Â Â Â Â Â Â Â Â CURRENT_PAGE = <span style="color: #0000ff;">1</span>;
Â Â Â Â Â Â Â Â Â Â Â Â AJAX_PENDING_TIMER = <span style="color: #003369;">setTimeout</span>(<span style="color: #760f15;">"PerformLiveSearch()"</span>,<span style="color: #0000ff;">300</span>);
Â Â Â Â Â Â Â Â }

Â Â Â Â });

Â Â Â Â $(<span style="color: #760f15;">"#livesearch_result_close_link"</span>).<span style="color: #003369;">click</span>(<span style="color: #881350;">function</span>() {
Â Â Â Â Â Â Â Â <span style="color: #003369;">HideLiveSearch</span>();
Â Â Â Â });

}</pre>
<p>Look at the keyup event, we only perform livesearch when users input more than 3 characters. It&#8217;s for saving HTTP request.<br />
Add these line at the bottom of javascript file:</p>
<pre class="javascript">$(document).<span style="color: #003369;">ready</span>(<span style="color: #881350;">function</span>() {
Â Â Â Â <span style="color: #003369;">init</span>();
});</pre>
<p>The completed code of <strong>PerformLiveSearch()</strong> function:</p>
<pre class="javascript"><span style="color: #881350;">var</span> AJAX_IS_RUNNING = <span style="color: #881350;">false</span>;
<span style="color: #881350;">function</span> <span style="color: #003369;">PerformLiveSearch</span>() {

Â Â Â Â <span style="color: #881350;">if</span>(AJAX_IS_RUNNING == <span style="color: #881350;">true</span>) {
Â Â Â Â Â Â Â Â <span style="color: #881350;">return</span>;
Â Â Â Â }

Â Â Â Â <span style="color: #881350;">var</span> query Â Â Â Â Â = $(<span style="color: #760f15;">"#search_val"</span>);
Â Â Â Â <span style="color: #881350;">var</span> q_val Â Â Â Â Â = (query.<span style="color: #003369;">val</span>() &amp;&amp; query.<span style="color: #003369;">val</span>() != SEARCH_BOX_DEFAULT_TEXT) ? query.<span style="color: #003369;">val</span>() : <span style="color: #760f15;">''</span>; Â Â Â 

Â Â Â Â <span style="color: #881350;">if</span>(q_val == <span style="color: #760f15;">''</span>) {
Â Â Â Â Â Â Â Â <span style="color: #881350;">return</span>;
Â Â Â Â }
Â Â Â Â AJAX_IS_RUNNING = <span style="color: #881350;">true</span>;

Â Â Â Â $.<span style="color: #003369;">ajax</span>({
Â Â Â Â Â Â Â Â url: Â Â Â Â Â Â Â <span style="color: #760f15;">'./search'</span>,
Â Â Â Â Â Â Â Â data: {
Â Â Â Â Â Â Â Â Â Â Â Â query: q_val,
Â Â Â Â Â Â Â Â Â Â Â Â output: <span style="color: #760f15;">'json'</span>,
Â Â Â Â Â Â Â Â Â Â Â Â page: CURRENT_PAGE,
Â Â Â Â Â Â Â Â Â Â Â Â limit: CURRENT_LIMIT
Â Â Â Â Â Â Â Â },
Â Â Â Â Â Â Â Â type: Â Â Â Â Â Â <span style="color: #760f15;">'GET'</span>,
Â Â Â Â Â Â Â Â timeout: Â Â Â <span style="color: #760f15;">'5000'</span>,
Â Â Â Â Â Â Â Â dataType: Â Â <span style="color: #760f15;">'json'</span>,
Â Â Â Â Â Â Â Â beforeSend: <span style="color: #881350;">function</span>() {
Â Â Â Â Â Â Â Â Â Â Â Â <em><span style="color: #236e25;">// Before send request </span></em>Â Â Â Â Â Â Â Â Â Â Â Â <em><span style="color: #236e25;">// Show the loader </span></em>Â Â Â Â Â Â Â Â Â Â Â Â AJAX_IS_RUNNING = <span style="color: #881350;">true</span>;
Â Â Â Â Â Â Â Â Â Â Â Â <span style="color: #003369;">ShowLoaders</span>();
Â Â Â Â Â Â Â Â },
Â Â Â Â Â Â Â Â complete: <span style="color: #881350;">function</span>() {
Â Â Â Â Â Â Â Â Â Â Â Â <em><span style="color: #236e25;">// When Sent request </span></em>Â Â Â Â Â Â Â Â Â Â Â Â <em><span style="color: #236e25;">// Hide the loader </span></em>Â Â Â Â Â Â Â Â Â Â Â Â AJAX_IS_RUNNING = <span style="color: #881350;">false</span>;
Â Â Â Â Â Â Â Â Â Â Â Â <span style="color: #003369;">HideLoaders</span>();
Â Â Â Â Â Â Â Â },
Â Â Â Â Â Â Â Â success: <span style="color: #881350;">function</span>(data, textStatus) {
Â Â Â Â Â Â Â Â Â Â Â Â AJAX_IS_RUNNING = <span style="color: #881350;">false</span>;
Â Â Â Â Â Â Â Â Â Â Â Â <span style="color: #003369;">HideLoaders</span>();
Â Â Â Â Â Â Â Â Â Â Â Â $(<span style="color: #760f15;">'#livesearch'</span>).<span style="color: #003369;">slideDown</span>();

Â Â Â Â Â Â Â Â Â Â Â Â <em><span style="color: #236e25;">// clear the results </span></em>Â Â Â Â Â Â Â Â Â Â Â Â $(<span style="color: #760f15;">".livesearch_results dd"</span>).<span style="color: #003369;">remove</span>();
Â Â Â Â Â Â Â Â Â Â Â Â <span style="color: #881350;">var</span> resultsNav = $(<span style="color: #760f15;">'.livesearch_results dt'</span>);

Â Â Â Â Â Â Â Â Â Â Â Â <span style="color: #881350;">if</span>( data[<span style="color: #760f15;">'results'</span>].length ) {

Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â <em><span style="color: #236e25;">// add the new results (in reverse since the </span></em>Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â <em><span style="color: #236e25;">// append inserts right after the header and not </span></em>Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â <em><span style="color: #236e25;">// at the end of the result list </span></em>Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â <span style="color: #881350;">var</span> current = resultsNav;
Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â <span style="color: #881350;">for</span>(i=data[<span style="color: #760f15;">'results'</span>].length;i&gt;<span style="color: #0000ff;">0</span>;i--) {
Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â current.<span style="color: #003369;">after</span>(
Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â <span style="color: #003369;">ResultRowHTML</span>(data[<span style="color: #760f15;">'results'</span>][i-<span style="color: #0000ff;">1</span>])
Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â );
Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â }
Â Â Â Â Â Â Â Â Â Â Â Â }
Â Â Â Â Â Â Â Â Â Â Â Â <span style="color: #881350;">else</span> {
Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â resultsNav.<span style="color: #003369;">after</span>(<span style="color: #760f15;">'&lt;dd id=""&gt;&lt;p&gt;No articles found with these search terms&lt;/p&gt;&lt;/dd&gt;'</span>);
Â Â Â Â Â Â Â Â Â Â Â Â }

Â Â Â Â Â Â Â Â Â Â Â Â <em><span style="color: #236e25;">// Pagination at the bottom of LiveSearch panel </span></em>Â Â Â Â Â Â Â Â Â Â Â Â <span style="color: #003369;">UpdateResultFooter</span>(data[<span style="color: #760f15;">'paging'</span>],<span style="color: #760f15;">".livesearch_navbody"</span>);

Â Â Â Â Â Â Â Â },

Â Â Â Â Â Â Â Â <em><span style="color: #236e25;">// We're gonna hide everything when get error </span></em>Â Â Â Â Â Â Â Â error: <span style="color: #881350;">function</span>(XMLHttpRequest, textStatus, errorThrown) {
Â Â Â Â Â Â Â Â Â Â Â Â AJAX_IS_RUNNING = <span style="color: #881350;">false</span>;
Â Â Â Â Â Â Â Â Â Â Â Â <span style="color: #003369;">HideLoaders</span>();
Â Â Â Â Â Â Â Â Â Â Â Â <span style="color: #003369;">HideLiveSearch</span>();
Â Â Â Â Â Â Â Â }
Â Â Â Â });

}</pre>
<p>The result will be displayed by a function name <strong>DisplayResult</strong>. Parameter is the value in <strong>data['results']</strong> array.</p>
<pre class="javascript"><span style="color: #881350;">function</span> <span style="color: #003369;">DisplayResult</span>(row) {
Â Â Â Â <span style="color: #881350;">var</span> output = <span style="color: #760f15;">'&lt;dd id=""&gt;'</span>;
Â Â Â Â output += <span style="color: #760f15;">'&lt;a href="'</span> + row[<span style="color: #760f15;">'url'</span>] + <span style="color: #760f15;">'"&gt;'</span>;
Â Â Â Â output += <span style="color: #760f15;">'&lt;p&gt;'</span>;
Â Â Â Â output += <span style="color: #760f15;">'&lt;b&gt;'</span> + row[<span style="color: #760f15;">'title'</span>] + <span style="color: #760f15;">'&lt;/b&gt;'</span>;
Â Â Â Â output += row[<span style="color: #760f15;">'summary'</span>];
Â Â Â Â output += <span style="color: #760f15;">'&lt;/p&gt;&lt;/a&gt;&lt;/dd&gt;'</span>;
Â Â Â Â <span style="color: #881350;">return</span> output;
}</pre>
<p>The Pagination function by jQuery:</p>
<pre class="javascript"><span style="color: #881350;">function</span> <span style="color: #003369;">Pagination</span>(p,selector) {

Â Â Â Â $(selector + <span style="color: #760f15;">" *"</span>).<span style="color: #003369;">remove</span>();

Â Â Â Â <span style="color: #881350;">if</span>(p[<span style="color: #760f15;">'start_idx'</span>] != undefined) {
Â Â Â Â Â Â Â Â <span style="color: #881350;">var</span> html = <span style="color: #760f15;">'&lt;span class="livesearch_legend"&gt;'</span> + p[<span style="color: #760f15;">'start_idx'</span>] + <span style="color: #760f15;">' - '</span> + p[<span style="color: #760f15;">'end_idx'</span>] + <span style="color: #760f15;">' of '</span> + p[<span style="color: #760f15;">'total'</span>] + <span style="color: #760f15;">' Results&lt;/span&gt;'</span>;
Â Â Â Â Â Â Â Â html += <span style="color: #760f15;">'&lt;a class="livesearch_next" href="javascript:void(0);" title="Next 5 Results"&gt;&lt;em&gt;Next&lt;/em&gt;&lt;/a&gt;'</span>;
Â Â Â Â Â Â Â Â html += <span style="color: #760f15;">'&lt;a class="livesearch_prev" href="javascript:void(0);" title="Previous 5 Results"&gt;&lt;em&gt;Previous&lt;/em&gt;&lt;/a&gt;'</span>;
Â Â Â Â Â Â Â Â html += <span style="color: #760f15;">'&lt;div class="clearfix"&gt;&amp;nbsp;&lt;/div&gt;'</span>;

Â Â Â Â Â Â Â Â $(selector).<span style="color: #003369;">append</span>(html);
Â Â Â Â }
Â Â Â Â <span style="color: #881350;">else</span> {
Â Â Â Â Â Â Â Â <span style="color: #881350;">var</span> html = <span style="color: #760f15;">'&lt;span class="kbls_legend"&gt;0 Results&lt;/span&gt;'</span>;
Â Â Â Â Â Â Â Â html += <span style="color: #760f15;">'&lt;a class="livesearch_next" href="javascript:void(0);" title="Next 5 Results"&gt;&lt;em&gt;Next&lt;/em&gt;&lt;/a&gt;'</span>;
Â Â Â Â Â Â Â Â html += <span style="color: #760f15;">'&lt;a class="livesearch_prev" href="javascript:void(0);" title="Previous 5 Results"&gt;&lt;em&gt;Previous&lt;/em&gt;&lt;/a&gt;'</span>;
Â Â Â Â Â Â Â Â html += <span style="color: #760f15;">'&lt;div class="clearfix"&gt;&amp;nbsp;&lt;/div&gt;'</span>;

Â Â Â Â Â Â Â Â $(selector).<span style="color: #003369;">append</span>(html);
Â Â Â Â }

Â Â Â Â $(selector + <span style="color: #760f15;">" .livesearch_next"</span>).<span style="color: #003369;">click</span>(<span style="color: #881350;">function</span>() {
Â Â Â Â Â Â Â Â <span style="color: #003369;">NextPage</span>(p);
Â Â Â Â });
Â Â Â Â $(selector + <span style="color: #760f15;">" .livesearch_prev"</span>).<span style="color: #003369;">click</span>(<span style="color: #881350;">function</span>() {
Â Â Â Â Â Â Â Â <span style="color: #003369;">PrevPage</span>(p);
Â Â Â Â });

}</pre>
<p>This function just rends the html code to display pagination. The range of post and number of total post depend on <strong>data['results']</strong>. The Next and Previous button are handled by:</p>
<pre class="javascript"><span style="color: #881350;">function</span> <span style="color: #003369;">NextPage</span>(p) {
Â Â Â Â <span style="color: #881350;">if</span>(p[<span style="color: #760f15;">'has_next'</span>]) {
Â Â Â Â Â Â Â Â AJAX_IS_RUNNING = <span style="color: #881350;">false</span>;
Â Â Â Â Â Â Â Â CURRENT_PAGE++;
Â Â Â Â Â Â Â Â <span style="color: #003369;">PerformLiveSearch</span>();
Â Â Â Â }
}
<span style="color: #881350;">function</span> <span style="color: #003369;">PrevPage</span>(p) {
Â Â Â Â <span style="color: #881350;">if</span>(p[<span style="color: #760f15;">'has_prev'</span>]) {
Â Â Â Â Â Â Â Â AJAX_IS_RUNNING = <span style="color: #881350;">false</span>;
Â Â Â Â Â Â Â Â CURRENT_PAGE--;
Â Â Â Â Â Â Â Â <span style="color: #003369;">PerformLiveSearch</span>();
Â Â Â Â }
}</pre>
<p>The link to next page and previous page is automatically increased and decreased if it has next page and previous page.<br />
The ajax loader when ajax is running or not will be in these functions:</p>
<pre class="javascript"><span style="color: #881350;">function</span> <span style="color: #003369;">ShowLoaders</span>() {
Â Â Â Â $(<span style="color: #760f15;">'#ajaxloader'</span>).<span style="color: #003369;">fadeIn</span>(<span style="color: #760f15;">'fast'</span>);

Â Â Â Â <span style="color: #881350;">if</span>( $(<span style="color: #760f15;">'#livesearch'</span>).<span style="color: #003369;">css</span>(<span style="color: #760f15;">'display'</span>) == <span style="color: #760f15;">'block'</span> ) {
Â Â Â Â Â Â Â Â <span style="color: #881350;">var</span> h = $(<span style="color: #760f15;">'#livesearch'</span>).<span style="color: #003369;">height</span>() - <span style="color: #0000ff;">5</span>;
Â Â Â Â Â Â Â Â <span style="color: #881350;">var</span> w = $(<span style="color: #760f15;">'#livesearch'</span>).<span style="color: #003369;">width</span>() - <span style="color: #0000ff;">45</span>;
Â Â Â Â Â Â Â Â $(<span style="color: #760f15;">'#loader_div'</span>).<span style="color: #003369;">width</span>(w);
Â Â Â Â Â Â Â Â $(<span style="color: #760f15;">'#loader_div'</span>).<span style="color: #003369;">height</span>(h);
Â Â Â Â Â Â Â Â $(<span style="color: #760f15;">'#loader_div p'</span>).<span style="color: #003369;">css</span>(<span style="color: #760f15;">'margin-top'</span>,(h/<span style="color: #0000ff;">2</span>)+<span style="color: #0000ff;">20</span>);
Â Â Â Â Â Â Â Â $(<span style="color: #760f15;">'#loader_div'</span>).<span style="color: #003369;">fadeIn</span>(<span style="color: #760f15;">'fast'</span>);
Â Â Â Â }
}

<span style="color: #881350;">function</span> <span style="color: #003369;">HideLoaders</span>() {
Â Â Â Â $(<span style="color: #760f15;">'#ajaxloader'</span>).<span style="color: #003369;">fadeOut</span>(<span style="color: #760f15;">'fast'</span>);
Â Â Â Â $(<span style="color: #760f15;">'#loader_div'</span>).<span style="color: #003369;">hide</span>();
}</pre>
<p>You can find where the ajax loader by searching in the <strong>search_form.php</strong> file:</p>
<pre class="php">.........

Â Â Â Â Â Â Â Â Â <span style="color: #881280;">&lt;fieldset&gt;</span>
Â Â Â Â Â Â Â Â Â Â Â Â <span style="color: #881280;">&lt;img </span><span style="color: #994500;">src</span><span style="color: #881280;">=</span><span style="color: #1a1aa6;">"</span><span style="color: #dd0000;">&lt;?php</span> <span style="color: #881350;">echo</span> <span style="color: #003369;">base_url</span>(); <span style="color: #dd0000;">?&gt;</span><span style="color: #1a1aa6;">/images/ajax-loader.gif"</span><span style="color: #881280;"> Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â </span><span style="color: #994500;">id</span><span style="color: #881280;">=</span><span style="color: #1a1aa6;">"ajaxloader"</span><span style="color: #994500;">alt</span><span style="color: #881280;">=</span><span style="color: #1a1aa6;">"Loading Results..."</span><span style="color: #994500;">style</span><span style="color: #881280;">=</span><span style="color: #1a1aa6;">"display: none;"</span><span style="color: #881280;">&gt;</span>
Â Â Â Â Â Â Â Â Â Â Â Â <span style="color: #881280;">&lt;input </span><span style="color: #994500;">name</span><span style="color: #881280;">=</span><span style="color: #1a1aa6;">"query"</span><span style="color: #994500;">id</span><span style="color: #881280;">=</span><span style="color: #1a1aa6;">"search_val"</span><span style="color: #994500;">value</span><span style="color: #881280;">=</span><span style="color: #1a1aa6;">"</span><span style="color: #dd0000;">&lt;?php</span> <span style="color: #661aa9;">printf</span>(<span style="color: #825900;">$search_term</span>); <span style="color: #dd0000;">?&gt;</span><span style="color: #1a1aa6;">"</span><span style="color: #881280;"> Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â </span><span style="color: #994500;">tabindex</span><span style="color: #881280;">=</span><span style="color: #1a1aa6;">"1"</span><span style="color: #994500;">autocomplete</span><span style="color: #881280;">=</span><span style="color: #1a1aa6;">"off"</span><span style="color: #994500;">type</span><span style="color: #881280;">=</span><span style="color: #1a1aa6;">"text"</span><span style="color: #881280;">&gt;</span>
Â Â Â Â Â Â Â Â Â <span style="color: #881280;">&lt;/fieldset&gt;</span>

.........</pre>
<p>And hide the LiveSearch panel:</p>
<pre class="javascript"><span style="color: #881350;">function</span> <span style="color: #003369;">HideLiveSearch</span>() {
Â Â Â Â $(<span style="color: #760f15;">'#livesearch'</span>).<span style="color: #003369;">slideUp</span>();
Â Â Â Â $(<span style="color: #760f15;">'#ajaxloader'</span>).<span style="color: #003369;">fadeOut</span>(<span style="color: #760f15;">'fast'</span>);
}</pre>
<h3>8. One more step</h3>
<hr />
<p>The default URL in our address bar when you use CodeIgniter framework is:</p>
<pre class="php">http://<span style="color: #881280;">&lt;you-codeigniter&gt;</span>/index.php/<span style="color: #881280;">&lt;your-controller-filename&gt;</span></pre>
<p>So, write in your .htaccess file with following content to re-write your URL</p>
<pre class="html">http://<span style="color: #881280;">&lt;you-codeigniter&gt;</span>/<span style="color: #881280;">&lt;your-controller-filename&gt;</span></pre>
<p>The code for URL rewrite is:</p>
<pre class="html">&lt;IfModule mod_rewrite.c&gt;
Â Â Â Â RewriteEngine On
Â Â Â Â RewriteBase /

Â Â Â Â #Removes access to the system folder by users.
Â Â Â Â #Additionally this will allow you to create a System.php controller,
Â Â Â Â #previously this would not have been possible.
Â Â Â Â #'system' can be replaced if you have renamed your system folder.
Â Â Â Â RewriteCond %{REQUEST_URI} ^system.*
Â Â Â Â RewriteRule ^(.*)$ /index.php/$1 [L]

Â Â Â Â #Checks to see if the user is attempting to access a valid file,
Â Â Â Â #such as an image or css document, if this isn't true it sends the
Â Â Â Â #request to index.php
Â Â Â Â RewriteCond %{REQUEST_FILENAME} !-f
Â Â Â Â RewriteCond %{REQUEST_FILENAME} !-d
Â Â Â Â RewriteRule ^(.*)$ /index.php/$1 [L]
&lt;/IfModule&gt;</pre>
<h3>WOW</h3>
<hr />
<p>What&#8217;s wow? Just relax after working. Feel free to give me a comment. I&#8217;m not sure if I corrected all the whole things, but it seems work fine in my demo. Enjoy it and download the source code. Cheer!</p>
]]></content:encoded>
			<wfw:commentRss>http://aext.net/2009/10/codeigniter-and-jquery-real-live-search-with-pagination/feed/</wfw:commentRss>
		<slash:comments>87</slash:comments>
		</item>
		<item>
		<title>WordPress Theme Design with Options Administration</title>
		<link>http://aext.net/2009/10/wordpress-theme-design-with-options-adminstration/</link>
		<comments>http://aext.net/2009/10/wordpress-theme-design-with-options-adminstration/#comments</comments>
		<pubDate>Thu, 01 Oct 2009 23:35:23 +0000</pubDate>
		<dc:creator>Lief</dc:creator>
				<category><![CDATA[Popular]]></category>
		<category><![CDATA[WordPress]]></category>
		<category><![CDATA[get_option()]]></category>
		<category><![CDATA[Options]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Theme]]></category>
		<category><![CDATA[Tips]]></category>
		<category><![CDATA[Wordpress]]></category>
		<category><![CDATA[Wordpress Theme]]></category>

		<guid isPermaLink="false">http://aext.net/?p=921</guid>
		<description><![CDATA[<p><a href="http://aext.net/theme/platinoom" title="Platinoom - Premium WordPress Themes"><img src="http://aext.net/wp-content/uploads/2011/04/platinoom_wordpress_theme.png" alt="ThemeKiss" /></a></p><p style="float: right;"><a href="http://api.tweetmeme.com/share?url=http://aext.net/2009/10/wordpress-theme-design-with-options-adminstration/&service=su.pr&service_api=b57727e991c454bd2b2c62ff71462c79"><img src="http://api.tweetmeme.com/imagebutton.gif?url=http://aext.net/2009/10/wordpress-theme-design-with-options-adminstration/" height="61" width="51" /></a></p>Are you running Wordpress? And how are you controlling all options of your current wordpress theme? Wordpress offers you get_option() function to get the options of your wordpress. However, all of default options wordpress gives to you are not enough. You maybe want to add more options that easy to implement on your theme design. How to do that? This tutorial will help you create options and administrate them in your Wordpress Administration Panel <a href="http://aext.net/2009/10/wordpress-theme-design-with-options-adminstration/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p><a href="http://aext.net/theme/platinoom" title="Platinoom - Premium WordPress Themes"><img src="http://aext.net/wp-content/uploads/2011/04/platinoom_wordpress_theme.png" alt="ThemeKiss" /></a></p><p style="float: right;"><a href="http://api.tweetmeme.com/share?url=http://aext.net/2009/10/wordpress-theme-design-with-options-adminstration/&service=su.pr&service_api=b57727e991c454bd2b2c62ff71462c79"><img src="http://api.tweetmeme.com/imagebutton.gif?url=http://aext.net/2009/10/wordpress-theme-design-with-options-adminstration/" height="61" width="51" /></a></p><p>Are you running WordPress? And how are you controlling all options of your current wordpress theme? WordPress offers you get_option() function to get the options of your wordpress. However, all of default options wordpress gives to you are not enough. You maybe want to add more options that easy to implement on your theme design. How to do that? This tutorial will help you create options and administrate them in wordpress admin page.</p>
<p><span id="more-921"></span></p>
<div class="demo-download">
<ul>
<li><a class="downloadit" href="http://www.box.net/shared/yd1yyts3ev">Download</a></li>
</ul>
</div>
<h3>Theme Options</h3>
<hr />
<p>All of wordpress options are stored in the database under table wp_options, and your theme options will be so. Let&#8217;s see the structure of wp_options table, all the field, and data type of each are:</p>
<ul class="mylist">
<li>option_id &#8211; BIGINT</li>
<li>blog_id &#8211; INTEGER</li>
<li><strong>option_name</strong> &#8211; VARCHAR</li>
<li><strong>option_value</strong> &#8211; LONGTEXT</li>
<li>autoload &#8211; VARCHAR</li>
</ul>
<p>All you need are the fields in bold, because you use the function get_option() to get options with the name in option_name filed, and the option_value, of course is what you need to get.<br />
The default using of get_option() is:</p>
<pre class="php"><span style="color: #0000bb;">&lt;?phpÂ </span><span style="color: #007700;">echoÂ </span><span style="color: #0000bb;">get_option</span><span style="color: #007700;">(Â </span><span style="color: #0000bb;">$showÂ </span><span style="color: #007700;">);Â </span><span style="color: #0000bb;">?&gt;</span></pre>
<p>And the code above is the same as the query below:</p>
<div class="bigimage"><img class="alignnone size-full wp-image-929" title="get_option() funtion and return value" src="http://aext.net/wp-contents/uploads/2009/09/get_option-funtion-and-return-value.jpg" alt="get_option() funtion and return value" width="500" /></div>
<p>However, you should know the difference is the function will return FASLE if the desired option does not exist, or no value is associated with it, but the query will return null.</p>
<h3>How do we process an option?</h3>
<hr />
<p>At first, you have to declare an option on your theme functions.php file. The simple option is fine for configuration and getting will be declared as:</p>
<pre class="html"><span style="color: #236e25;"><em>// theme options </em></span><span style="color: #825900;">$options</span> = <span style="color: #881350;">array</span>(

Â Â <span style="color: #881350;">array</span>(
Â Â Â Â <span style="color: #eb7300;">"name"</span> Â Â Â Â Â =&gt; <span style="color: #eb7300;">"Option Title"</span>,
Â Â Â Â <span style="color: #eb7300;">"desc"</span> Â Â Â Â Â =&gt; <span style="color: #eb7300;">"Option Description"</span>,
Â Â Â Â <span style="color: #eb7300;">"id"</span> Â Â Â Â Â Â Â =&gt; <span style="color: #eb7300;">"Option Unique ID"</span>, <span style="color: #236e25;"><em>//This is option_name filed in the database </em></span>Â Â Â Â <span style="color: #eb7300;">"default"</span> Â Â =&gt; <span style="color: #eb7300;">"10"</span>), <span style="color: #236e25;"><em>//Its default value </em></span>
);</pre>
<p>Your theme setting page will as good as display as image below:</p>
<div class="bigimage"><img class="alignnone size-full wp-image-933" title="get_option - The option display on setting page" src="http://aext.net/wp-contents/uploads/2009/09/get_option-The-option-display-on-setting-page.jpg" alt="get_option - The option display on setting page" width="500" height="217" /></div>
<p>Hey, but before getting a result above, we have to write some code for displaying. As an example, I will make a block of an option. In my case, I want to set an option for number of Community News article will be displayed on the homepage.</p>
<p>Declare the option:</p>
<pre class="php"><span style="color: #dd0000;">&lt;?php</span>

<span style="color: #825900;">$options</span> = <span style="color: #881350;">array</span>(

  <span style="color: #881350;">array</span>(    
    <span style="color: #eb7300;">"name"</span> =&gt; <span style="color: #eb7300;">"&lt;strong&gt;Homepage Community Entries&lt;/strong&gt;"</span>,
    <span style="color: #eb7300;">"desc"</span> =&gt; <span style="color: #eb7300;">"Select the number of Community entries that should appear on the Index page"</span>,
    <span style="color: #eb7300;">"id"</span> =&gt; <span style="color: #eb7300;">"aext_numcomnews"</span>,
    <span style="color: #eb7300;">"default"</span> =&gt; <span style="color: #eb7300;">"10"</span>),

);

<span style="color: #dd0000;">?&gt;</span></pre>
<p>The code to display:</p>
<pre class="php"> <span style="color: #881280;">&lt;div </span><span style="color: #994500;">class</span><span style="color: #881280;">=</span><span style="color: #1a1aa6;">"stuffbox"</span><span style="color: #881280;">&gt;</span>
  <span style="color: #881280;">&lt;h3&gt;</span>Homepage Setting<span style="color: #881280;">&lt;/h3&gt;</span>
  <span style="color: #881280;">&lt;div </span><span style="color: #994500;">class</span><span style="color: #881280;">=</span><span style="color: #1a1aa6;">"inside"</span><span style="color: #881280;">&gt;</span>
    <span style="color: #881280;">&lt;table </span><span style="color: #994500;">class</span><span style="color: #881280;">=</span><span style="color: #1a1aa6;">"form-table"</span><span style="color: #994500;">style</span><span style="color: #881280;">=</span><span style="color: #1a1aa6;">"width: auto"</span><span style="color: #881280;">&gt;</span>
    <span style="color: #dd0000;">&lt;?php</span>
     <span style="color: #881350;">foreach</span>(<span style="color: #825900;">$options</span> <span style="color: #881350;">as</span> <span style="color: #825900;">$value</span>) {
      <span style="color: #881350;">switch</span>( <span style="color: #825900;">$value</span>[<span style="color: #eb7300;">'id'</span>] ) {
        <span style="color: #881350;">case</span> <span style="color: #eb7300;">"aext_numcomnews"</span>: <span style="color: #dd0000;">?&gt;</span>
        <span style="color: #881280;">&lt;tr&gt;</span>
        <span style="color: #881280;">&lt;th </span><span style="color: #994500;">scope</span><span style="color: #881280;">=</span><span style="color: #1a1aa6;">"row"</span><span style="color: #881280;">&gt;</span><span style="color: #dd0000;">&lt;?php</span> <span style="color: #881350;">echo</span> <span style="color: #825900;">$value</span>[<span style="color: #eb7300;">'name'</span>]; <span style="color: #dd0000;">?&gt;</span><span style="color: #881280;">&lt;br /&gt;</span><span style="color: #dd0000;">&lt;?php</span> <span style="color: #881350;">echo</span> <span style="color: #825900;">$value</span>[<span style="color: #eb7300;">'desc'</span>]; <span style="color: #dd0000;">?&gt;</span><span style="color: #881280;">&lt;/th&gt;</span>
        <span style="color: #881280;">&lt;td&gt;</span>
         <span style="color: #881280;">&lt;input </span><span style="color: #994500;">name</span><span style="color: #881280;">=</span><span style="color: #1a1aa6;">"</span><span style="color: #dd0000;">&lt;?php</span> <span style="color: #881350;">echo</span> <span style="color: #825900;">$value</span>[<span style="color: #eb7300;">'id'</span>]; <span style="color: #dd0000;">?&gt;</span><span style="color: #1a1aa6;">"</span><span style="color: #881280;">                 </span><span style="color: #994500;">id</span><span style="color: #881280;">=</span><span style="color: #1a1aa6;">"</span><span style="color: #dd0000;">&lt;?php</span> <span style="color: #881350;">echo</span> <span style="color: #825900;">$value</span>[<span style="color: #eb7300;">'id'</span>]; <span style="color: #dd0000;">?&gt;</span><span style="color: #1a1aa6;">"</span><span style="color: #994500;">type</span><span style="color: #881280;">=</span><span style="color: #1a1aa6;">"text"</span><span style="color: #881280;">                 </span><span style="color: #994500;">value</span><span style="color: #881280;">=</span><span style="color: #1a1aa6;">"</span><span style="color: #dd0000;">&lt;?php</span> <span style="color: #003369;">get_option</span>(<span style="color: #825900;">$value</span>[<span style="color: #eb7300;">'id'</span>])?<span style="color: #661aa9;">printf</span>(<span style="color: #003369;">get_option</span>(<span style="color: #825900;">$value</span>[<span style="color: #eb7300;">'id'</span>])): <span style="color: #661aa9;">printf</span>(<span style="color: #825900;">$value</span>[<span style="color: #eb7300;">'default'</span>]) <span style="color: #dd0000;">?&gt;</span><span style="color: #1a1aa6;">"</span><span style="color: #881280;"> /&gt;</span>
        <span style="color: #881280;">&lt;/td&gt;</span>
        <span style="color: #881280;">&lt;/tr&gt;</span>

      <span style="color: #dd0000;">&lt;?php</span> <span style="color: #881350;">break</span>;
        }
    }
    <span style="color: #dd0000;">?&gt;</span>
   <span style="color: #881280;">&lt;/table&gt;</span>
  <span style="color: #881280;">&lt;/div&gt;</span>
 <span style="color: #881280;">&lt;/div&gt;</span></pre>
<p>And, here is the result &#8230;</p>
<div class="bigimage"><img class="alignnone size-full wp-image-944" title="get_option - display in theme setting page" src="http://aext.net/wp-contents/uploads/2009/10/get_option-display-in-theme-setting-page1.jpg" alt="get_option - display in theme setting page" width="500" height="200" /></div>
<h3>Where&#8217;s the Theme Setting Page?</h3>
<hr />
<p>We already write a lot of code for theme setting page, but we can not edit/update the option without display page. Let&#8217;s see how to add a link which link to theme setting page.<br />
At first, you have to understand what your functions.php looks like. To make it easy, the name and the content of this functions.php are:</p>
<pre class="php"><span style="color: #dd0000;">&lt;?php</span>

<span style="color: #825900;">$options</span> = <span style="color: #881350;">array</span>(

  <span style="color: #236e25;"><em>//All your options will be declared here </em></span>   
);

<span style="color: #236e25;"><em>//This function is used for processing </em></span><span style="color: #881350;">function</span> <span style="color: #003369;">themename_options</span>() {
  <span style="color: #881350;">global</span> <span style="color: #825900;">$options</span>;

    <span style="color: #003369;">add_theme_page</span>(<span style="color: #eb7300;">'Theme Setting'</span>, <span style="color: #eb7300;">'Theme Setting'</span>, <span style="color: #0000ff;">10</span>, <span style="color: #eb7300;">'themename-settings'</span>, <span style="color: #eb7300;">'themename_admin'</span>);
}

<span style="color: #236e25;"><em>//All the code of displaying will be here </em></span><span style="color: #881350;">function</span> <span style="color: #003369;">themename_admin</span>() {
    <span style="color: #881350;">global</span> <span style="color: #825900;">$options</span>;

}

<span style="color: #003369;">add_action</span>(<span style="color: #eb7300;">'admin_menu'</span>, <span style="color: #eb7300;">'themename_options'</span>);

<span style="color: #dd0000;">?&gt;</span></pre>
<p>The <strong>add_theme_page()</strong> function is used to add a page to a add a page to your WP admin. Here it is, this page is your theme setting page. <strong>The third parameter</strong> is included as unique URL to this page. Example with this URL below is the link to a theme setting page:</p>
<pre class="html">http://localhost/wp-admin/themes.php?page=themename-settings</pre>
<p>The <strong>add_action()</strong> function is used to hook function themename_option() and create a menu item in admin menu. You cannot access your theme setting page without this function.</p>
<p>You know what? The menu item that link to this page can be added with two different type. The <strong>add_theme_page()</strong> above will add an item into Theme Menu. You can use this code below to add a separated menu item as another parent menu item.</p>
<p>Replace the <strong>add_theme_page()</strong> by <strong>add_menu_page()</strong> with the same parameters:</p>
<pre class="php">    <span style="color: #003369;">add_menu_page</span>(<span style="color: #eb7300;">'Theme Setting'</span>, <span style="color: #eb7300;">'Theme Setting'</span>, <span style="color: #0000ff;">10</span>, <span style="color: #eb7300;">'themename-settings'</span>, <span style="color: #eb7300;">'themename_admin'</span>);</pre>
<p>Take a look:</p>
<div class="bigimage"><img class="alignnone size-full wp-image-951" title="Menu Item Style for Theme Setting Page" src="http://aext.net/wp-contents/uploads/2009/10/Menu-Item-Style-for-Theme-Setting-Page.jpg" alt="Menu Item Style for Theme Setting Page" width="500" height="357" /></div>
<h3>Complete the setting page code</h3>
<hr />
<p>You need more works to make your setting page run smooth. Now, we will combine all the code together. First code block that we start completing code is option array:</p>
<pre class="php"><em>// theme options </em><span style="color: #825900;">$options</span> = <span style="color: #881350;">array</span>(

  <span style="color: #881350;">array</span>(    
    <span style="color: #eb7300;">"name"</span> =&gt; <span style="color: #eb7300;">"&lt;strong&gt;Homepage Community Entries&lt;/strong&gt;"</span>,
    <span style="color: #eb7300;">"desc"</span> =&gt; <span style="color: #eb7300;">"Select the number of Community entries that should appear on the Index page"</span>,
    <span style="color: #eb7300;">"id"</span> =&gt; <span style="color: #eb7300;">"aext_numcomnews"</span>,
    <span style="color: #eb7300;">"default"</span> =&gt; <span style="color: #eb7300;">"10"</span>),

  <span style="color: #881350;">array</span>(
   <span style="color: #eb7300;">"id"</span> =&gt; <span style="color: #eb7300;">"aext_footer"</span>,
   <span style="color: #eb7300;">"default"</span> =&gt; <span style="color: #eb7300;">""</span>),

);</pre>
<p>In this tutorial, I just use two options to make it easy to look. We have two options above, right? But we just need to focus on the value of <strong>id</strong> of each array, because the code below will get value of <strong>id</strong> and we can identify what option we update by <strong>unique id</strong>.</p>
<pre class="php"><span style="color: #dd0000;">&lt;?php</span>

.....

<span style="color: #881350;">function</span> <span style="color: #003369;">themename_admin</span>() {
    <span style="color: #881350;">global</span> <span style="color: #825900;">$options</span>;
<span style="color: #dd0000;">?&gt;</span>
<span style="color: #881280;">&lt;div </span><span style="color: #994500;">class</span><span style="color: #881280;">=</span><span style="color: #1a1aa6;">"wrap"</span><span style="color: #881280;">&gt;</span>
  <span style="color: #881280;">&lt;h2 </span><span style="color: #994500;">class</span><span style="color: #881280;">=</span><span style="color: #1a1aa6;">"alignleft"</span><span style="color: #881280;">&gt;</span>Theme Setting<span style="color: #881280;">&lt;/h2&gt;</span>
  <span style="color: #881280;">&lt;br </span><span style="color: #994500;">clear</span><span style="color: #881280;">=</span><span style="color: #1a1aa6;">"all"</span><span style="color: #881280;"> /&gt;</span>

  <span style="color: #dd0000;">&lt;?php</span>
    <span style="color: #236e25;"><em>//Check if settings saved! </em></span>    <span style="color: #881350;">if</span>( <span style="color: #825900;">$_REQUEST</span>[<span style="color: #eb7300;">'saved'</span>] ) {
  <span style="color: #dd0000;">?&gt;</span>
        <span style="color: #881280;">&lt;div </span><span style="color: #994500;">class</span><span style="color: #881280;">=</span><span style="color: #1a1aa6;">"updated fade"</span><span style="color: #881280;">&gt;&lt;p&gt;&lt;strong&gt;</span>Setting Saved<span style="color: #881280;">&lt;/strong&gt;&lt;/p&gt;&lt;/div&gt;</span>
  <span style="color: #dd0000;">&lt;?php</span> } <span style="color: #dd0000;">?&gt;</span>

<span style="color: #881280;">&lt;form </span><span style="color: #994500;">method</span><span style="color: #881280;">=</span><span style="color: #1a1aa6;">"post"</span><span style="color: #994500;">id</span><span style="color: #881280;">=</span><span style="color: #1a1aa6;">"myForm"</span><span style="color: #881280;">&gt;</span>
<span style="color: #881280;">&lt;div </span><span style="color: #994500;">id</span><span style="color: #881280;">=</span><span style="color: #1a1aa6;">"poststuff"</span><span style="color: #994500;">class</span><span style="color: #881280;">=</span><span style="color: #1a1aa6;">"metabox-holder"</span><span style="color: #881280;">&gt;</span>

 <span style="color: #881280;">&lt;div </span><span style="color: #994500;">class</span><span style="color: #881280;">=</span><span style="color: #1a1aa6;">"stuffbox"</span><span style="color: #881280;">&gt;</span>
  <span style="color: #881280;">&lt;h3&gt;</span>Homepage Setting<span style="color: #881280;">&lt;/h3&gt;</span>
  <span style="color: #881280;">&lt;div </span><span style="color: #994500;">class</span><span style="color: #881280;">=</span><span style="color: #1a1aa6;">"inside"</span><span style="color: #881280;">&gt;</span>
    <span style="color: #881280;">&lt;table </span><span style="color: #994500;">class</span><span style="color: #881280;">=</span><span style="color: #1a1aa6;">"form-table"</span><span style="color: #994500;">style</span><span style="color: #881280;">=</span><span style="color: #1a1aa6;">"width: auto"</span><span style="color: #881280;">&gt;</span>
    <span style="color: #dd0000;">&lt;?php</span>
     <span style="color: #881350;">foreach</span>(<span style="color: #825900;">$options</span> <span style="color: #881350;">as</span> <span style="color: #825900;">$value</span>) {
      <span style="color: #881350;">switch</span>( <span style="color: #825900;">$value</span>[<span style="color: #eb7300;">'id'</span>] ) {
        <span style="color: #881350;">case</span> <span style="color: #eb7300;">"aext_numcomnews"</span>: <span style="color: #dd0000;">?&gt;</span>
        <span style="color: #881280;">&lt;tr&gt;</span>
        <span style="color: #881280;">&lt;th </span><span style="color: #994500;">scope</span><span style="color: #881280;">=</span><span style="color: #1a1aa6;">"row"</span><span style="color: #881280;">&gt;</span><span style="color: #dd0000;">&lt;?php</span> <span style="color: #881350;">echo</span> <span style="color: #825900;">$value</span>[<span style="color: #eb7300;">'name'</span>]; <span style="color: #dd0000;">?&gt;</span><span style="color: #881280;">&lt;br /&gt;</span><span style="color: #dd0000;">&lt;?php</span> <span style="color: #881350;">echo</span> <span style="color: #825900;">$value</span>[<span style="color: #eb7300;">'desc'</span>]; <span style="color: #dd0000;">?&gt;</span><span style="color: #881280;">&lt;/th&gt;</span>
        <span style="color: #881280;">&lt;td&gt;</span>
         <span style="color: #881280;">&lt;input </span><span style="color: #994500;">name</span><span style="color: #881280;">=</span><span style="color: #1a1aa6;">"</span><span style="color: #dd0000;">&lt;?php</span> <span style="color: #881350;">echo</span> <span style="color: #825900;">$value</span>[<span style="color: #eb7300;">'id'</span>]; <span style="color: #dd0000;">?&gt;</span><span style="color: #1a1aa6;">"</span><span style="color: #881280;">                 </span><span style="color: #994500;">id</span><span style="color: #881280;">=</span><span style="color: #1a1aa6;">"</span><span style="color: #dd0000;">&lt;?php</span> <span style="color: #881350;">echo</span> <span style="color: #825900;">$value</span>[<span style="color: #eb7300;">'id'</span>]; <span style="color: #dd0000;">?&gt;</span><span style="color: #1a1aa6;">"</span><span style="color: #994500;">type</span><span style="color: #881280;">=</span><span style="color: #1a1aa6;">"text"</span><span style="color: #881280;">                 </span><span style="color: #994500;">value</span><span style="color: #881280;">=</span><span style="color: #1a1aa6;">"</span><span style="color: #dd0000;">&lt;?php</span> <span style="color: #003369;">get_option</span>(<span style="color: #825900;">$value</span>[<span style="color: #eb7300;">'id'</span>])?<span style="color: #661aa9;">printf</span>(<span style="color: #003369;">get_option</span>(<span style="color: #825900;">$value</span>[<span style="color: #eb7300;">'id'</span>])): <span style="color: #661aa9;">printf</span>(<span style="color: #825900;">$value</span>[<span style="color: #eb7300;">'default'</span>]) <span style="color: #dd0000;">?&gt;</span><span style="color: #1a1aa6;">"</span><span style="color: #881280;"> /&gt;</span>
        <span style="color: #881280;">&lt;/td&gt;</span>
        <span style="color: #881280;">&lt;/tr&gt;</span>

      <span style="color: #dd0000;">&lt;?php</span> <span style="color: #881350;">break</span>;
        <span style="color: #881350;">case</span> <span style="color: #eb7300;">"aext_footer"</span>: <span style="color: #dd0000;">?&gt;</span>
        <span style="color: #881280;">&lt;tr&gt;</span>
          <span style="color: #881280;">&lt;th </span><span style="color: #994500;">scope</span><span style="color: #881280;">=</span><span style="color: #1a1aa6;">"row"</span><span style="color: #881280;">&gt;</span>
            <span style="color: #881280;">&lt;strong&gt;</span>Footer HTML<span style="color: #881280;">&lt;/strong&gt;</span>
            <span style="color: #881280;">&lt;br /&gt;</span>Put some HTML code here. That's helpful if you want to add tracking code ....
          <span style="color: #881280;">&lt;/th&gt;</span>
          <span style="color: #881280;">&lt;td&gt;</span>
            <span style="color: #881280;">&lt;textarea </span><span style="color: #994500;">class</span><span style="color: #881280;">=</span><span style="color: #1a1aa6;">"code"</span><span style="color: #994500;">rows</span><span style="color: #881280;">=</span><span style="color: #1a1aa6;">"4"</span><span style="color: #994500;">cols</span><span style="color: #881280;">=</span><span style="color: #1a1aa6;">"60"</span><span style="color: #881280;">                         </span><span style="color: #994500;">name</span><span style="color: #881280;">=</span><span style="color: #1a1aa6;">"</span><span style="color: #dd0000;">&lt;?php</span> <span style="color: #881350;">echo</span> <span style="color: #825900;">$value</span>[<span style="color: #eb7300;">'id'</span>]; <span style="color: #dd0000;">?&gt;</span><span style="color: #1a1aa6;">"</span><span style="color: #881280;">                         </span><span style="color: #994500;">id</span><span style="color: #881280;">=</span><span style="color: #1a1aa6;">"</span><span style="color: #dd0000;">&lt;?php</span> <span style="color: #881350;">echo</span> <span style="color: #825900;">$value</span>[<span style="color: #eb7300;">'id'</span>]; <span style="color: #dd0000;">?&gt;</span><span style="color: #1a1aa6;">"</span><span style="color: #881280;">&gt;</span><span style="color: #dd0000;">&lt;?php</span> <span style="color: #881350;">print</span> <span style="color: #003369;">get_option</span>(<span style="color: #825900;">$value</span>[<span style="color: #eb7300;">'id'</span>]); <span style="color: #dd0000;">?&gt;</span>
            <span style="color: #881280;">&lt;/textarea&gt;</span>
          <span style="color: #881280;">&lt;/td&gt;</span>
        <span style="color: #881280;">&lt;/tr&gt;</span>

      <span style="color: #dd0000;">&lt;?php</span> <span style="color: #881350;">break</span>;
        }
    }
    <span style="color: #dd0000;">?&gt;</span>
   <span style="color: #881280;">&lt;/table&gt;</span>
  <span style="color: #881280;">&lt;/div&gt;</span>
 <span style="color: #881280;">&lt;/div&gt;</span>  

<span style="color: #881280;">&lt;/div&gt;</span>
<span style="color: #881280;">&lt;input </span><span style="color: #994500;">name</span><span style="color: #881280;">=</span><span style="color: #1a1aa6;">"theme_save"</span><span style="color: #994500;">type</span><span style="color: #881280;">=</span><span style="color: #1a1aa6;">"submit"</span><span style="color: #994500;">class</span><span style="color: #881280;">=</span><span style="color: #1a1aa6;">"button-primary"</span><span style="color: #994500;">value</span><span style="color: #881280;">=</span><span style="color: #1a1aa6;">"Save changes"</span><span style="color: #881280;"> /&gt;</span>
<span style="color: #881280;">&lt;input </span><span style="color: #994500;">type</span><span style="color: #881280;">=</span><span style="color: #1a1aa6;">"hidden"</span><span style="color: #994500;">name</span><span style="color: #881280;">=</span><span style="color: #1a1aa6;">"action"</span><span style="color: #994500;">value</span><span style="color: #881280;">=</span><span style="color: #1a1aa6;">"theme_save"</span><span style="color: #881280;"> /&gt;</span>
<span style="color: #881280;">&lt;/form&gt;</span>

<span style="color: #881280;">&lt;/div&gt;</span>
<span style="color: #dd0000;">&lt;?php</span>
}

.....

<span style="color: #dd0000;">?&gt;</span></pre>
<p>At the top of this block of code above, we check the value of the request <strong>saved</strong>. The text will displays to notify that setting was saved successful. The form will load default value of id: <strong>aext_numcomnews</strong> if there are no value of <strong>aext_numcomnews</strong> in database.</p>
<p>That is the form display. We need to process the value that the form submitted. We do it inside themename_options() function:</p>
<pre class="php"><span style="color: #dd0000;">&lt;?php</span>

<span style="color: #881350;">function</span> <span style="color: #003369;">themename_options</span>() {
  <span style="color: #881350;">global</span> <span style="color: #825900;">$options</span>;

  <span style="color: #881350;">if</span>(<span style="color: #eb7300;">'theme_save'</span>== <span style="color: #825900;">$_REQUEST</span>[<span style="color: #eb7300;">'action'</span>] ) {

    <span style="color: #881350;">foreach</span>(<span style="color: #825900;">$options</span> <span style="color: #881350;">as</span> <span style="color: #825900;">$value</span>) {
     <span style="color: #881350;">if</span>( !<span style="color: #003369;">isset</span>( <span style="color: #825900;">$_REQUEST</span>[ <span style="color: #825900;">$value</span>[<span style="color: #eb7300;">'id'</span>] ] ) ) {  
        <span style="color: #236e25;"><em>//Do nothing </em></span>     } <span style="color: #881350;">else</span> {
        <span style="color: #003369;">update_option</span>( <span style="color: #825900;">$value</span>[<span style="color: #eb7300;">'id'</span>], <span style="color: #661aa9;">stripslashes</span>(<span style="color: #825900;">$_REQUEST</span>[ <span style="color: #825900;">$value</span>[<span style="color: #eb7300;">'id'</span>]])); }
     }

     <span style="color: #881350;">if</span>(<span style="color: #661aa9;">stristr</span>(<span style="color: #825900;">$_SERVER</span>[<span style="color: #eb7300;">'REQUEST_URI'</span>],<span style="color: #eb7300;">'&amp;saved=true'</span>)) {
        <span style="color: #825900;">$location</span> = <span style="color: #825900;">$_SERVER</span>[<span style="color: #eb7300;">'REQUEST_URI'</span>];
     } <span style="color: #881350;">else</span> {
        <span style="color: #825900;">$location</span> = <span style="color: #825900;">$_SERVER</span>[<span style="color: #eb7300;">'REQUEST_URI'</span>] . <span style="color: #eb7300;">"&amp;saved=true"</span>;
     }

     <span style="color: #661aa9;">header</span>(<span style="color: #eb7300;">"Location: </span><span style="color: #c94b16;">$location</span><span style="color: #eb7300;">"</span>);
     <span style="color: #881350;">die</span>;
  }

  <span style="color: #003369;">add_theme_page</span>(<span style="color: #eb7300;">'AEXT Theme Setting'</span>, <span style="color: #eb7300;">'AEXT Theme Setting'</span>, <span style="color: #0000ff;">10</span>, <span style="color: #eb7300;">'aext-settings'</span>, <span style="color: #eb7300;">'aext_admin'</span>);
}

<span style="color: #dd0000;">?&gt;</span></pre>
<p>That function processes the data submitted by <strong>update_option()</strong> with parameter is the value of each <strong>id</strong> was changed. After submit your changes, your all changes will be made in table <strong>wp_options</strong> in your database.</p>
<p>At last, remember to put the line of code here at the end of your <strong>functions.php</strong> file:</p>
<pre class="php"><span style="color: #003369;">add_action</span>(<span style="color: #eb7300;">'admin_menu'</span>, <span style="color: #eb7300;">'themename_options'</span>);</pre>
<p>If you want to rest your current options to default value, simply copy and paste these code next to your current form. The reason is we can not submit two value <strong>theme_save</strong> and <strong>theme_reset</strong> at same time.</p>
<pre class="html"><span style="color: #881280;">&lt;form </span><span style="color: #994500;">method</span><span style="color: #881280;">=</span><span style="color: #1a1aa6;">"post"</span><span style="color: #881280;">&gt;</span>
    <span style="color: #881280;">&lt;p </span><span style="color: #994500;">class</span><span style="color: #881280;">=</span><span style="color: #1a1aa6;">"submit"</span><span style="color: #994500;">style</span><span style="color: #881280;">=</span><span style="color: #1a1aa6;">"border: 0;"</span><span style="color: #881280;">&gt;</span>
       <span style="color: #881280;">&lt;input </span><span style="color: #994500;">name</span><span style="color: #881280;">=</span><span style="color: #1a1aa6;">"reset"</span><span style="color: #994500;">type</span><span style="color: #881280;">=</span><span style="color: #1a1aa6;">"submit"</span><span style="color: #994500;">value</span><span style="color: #881280;">=</span><span style="color: #1a1aa6;">"Reset"</span><span style="color: #881280;"> /&gt;</span>
       <span style="color: #881280;">&lt;input </span><span style="color: #994500;">type</span><span style="color: #881280;">=</span><span style="color: #1a1aa6;">"hidden"</span><span style="color: #994500;">name</span><span style="color: #881280;">=</span><span style="color: #1a1aa6;">"action"</span><span style="color: #994500;">value</span><span style="color: #881280;">=</span><span style="color: #1a1aa6;">"theme_reset"</span><span style="color: #881280;"> /&gt;</span>
    <span style="color: #881280;">&lt;/p&gt;</span>
<span style="color: #881280;">&lt;/form&gt;</span></pre>
<p>And edit your theme_options() funtion like this:</p>
<pre class="php">  <span style="color: #881350;">if</span>(<span style="color: #eb7300;">'theme_save'</span>== <span style="color: #825900;">$_REQUEST</span>[<span style="color: #eb7300;">'action'</span>] ) {

    .....

  } <span style="color: #881350;">else</span> <span style="color: #881350;">if</span>(<span style="color: #eb7300;">'theme_reset'</span> == <span style="color: #825900;">$_REQUEST</span>[<span style="color: #eb7300;">'action'</span>] ) {

    <span style="color: #881350;">foreach</span>(<span style="color: #825900;">$options</span> <span style="color: #881350;">as</span> <span style="color: #825900;">$value</span>) {
     <span style="color: #003369;">delete_option</span>( <span style="color: #825900;">$value</span>[<span style="color: #eb7300;">'id'</span>] );
     <span style="color: #825900;">$location</span> = <span style="color: #825900;">$_SERVER</span>[<span style="color: #eb7300;">'REQUEST_URI'</span>] . <span style="color: #eb7300;">"&amp;reset=true"</span>;
    }

    <span style="color: #661aa9;">header</span>(<span style="color: #eb7300;">"Location: </span><span style="color: #c94b16;">$location</span><span style="color: #eb7300;">"</span>);
    <span style="color: #881350;">die</span>;

  }</pre>
<h3>Get your options in your theme files</h3>
<hr />
<p>It&#8217;s as easy as the code at the top of this entry. Because all of your options are stored in database, we will get values for a named option from the options database table. If the desired option does not exist, return value is <strong>FASLE</strong>.</p>
<p>In my index.php file:</p>
<pre class="php">
......

<span style="color: #dd0000;">&lt;?php</span> 

    <span style="color: #825900;">$numcomnews</span> = <span style="color: #003369;">get_option</span>(<span style="color: #eb7300;">'aext_numcomnews'</span>);

    <span style="color: #003369;">query_posts</span>(<span style="color: #eb7300;">"category_name=Community News&amp;showposts=</span><span style="color: #c94b16;">$numcomnews</span><span style="color: #eb7300;">"</span>);

    <span style="color: #881350;">if</span>(<span style="color: #003369;">have_posts</span>()) :

    <span style="color: #236e25;"><em>//Displaying posts </em></span>    
    <span style="color: #881350;">endif</span>;

<span style="color: #dd0000;">?&gt;</span>

......</pre>
<h3>What&#8217;s up?</h3>
<hr />
<p>The completed code is attached in this post, you can download it for use later. However, I suggest that you follow step by step to help you get all what I want to say. This tutorial comes along with only two options, you can add more options to customize your own wordpress template. Although these code is working, maybe I am doing something wrong. Feel free to comment, I&#8217;ll really appreciate all you give to me!</p>
<div class="txtad-cont"></div>
]]></content:encoded>
			<wfw:commentRss>http://aext.net/2009/10/wordpress-theme-design-with-options-adminstration/feed/</wfw:commentRss>
		<slash:comments>76</slash:comments>
		</item>
		<item>
		<title>Learning jQuery: Submit form PHP Mac style Modal window</title>
		<link>http://aext.net/2009/09/learning-jquery-submit-form-php-mac-style-modal-window/</link>
		<comments>http://aext.net/2009/09/learning-jquery-submit-form-php-mac-style-modal-window/#comments</comments>
		<pubDate>Sat, 05 Sep 2009 16:00:56 +0000</pubDate>
		<dc:creator>Lief</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[CSS & XHTML]]></category>
		<category><![CDATA[DeskShade]]></category>
		<category><![CDATA[Form]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[Learning jQuery]]></category>
		<category><![CDATA[Login]]></category>
		<category><![CDATA[Modal Window]]></category>
		<category><![CDATA[POST]]></category>
		<category><![CDATA[Rquest]]></category>

		<guid isPermaLink="false">http://aext.net/?p=622</guid>
		<description><![CDATA[<p><a href="http://aext.net/theme/platinoom" title="Platinoom - Premium WordPress Themes"><img src="http://aext.net/wp-content/uploads/2011/04/platinoom_wordpress_theme.png" alt="ThemeKiss" /></a></p><p style="float: right;"><a href="http://api.tweetmeme.com/share?url=http://aext.net/2009/09/learning-jquery-submit-form-php-mac-style-modal-window/&service=su.pr&service_api=b57727e991c454bd2b2c62ff71462c79"><img src="http://api.tweetmeme.com/imagebutton.gif?url=http://aext.net/2009/09/learning-jquery-submit-form-php-mac-style-modal-window/" height="61" width="51" /></a></p>]]></description>
			<content:encoded><![CDATA[<p><a href="http://aext.net/theme/platinoom" title="Platinoom - Premium WordPress Themes"><img src="http://aext.net/wp-content/uploads/2011/04/platinoom_wordpress_theme.png" alt="ThemeKiss" /></a></p><p style="float: right;"><a href="http://api.tweetmeme.com/share?url=http://aext.net/2009/09/learning-jquery-submit-form-php-mac-style-modal-window/&service=su.pr&service_api=b57727e991c454bd2b2c62ff71462c79"><img src="http://api.tweetmeme.com/imagebutton.gif?url=http://aext.net/2009/09/learning-jquery-submit-form-php-mac-style-modal-window/" height="61" width="51" /></a></p><p>In my previous post, I show you how to make up the login form likes Mac app <a href="http://www.macrabbit.com/deskshade/">DeskShade</a>. Today, this entry is the next part of that post. This tutorial will help you submit that form with PHP handler. Furthermore, with some effects come along, your form will be nicer. I would like to name this post as &#8220;Learning jQuery&#8230;&#8221; in the series of jQuery Learning posts because in each post, I will focus on one or two functions of jQuery, and after reading, I&#8217;m sure you&#8217;ll be cool. If you&#8217;re new to jQuery, you should not miss!<br />
<span id="more-622"></span></p>
<p><span id="more-34"> </span></p>
<p>Before start this tutorial, I would like to forward you a link to this demo. You can download this source code to see how simple it is. Default login is username: admin, password: 123456</p>
<div class="demo-download">
<ul>
<li><a class="downloadit" href="http://www.box.net/shared/5jels3y5am">Download</a></li>
<li><a class="demoit" href="http://aext.net/example/loginmac/">Demo</a></li>
</ul>
</div>
<div class="bigimage"><img class="alignnone size-full wp-image-461" title="Learning jQuery: Submit form PHP Mac style Modal window" src="http://aext.net/wp-contents/uploads/2009/08/demo.jpg" alt="Learning jQuery: Submit form PHP Mac style Modal window" width="500" height="246" /></div>
<p>Maybe you have not read my previous entry that&#8217;s first part of this one today. I suggest you read it first before continue because I&#8217;m not gonna talk about that again. Check this post <a href="http://aext.net/2009/08/learning-jquery-click-event-with-locked-page-likes-mac/">Learning jQuery: Click Event with Locked Page likes Mac</a>! then come back later <img src='http://aext.net/wp-includes/images/smilies/icon_biggrin.gif' alt=':D' class='wp-smiley' /> </p>
<h2>1. HTML &amp; CSS code</h2>
<hr />I forgot I wont talk about it. Please click <a href="http://aext.net/2009/08/learning-jquery-click-event-with-locked-page-likes-mac/">here</a> to get the HTML and CSS code. Some update will be below.</p>
<h2>2. Javascript</h2>
<hr />Because I&#8217;ve made some changes with javascript code, I will show you the differences. I wrote all the javascript code into one file, and below it&#8217;s explaining each javascript code block.</p>
<p><strong>Validate the form:</strong></p>
<p>I just write some code to validate the form if it&#8217;s blank. You will see the code how it simple. If you want to check more with this form, you can write yourself with same as below.</p>
<pre class="javscript">Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â <span style="color:#236e25;"><em>//Validate the Username field if it's blank
</em></span>Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â <span style="color:#881350;">var</span> username = $(<span style="color:#760f15;">"input#username"</span>).<span style="color:#003369;">val</span>();
Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â <span style="color:#881350;">if</span><span style="color:#003369;"> </span>(username == <span style="color:#760f15;">""</span>) {
Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â $(<span style="color:#760f15;">'#message'</span>).<span style="color:#003369;">html</span>(<span style="color:#760f15;">"All the fields are required"</span>);
Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â $(<span style="color:#760f15;">"#message"</span>).<span style="color:#003369;">hide</span>().<span style="color:#003369;">fadeIn</span>(<span style="color:#0000ff;">1500</span>);
Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â $(<span style="color:#760f15;">"input#username"</span>).<span style="color:#003369;">focus</span>();
Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â <span style="color:#881350;">return</span> <span style="color:#881350;">false</span>;
Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â }

Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â <span style="color:#236e25;"><em>//Validate the Password field if it's blank
</em></span>Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â <span style="color:#881350;">var</span> password = $(<span style="color:#760f15;">"input#password"</span>).<span style="color:#003369;">val</span>();
Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â <span style="color:#881350;">if</span><span style="color:#003369;"> </span>(password == <span style="color:#760f15;">""</span>) {
Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â $(<span style="color:#760f15;">'#message'</span>).<span style="color:#003369;">html</span>(<span style="color:#760f15;">"All the fields are required"</span>);
Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â $(<span style="color:#760f15;">"#message"</span>).<span style="color:#003369;">hide</span>().<span style="color:#003369;">fadeIn</span>(<span style="color:#0000ff;">1500</span>);
Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â $(<span style="color:#760f15;">"input#password"</span>).<span style="color:#003369;">focus</span>();
Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â <span style="color:#881350;">return</span> <span style="color:#881350;">false</span>;
Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â }</pre>
<p><strong>Post File</strong></p>
<p>Let&#8217;s set the file that the form will send a simple POST request to.</p>
<pre class="javascript">Â Â Â Â Â Â Â Â Â Â Â Â <span style="color:#236e25;"><em>// POST handler
</em></span>Â Â Â Â Â Â Â Â Â Â Â Â <span style="color:#881350;">var</span> postFile Â Â Â = Â Â <span style="color:#760f15;">'bin/doLogin.php'</span></pre>
<p><strong>Submit the form<br />
</strong></p>
<p>If the form is valid, then submit it to postFile. At first, I would like to talk about using POST request with jQuery. The $.post will be something likes this:</p>
<pre>$.post(url, data, callback, type);</pre>
<ul class="mylist">
<li><strong>url</strong>: The URL of the page to load.</li>
<li><strong>data</strong>: Key/value pairs or the return value of the .serialize() function that will be sent to the server. (Optional)</li>
<li><strong>callback</strong>: A function to be executed whenever the data is loaded successfully. (Optional)</li>
<li><strong>type</strong>: Type of data to be returned to callback function: &#8220;xml&#8221;, &#8220;html&#8221;, &#8220;script&#8221;, &#8220;json&#8221;, &#8220;jsonp&#8221;, or &#8220;text&#8221;.(Optional)</li>
</ul>
<p>In this tutorial, the form will get the doLogin.php page contents which has been returned in json format</p>
<pre class="javascript">Â Â Â Â Â  $.<span style="color:#003369;">post</span>(postFile, { usernamePost: username, passwordPost: password }, <span style="color:#881350;">function</span>(data) {
Â Â Â Â Â Â Â Â Â  <span style="color:#881350;">if</span>(data.status==<span style="color:#881350;">true</span>) {
Â Â Â Â Â Â Â Â Â Â Â Â Â  <span style="color:#236e25;"><em>//If return data is true, it means username and password are correct</em></span>
Â Â Â Â Â Â Â Â Â Â Â Â Â  window.location=data.url;
Â Â Â Â Â Â Â Â Â  } <span style="color:#881350;">else</span> {
Â Â Â Â Â Â Â Â Â Â Â Â   $(<span style="color:#760f15;">"#message"</span>).<span style="color:#003369;">html</span>(<span style="color:#760f15;">"Wrong Username or Password"</span>);
Â Â Â Â Â Â Â Â Â Â Â Â Â  $(<span style="color:#760f15;">"#message"</span>).<span style="color:#003369;">hide</span>().<span style="color:#003369;">fadeIn</span>(<span style="color:#0000ff;">1500</span>);
Â Â Â Â Â Â Â Â Â Â Â Â Â  $(<span style="color:#760f15;">"input#username"</span>).<span style="color:#003369;">focus</span>();
Â Â Â Â Â Â Â Â Â  }
Â Â Â Â Â  }, <span style="color:#760f15;">"json"</span>);

Â Â Â Â Â  <span style="color:#881350;">return</span> <span style="color:#881350;">false</span>;</pre>
<p>At the code above, when form was submitted, if return is true, browser will transfer to a destination page. If return is false, it&#8217;ll replace the text in span id #message by error message by fadeIn effect. Finally, focus mouse on username text field.</p>
<p>Cancel button was handled by closing the form with fadeOut effect and display the locked icon. I want to mention this part to who has never read previous post.</p>
<pre class="javascript">Â Â Â Â Â Â Â Â Â Â Â Â <span style="color:#236e25;"><em>// This is example of other button
</em></span>Â Â Â Â Â Â Â Â Â Â Â Â $(<span style="color:#760f15;">"input#cancel_submit"</span>).<span style="color:#003369;">click</span>(<span style="color:#881350;">function</span>(e) {
Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â $(<span style="color:#760f15;">"#loginform"</span>).<span style="color:#003369;">hide</span>();
Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â $(<span style="color:#760f15;">".lock"</span>).<span style="color:#003369;">fadeIn</span>();
Â Â Â Â Â Â Â Â Â Â Â Â });</pre>
<p><strong>Some effects that make the form cool</strong><br />
Add more script to add some cool effect to the form. If the form is valid and return with authorized account, login form will go up to the top and change opacity to 0. I dont how you like it but I want it delays 1 second after the form move up then set location for browser to destination. The timer plugin I&#8217;m using is <a href="http://www.evanbot.com/article/jquery-timer-plugin/23">jQuery Timer</a></p>
<pre class="javascript">Â Â Â Â Â  $.<span style="color:#003369;">post</span>(postFile, { usernamePost: username, passwordPost: password }, <span style="color:#881350;">function</span>(data) {
Â Â Â Â Â Â Â Â Â  <span style="color:#881350;">  if</span>(data.status==<span style="color:#881350;">true</span>) {
Â Â Â Â Â Â Â Â Â Â Â Â Â  <span style="color:#236e25;"><em>//If return data is true, it's mean username and password are correct
</em></span>
Â Â Â Â Â Â Â Â Â Â Â Â Â  <span style="color:#236e25;"><em>//The distance in pixel that the form will move from original location
</em></span>Â Â Â Â Â Â Â Â Â Â Â Â Â  <span style="color:#881350;">var</span> distance = <span style="color:#0000ff;">10</span>;

Â Â Â Â Â Â Â Â Â Â Â Â Â  <span style="color:#236e25;"><em>//The time that the form take to animate
</em></span>Â Â Â Â Â Â Â Â Â Â Â Â Â  <span style="color:#881350;">var</span> time = <span style="color:#0000ff;">500</span>;

Â Â Â Â Â Â Â Â Â Â Â Â Â  <span style="color:#236e25;"><em>// This will hold our timer
</em></span>Â Â Â Â Â Â Â Â Â Â Â Â Â  <span style="color:#881350;">var</span> myTimer = {};

Â Â Â Â Â Â Â Â Â Â Â Â Â  $(<span style="color:#760f15;">"#loginform"</span>).<span style="color:#003369;">animate</span>({
Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â  marginTop: <span style="color:#760f15;">'-='</span>+ distance +<span style="color:#760f15;">'px'</span>,
Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â  opacity: <span style="color:#0000ff;">0</span>
Â Â Â Â Â Â Â Â Â Â Â Â Â  }, time, <span style="color:#760f15;">'swing'</span>, <span style="color:#881350;">function</span><span style="color:#003369;"> </span>() {
Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â  $(<span style="color:#760f15;">"#loginform"</span>).<span style="color:#003369;">hide</span>();
Â Â Â Â Â Â Â Â Â Â Â Â Â  }); Â Â Â Â 

Â Â Â Â Â Â Â Â Â Â Â Â Â  <span style="color:#236e25;"><em>// Set the timer for 1 seconds
</em></span>Â Â Â Â Â Â Â Â Â Â Â Â Â  myTimer = $.<span style="color:#003369;">timer</span>(<span style="color:#0000ff;">1000</span>,<span style="color:#881350;">function</span>() {
Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â  window.location=data.url;
Â Â Â Â Â Â Â Â Â Â Â Â Â  });
Â Â Â Â Â Â Â Â Â  } <span style="color:#881350;">else</span> {
Â Â Â Â Â Â Â Â Â Â Â Â Â  $(<span style="color:#760f15;">"#message"</span>).<span style="color:#003369;">html</span>(<span style="color:#760f15;">"Wrong Username or Password"</span>);
Â Â Â Â Â Â Â Â Â Â Â Â Â  $(<span style="color:#760f15;">"#message"</span>).<span style="color:#003369;">hide</span>().<span style="color:#003369;">fadeIn</span>(<span style="color:#0000ff;">1500</span>);
Â Â Â Â Â Â Â Â Â Â Â Â Â  $(<span style="color:#760f15;">"input#username"</span>).<span style="color:#003369;">focus</span>();
Â Â Â Â Â Â Â Â Â  }
Â Â Â Â Â  }, <span style="color:#760f15;">"json"</span>);

Â Â Â Â Â  <span style="color:#881350;">return</span> <span style="color:#881350;">false</span>;</pre>
<p>After testing on Firefox and Safari, I decide to choose &#8220;marginTop&#8221; insted of &#8220;top&#8221; attribute. Although, this form is set position as absolute, It&#8217;s better if using &#8220;top&#8221; attribute but It&#8217;s get bug on Safari when moving likes flashing. So, just make it easier with marginTop. Sorry to IE users that I still have not tested on IE. Who get it with IE please give me some information.</p>
<h2>3. PHP</h2>
<hr />
PHP code, the POST handler is:</p>
<pre class="php"><span style="color:#dd0000;">&lt;?php</span>

Â Â Â Â <span style="color:#881350;">require</span>(<span style="color:#eb7300;">'config.php'</span>);
Â Â Â Â <span style="color:#881350;">require</span>(<span style="color:#eb7300;">'account.php'</span>);

Â Â Â Â <span style="color:#236e25;"><em>//Create new account from Account class to verify account was submitted
</em></span>Â Â Â Â <span style="color:#825900;">$account</span> = <span style="color:#881350;">new</span> Account;

Â Â Â Â <span style="color:#236e25;"><em>//Clean up the form submission
</em></span>Â Â Â Â <span style="color:#825900;">$username</span> = (!<span style="color:#003369;">empty</span>(<span style="color:#825900;">$_POST</span>[<span style="color:#eb7300;">'usernamePost'</span>]))?<span style="color:#661aa9;">trim</span>(<span style="color:#825900;">$_POST</span>[<span style="color:#eb7300;">'usernamePost'</span>]):<span style="color:#881350;">false</span>;
Â Â Â Â <span style="color:#825900;">$password</span> = (!<span style="color:#003369;">empty</span>(<span style="color:#825900;">$_POST</span>[<span style="color:#eb7300;">'passwordPost'</span>]))?<span style="color:#661aa9;">trim</span>(<span style="color:#825900;">$_POST</span>[<span style="color:#eb7300;">'passwordPost'</span>]):<span style="color:#881350;">false</span>;

Â Â Â Â <span style="color:#236e25;"><em>//Signin will be here
</em></span>Â Â Â Â <span style="color:#825900;">$verify</span> = <span style="color:#825900;">$account</span>-&gt;<span style="color:#003369;">login</span>(<span style="color:#825900;">$username</span>,<span style="color:#825900;">$password</span>);

Â Â Â Â <span style="color:#881350;">if</span>(<span style="color:#825900;">$verify</span>) {
Â Â Â Â Â Â Â Â <span style="color:#236e25;"><em>//Login Successful
</em></span>Â Â Â Â Â Â Â Â <span style="color:#881350;">echo</span> <span style="color:#eb7300;">"{'status': true,'url':'"</span>.TARGET.<span style="color:#eb7300;">"'}"</span>;
Â Â Â Â } <span style="color:#881350;">else</span> {
Â Â Â Â Â Â Â Â <span style="color:#236e25;"><em>//Failed to login
</em></span>Â Â Â Â Â Â Â Â <span style="color:#881350;">echo</span> <span style="color:#eb7300;">"{'status': false,}"</span>;
Â Â Â Â }

Â Â Â Â <span style="color:#236e25;"><em>//Destroy
</em></span>Â Â Â Â <span style="color:#003369;">unset</span>(<span style="color:#825900;">$account</span>);
<span style="color:#dd0000;">?&gt;</span></pre>
<p>TARGET value was define in configuration file that will be your URL you want to send browser redirect to.</p>
<p><strong>Account Class</strong><br />
In Account class, you will write code to select data from users table. Login function will return true or false, so set the default return first. It&#8217;s always false.</p>
<pre class="php">Â Â Â Â Â Â Â Â Â Â Â Â <span style="color:#236e25;"><em>//Set default return value is false
</em></span>Â Â Â Â Â Â Â Â Â Â Â Â <span style="color:#825900;">$return</span> = <span style="color:#881350;">false</span>;</pre>
<p>Connect to database then select users table. All server, table value was configured in config.php file. You can easy change these value in that file.</p>
<pre class="php">Â Â Â Â Â Â Â Â Â Â Â Â <span style="color:#236e25;"><em>//Connect to the Database
</em>Â Â Â Â Â Â Â Â Â Â Â Â <span style="color:#825900;">$this</span>-&gt;db = <span style="color:#661aa9;">@mysql_connect</span>(<span style="color:#825900;">$config</span>[<span style="color:#eb7300;">'server'</span>],<span style="color:#825900;">$config</span>[<span style="color:#eb7300;">'dbuser'</span>],<span style="color:#825900;">$config</span>[<span style="color:#eb7300;">'dbpass'</span>]);

Â Â Â Â Â Â Â Â Â Â Â Â <span style="color:#236e25;"><em>//Can not connect? return false
</em></span>Â Â Â Â Â Â Â Â Â Â Â Â <span style="color:#881350;">if</span>(!<span style="color:#825900;">$this</span>-&gt;db)
Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â <span style="color:#881350;">return</span> <span style="color:#881350;">false</span>;

Â Â Â Â Â Â Â Â Â Â Â Â <span style="color:#236e25;"><em>//Select Database table
</em></span>Â Â Â Â Â Â Â Â Â Â Â Â <span style="color:#825900;">$opendb</span> = <span style="color:#661aa9;">@mysql_select_db</span>(<span style="color:#825900;">$config</span>[<span style="color:#eb7300;">'dbname'</span>], <span style="color:#825900;">$this</span>-&gt;db);

Â Â Â Â Â Â Â Â Â Â Â Â <span style="color:#236e25;"><em>//Can not find or open table? Or something else ....Return false
</em></span>Â Â Â Â Â Â Â Â Â Â Â Â <span style="color:#881350;">if</span>(!<span style="color:#825900;">$opendb</span>)
Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â <span style="color:#881350;">return</span> <span style="color:#881350;">false</span>;
</span></pre>
<p>Because in this tutorial, I check username or email for identifying. So, SQL query will check if its has correct username or email will return true. Absolutely, password must be matched.</p>
<pre class="php">Â Â Â Â Â Â Â Â Â Â Â Â <span style="color:#825900;">$sql</span> = <span style="color:#eb7300;">"SELECT * FROM "</span>.<span style="color:#825900;">$config</span>[<span style="color:#eb7300;">'dbtable'</span>].<span style="color:#eb7300;">" WHERE "</span>;
Â Â Â Â Â Â Â Â Â Â Â Â <span style="color:#825900;">$sql</span> .= <span style="color:#eb7300;">"(username='"</span>.<span style="color:#661aa9;">mysql_real_escape_string</span>(<span style="color:#825900;">$username</span>).<span style="color:#eb7300;">"'"</span>;
Â Â Â Â Â Â Â Â Â Â Â Â <span style="color:#825900;">$sql</span> .= <span style="color:#eb7300;">" OR useremail='"</span>.<span style="color:#661aa9;">mysql_real_escape_string</span>(<span style="color:#825900;">$username</span>).<span style="color:#eb7300;">"')"</span>;
Â Â Â Â Â Â Â Â Â Â Â Â <span style="color:#825900;">$sql</span> .= <span style="color:#eb7300;">" AND userpassword = '"</span>.<span style="color:#661aa9;">md5</span>(<span style="color:#825900;">$password</span>).<span style="color:#eb7300;">"'"</span>;</pre>
<p>To check if it has valid account entered.</p>
<pre class="php">Â Â Â Â Â Â Â Â Â Â Â Â <span style="color:#236e25;"><em>//Query table
</em></span>Â Â Â Â Â Â Â Â Â Â Â Â <span style="color:#825900;">$rs</span> = <span style="color:#661aa9;">@mysql_query</span>(<span style="color:#825900;">$sql</span>,<span style="color:#825900;">$this</span>-&gt;db); Â Â Â Â 

Â Â Â Â Â Â Â Â Â Â Â Â <span style="color:#236e25;"><em>//Can not query
</em></span>Â Â Â Â Â Â Â Â Â Â Â Â <span style="color:#881350;">if</span>(!<span style="color:#825900;">$rs</span>) <span style="color:#881350;">return</span> <span style="color:#881350;">false</span>;

Â Â Â Â Â Â Â Â Â Â Â Â <span style="color:#236e25;"><em>//If result value is number of rows
</em></span>Â Â Â Â Â Â Â Â Â Â Â Â <span style="color:#881350;">if</span>(<span style="color:#661aa9;">mysql_num_rows</span>(<span style="color:#825900;">$rs</span>))
Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â <span style="color:#825900;">$return</span> = <span style="color:#881350;">true</span>;</pre>
<p>Finally, return to $return value</p>
<pre class="php">Â Â Â Â Â Â Â Â Â Â Â Â <span style="color:#236e25;"><em>//Return to the value of $return aboe
</em></span>Â Â Â Â Â Â Â Â Â Â Â Â <span style="color:#881350;">return</span> <span style="color:#825900;">$return</span>;</pre>
<h2>Conclusion:</h2>
<hr />It&#8217;s hard for me to code in PHP because I&#8217;m Java guy and not familiar with PHP. If something goes wrong with the code, please give me some advices. You should define some value that prevent direct file access to these PHP file.</p>
<p>Define in your POST handler</p>
<pre class="php">Â Â Â Â <span style="color:#236e25;"><em>//Prevent dirrect file access
</em></span>Â Â Â Â <span style="color:#661aa9;">define</span>(<span style="color:#eb7300;">'GUARD'</span>, <span style="color:#881350;">true</span>);</pre>
<p>And at the beginning of each others PHP file</p>
<pre class="php"><span style="color:#881350;">if</span><span style="color:#003369;"> </span>(!<span style="color:#661aa9;">defined</span>(<span style="color:#eb7300;">'GUARD'</span>)) {
Â Â Â <span style="color:#881350;">die</span>(<span style="color:#eb7300;">'I am sorry, you can not access this file directly.'</span>);
Â Â Â <span style="color:#881350;">exit</span>;
}</pre>
<p>Thanks for reading and feel free to comment, share this post. Suggestions will be appreciated. </p>
]]></content:encoded>
			<wfw:commentRss>http://aext.net/2009/09/learning-jquery-submit-form-php-mac-style-modal-window/feed/</wfw:commentRss>
		<slash:comments>22</slash:comments>
		</item>
	</channel>
</rss>

