Tuesday, 6 October 2015

Auto Complete in CodeIgniter

Assalamualaikum..

View (JQuery)

1
2
3
<form id="myForm">
  <input type="text" id="name" name="name">
</form>


1
2
3
4
5
$(document).ready(function(){
    $("#name").autocomplete({ 
     source: '<?=base_url();?>index.php/staff/getNames'
    });
}); 

Controller

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
class Staff extends CI_Controller {

  public function __construct() {
      parent::__construct();                
      $this->load->model("Staff_model");
      $this->load->helper('html');     
  }

  function index(){
      $this->load->view('staff_view');
  }
 
  function get_staffName(){
      if (isset($_GET['term'])){
         $q = strtolower($_GET['term']);
         $this->staff_model->get_staffName($q);
      }
  }

}

Model


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
class staff_model extends CI_Model
{
   public function get_staffName($search_term) {

       $this->db->select('name'); 
       $this->db->like('name', $search_term);
       $query = $this->db->get('tbl_staff');
  
       if($query->num_rows > 0){
           foreach ($query->result_array() as $row){
              $row_set[] = htmlentities(stripslashes($row['name'])); //build an array
           }
           echo json_encode($row_set); //format the array into json data
       }
    }
}


Sekian.

****************************************
Disediakan Oleh : Haslina Shamsudin
Sumber : www.codersmount.com
****************************************

0 comments:

Post a Comment