title: Live search using Codeigniter Mysql
body: <p>I am doing a project on Live Search Using Jquery Ajax, Php Codeigniter and Mysql</p>
  
  <p>Here is my model Code:</p>
  
  <pre><code>&lt;?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
  class Subjectmodel extends CI_Model {
     public function __construct() {
          parent::__construct();
  
      }
      function getSubject($search){
          $this-&gt;db-&gt;select("SUB_ID,NAME");
          $whereCondition = array('NAME' =&gt;$search);
          $this-&gt;db-&gt;where($whereCondition);
          $this-&gt;db-&gt;from('ix08s_subjects');
          $query = $this-&gt;db-&gt;get();
          return $query-&gt;result();
      }
  }
  ?&gt;
  </code></pre>
  
  <p>Here is my controller code:</p>
  
  <pre><code>&lt;?php
  class Subject extends CI_Controller {
      function __construct() {
          parent::__construct();
          $this-&gt;load-&gt;model('SubjectModel');
      }
      public function index(){
          $search=  $this-&gt;input-&gt;post('search');
          $query = $this-&gt;SubjectModel-&gt;getSubject($search);
          echo json_encode ($query);
      }
  }
  ?&gt;
  </code></pre>
  
  <p>and this is my view code :</p>
  
  <pre><code>&lt;html&gt;
  &lt;head&gt;
  &lt;script type="text/javascript" language="javascript" src="http://somexyz/js/javascripts/plugin/jquery.js"&gt;&lt;/script&gt;
  &lt;script type="text/javascript" src="http://somexyz/js/javascripts/plugin/json2.js"&gt;&lt;/script&gt;
  &lt;script&gt;
      $(document).ready(function(){
        $("#search").keyup(function(){
          if($("#search").val().length&gt;3){
          $.ajax({
              type: "post",
              url: "http://localhost/ibps/index.php/subject",
              cache: false,               
              data:'search='+$("#search").val(),
              success: function(response){
                  $('#finalResult').html("");
                  var obj = JSON.parse(response);
                  if(obj.length&gt;0){
                      try{
                          var items=[];   
                          $.each(obj, function(i,val){                                            
                              items.push($('&lt;li/&gt;').text(val.NAME));
                          }); 
                          $('#finalResult').append.apply($('#finalResult'), items);
                      }catch(e) {     
                          alert('Exception while request..');
                      }       
                  }else{
                      $('#finalResult').html($('&lt;li/&gt;').text("No Data Found"));       
                  }       
  
              },
              error: function(){                      
                  alert('Error while request..');
              }
          });
          }
          return false;
        });
      });
  &lt;/script&gt;
  &lt;/head&gt;
  &lt;body&gt;
  &lt;div id="container"&gt;
  &lt;input type="text" name="search" id="search" /&gt;
  &lt;ul id="finalResult"&gt;&lt;/ul&gt;
  &lt;/div&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>This is my output :</p>
  
  <p>[{"SUB_ID":"1","NAME":"physics"},{"SUB_ID":"2","NAME":"physics"}]</p>
  
  <p>But This should be the correct output</p>
  
  <p><a href="http://jsfiddle.net/serigo1990/uShzQ/" rel="nofollow">http://jsfiddle.net/serigo1990/uShzQ/</a></p>
  
  <p>Got the answer</p>