Thursday, 29 October 2015

MULTIPLE AUTOCOMPLETE IN CODEIGNTER

today i review my project then i found bugs i don't know why my autocomplete doesn't work well, so i fixed from here

so i fix from that tutorial cause that tutorial doesn't give you right autocomplete.

he just select wll data from table without spesific search so here is database acctually same but it's oke

DATABASSE please copy one by one
 CREATE TABLE `tb_kotaindonesia` (  
  `id` int(3) NOT NULL AUTO_INCREMENT,  
  `nama_kota` varchar(50) DEFAULT NULL,  
  `ibu_kota` varchar(50) DEFAULT NULL,  
  `keterangan` text,  
  PRIMARY KEY (`id`)  
 ) ENGINE=MyISAM DEFAULT CHARSET=latin1  
 insert into `tb_kotaindonesia` (`id`, `nama_kota`, `ibu_kota`, `keterangan`) values('1','Medan','Medan','Kota medan adalah ibukota Provinsi Sumatera Utara');  
 insert into `tb_kotaindonesia` (`id`, `nama_kota`, `ibu_kota`, `keterangan`) values('2','Tapanuli Utara','Tarutung','Tapanuli Utara atau sering disebut TAPUT adalah sebuah tempat sejuk');  
 insert into `tb_kotaindonesia` (`id`, `nama_kota`, `ibu_kota`, `keterangan`) values('3','Jakarta','Jakarta','Jakarta Megapolitan Ibukota Negara Republik Indonesia');  
 insert into `tb_kotaindonesia` (`id`, `nama_kota`, `ibu_kota`, `keterangan`) values('4','Padang','Padang','Padang adalah tempat wisata jam gadang, disana terdapat jembatan yang membelah sungai musi');  
 insert into `tb_kotaindonesia` (`id`, `nama_kota`, `ibu_kota`, `keterangan`) values('5','Deliserdang','Lubuk Pakam','Sebuah kota sebelum medan yang kita pijak pertama sekali jika lewat udara (Bandara Kualanamu).');  


Here is my controller, there is diffrent here please see
 $q = strtolower($_GET['term']);  
if refrensi above he didn't tell spesific data in table he just select all woithout key and where clasue in model,
 <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');  
 class Kota extends CI_Controller  
 {  
   public function __construct() {  
     parent::__construct();  
     $this->load->model('mkota'); //load model mkota yang berada di folder model  
     $this->load->helper(array('url')); //load helper url  
   }  
   public function index()  
   {  
     $data['titel']='Multiple Output Autocomplete Jquery UI + CodeIgniter'; //varibel title  
     $this->load->view('vkota',$data); //tampilan awal ketika controller kota di akses  
   }  
   public function get_allkota() {  
     $q = strtolower($_GET['term']);  
     $query = $this->mkota->get_allkota($q); //query model  
     $kota    = array();  
     foreach ($query as $d) {  
       $kota[]   = array(  
         'label' => $d->nama_kota, //variabel array yg dibawa ke label ketikan kunci  
         'nama' => $d->nama_kota , //variabel yg dibawa ke id nama  
         'ibukota' => $d->ibu_kota, //variabel yang dibawa ke id ibukota  
         'keterangan' => $d->keterangan //variabel yang dibawa ke id keterangan  
       );  
     }  
     echo json_encode($kota);   //data array yang telah kota deklarasikan dibawa menggunakan json  
   }  
 }  

Model here is i give where clause so you can spesific select table in autocomplete
 <?php  
 class Mkota extends CI_Model {  
   function __construct() {  
     parent::__construct();  
   }  
   function get_allkota($kode) {  
     $this->db->like('nama_kota', $kode);  
     $res = $this->db->get('tb_kotaindonesia');  
     if ($res->num_rows() > 0) {   
       return $res->result();  
     }  
   }  
 }  
 ?>  
Hope you understand lah what i mean here, thanks for visiting me ! file can download right here Note : Please provide file in view if possible just use cdn cause i take in my project

3 comments:

  1. bro minta file pendukung sperti jquerynya hehe

    ReplyDelete
    Replies
    1. hello pake ini aja http://pastebin.com/0GFgbzRJ
      tengok bagian sini https://jqueryui.com/autocomplete/ lihat di view sourcenya

      Delete
  2. This comment has been removed by the author.

    ReplyDelete