In this tutorial we will discuss how to create autocompletewith codeigniter , in this discussion I assume all friends TWD already understand the initial setting of codeigniter.If not please learn first two tutorials below
The first step we create a database with the name of the exercise in phpmyadmin:
Database: `latihan`
Database exercises
Next create tables and fields in the training database:
-- -- Struktur dari tabel `autocomplete` -- CREATE TABLE IF NOT EXISTS `autocomplete` ( `nim` bigint(20) NOT NULL, `nama` varchar(30) NOT NULL, `jurusan` varchar(30) NOT NULL ) ENGINE=InnoDB DEFAULT CHARSET=latin1;
Table autocomplete
Input / Insert new data to display data when autocomplete search:
-- -- Dumping data untuk tabel `autocomplete` -- INSERT INTO `autocomplete` (`nim`, `nama`, `jurusan`) VALUES (1199870012, 'Muhammad Yusuf Hamdani', 'Teknik Informatika'), (7779127910, 'Tutorial Web Design', 'Tutorial Website'), (9998711120, 'Rahmayanti', 'PGSD');
Insert autocomplete data
The phpmyadmin training database will look like this:
Click image to enlarge
Click image to enlarge
Open config / autoload.php, then find the code below and adjust it like this:
The structure or placement of the file will look like this, please create a folder and adjust it:
Then open the controllers / welcome.php, adjust as below:
<?phpif(!defined('BASEPATH'))exit('No direct script access allowed');classWelcomeextendsCI_Controller{/*** Index Page for this controller.** Maps to the following URL* http://example.com/index.php/welcome * - or - * http://example.com/index.php/welcome/index * - or - * Since this controller is set as the default controller in * config/routes.php, it's displayed at http://example.com/ * * So any other public methods not prefixed with an underscore will * map to /index.php/welcome/<method_name>* @see http://codeigniter.com/user_guide/general/urls.html */ public function index() { $this->load->view('index'); } } /* End of file welcome.php */ /* Location: ./application/controllers/welcome.php */ ?>
Controllers / welcome.php
Create a new file index.php , open the view / adjust as below:
<?phpif(!defined('BASEPATH'))exit('No direct script access allowed');?><<!DOCTYPE html><html><head><title>Autocomplete |AZZURA Media</title><!-- Memanggil file .js untuk proses autocomplete --><scripttype='text/javascript'src='<?phpechobase_url();?>assets/js/jquery-1.8.2.min.js'></script><scripttype='text/javascript'src='<?phpechobase_url();?>assets/js/jquery.autocomplete.js'></script><!-- Memanggil file .css untuk style saat data dicari dalam filed --><linkhref='<?phpechobase_url();?>assets/js/jquery.autocomplete.css'rel='stylesheet'/><!-- Memanggil file .css autocomplete_ci/assets/css/default.css --><linkhref='<?phpechobase_url();?>assets/css/default.css'rel='stylesheet'/><scripttype='text/javascript'>var site ="<?phpechosite_url();?>"; $(function(){ $('.autocomplete').autocomplete({// serviceUrl berisi URL ke controller/fungsi yang menangani request kita serviceUrl: site+'/autocomplete/search', // fungsi ini akan dijalankan ketika user memilih salah satu hasil request onSelect: function (suggestion) { $('#v_nim').val(''+suggestion.nim); // membuat id 'v_nim' untuk ditampilkan $('#v_jurusan').val(''+suggestion.jurusan); // membuat id 'v_jurusan' untuk ditampilkan } }); }); </script></head><body><divid="content"><h1>Autocomplete</h1><formaction="<?phpechosite_url('admin/c_admin/add_orders');?>"method="post"><divclass="wrap"><table><tr><td><small>Nama :</small><br><inputtype="search"class='autocomplete nama'id="autocomplete1"name="nama_customer"/></td></tr><tr><td><small>Jurusan :</small><br><inputtype="text"class='autocomplete'id="v_jurusan"name="nama_customer"/></td></tr><tr><td><small>NIM:</small><br><inputtype="text"class='autocomplete'id="v_nim"name="nama_customer"/></td></tr></div></form></div></body></html>
Views / index.php
Create a new file autocomplete.php to display data when searched, open controllers / adjust as below:
<?phpif(!defined('BASEPATH'))exit('No direct script access allowed');classAutocompleteextendsCI_Controller{publicfunction__construct(){parent::__construct();}publicfunctionsearch(){// tangkap variabel keyword dari URL $keyword = $this->uri->segment(3); // cari di database $data = $this->db->from('autocomplete')->like('nama',$keyword)->get(); // format keluaran di dalam array foreach($data->result() as $row) { $arr['query'] = $keyword; $arr['suggestions'][] = array( 'value' =>$row->nama, 'nim' =>$row->nim, 'jurusan' =>$row->jurusan ); } // minimal PHP 5.2 echo json_encode($arr); } } ?>
Controllers / autocomplete.php
The last one please create a css to design the input form etc., if you have not created the folder assets / css please make first on the folder website friends, for the structure of folders and files please see in the picture above, thefollowing script from default.css :
body {font-family: verdana,arial,sans-serif;margin:0px;padding:0px;}.wrap{width:50%;background:#F0F0F0;margin:auto;padding:25px;overflow: hidden;}h1 {text-align: center;}input.nama{font-size:28px;width:380px;}input, textarea {border:1px solid #CCC;}
Assets / css / default.css
Congratulations, all friends have finished the tutorial well, now please try it in the browser and see the result will be like this: