<?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 Tutorial</title>
	<atom:link href="http://aext.net/tag/php-tutorial/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>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>
	</channel>
</rss>

