AndiWahjuR E awreman@gmail.com Introduction CodeIgniter Framework untuk web yang dibuat dalam bentuk Web Berbasis Model-View-Controller (MVC) Model strukturdata View informasi yang disampaikan pe pengguna Controller sebuah perantara antara Model dan View 1
Starting CodeIgniter Ekstrak file CodeIgniter_2.1.0.zip ke C://xampp/htdocs/ Rename folder menjadi ci Di folder application ada beberapa folder, antara lain: Folder Models menghubungkan semua aplikasi web ke database Folder Views memuat file berisi sebuah tampilan umum pada halaman web Folder Controllers semua logika pemrograman ada Ekstrak file CodeIgniter_2.1.0.zip ke C://xampp/htdocs/ Rename folder menjadi ci Di folder application ada beberapa folder, antara lain: Folder Models menghubungkan semua aplikasi web ke database Folder Views memuat file berisi sebuah tampilan umum pada halaman web Folder Controllers semua logika pemrograman ada 2
Buatsebuahfile baru latihan.php simpandifolder controllers class Latihan extends CI_Controller{ function index() { echo "hello world."; Akses ke http://localhost/ci/index.php/latihan/ Ubah file latihan.php di folder controllers class Latihan extends CI_Controller{ function index(){ echo "hello world."; function fungsibaru(){ echo "contoh fungsi baru."; Akses ke http://localhost/ci/index.php/latihan/fungsibaru 3
Buat sebuah file viewlatihan.php simpan di folder views <html> <head> </head> <body> </body> </html> <title>view Latihan</title> Contoh halaman view latihan. //halaman latihan.php class Latihan extends CI_Controller{ function index() { $this->load->view('viewlatihan.php'); Akses ke http://localhost/ci/index.php/latihan/ Ubah file latihan.php di folder controllers class Latihan extends CI_Controller{ function index(){ $pwl['asdos1'] = "Christian Sutheja"; $pwl['asdos2'] = "Danny Aguswahyudi ; $pwl['asdos3'] = "Richard Hartanto"; $this->load->view('viewlatihan.php', $pwl); <html> <head> <title>view Latihan</title> </head> <body> Contoh halaman view latihan.<br /> Asisten Dosen di ini adalah echo $asdos1.", ".$asdos2.", dan ".$asdos3; </body> </html> Akses ke http://localhost/ci/index.php/latihan/ 4
Membuat File Model Buat sebuah file modellatihan.php simpan di folder models Buka file config.php di folder application/config/ $config['base_url'] = 'http://localhost/ci/index.php/'; Buka file routes.php di folder application/config/ $route['default_controller'] = "latihan"; Buka file autoload.php di folder application/config/ $autoload['libraries'] = array('database'); Database Ubah file database.php di folder config $db['default']['hostname'] = 'localhost'; $db['default']['username'] = 'root'; $db['default']['password'] = ''; $db['default']['database'] = 'latihan'; $db['default']['dbdriver'] = 'mysql'; 5
Database (cont d) Ubah file latihan.php di folder controllers class Latihan extends CI_Controller { function index() { $this->load->model('modellatihan'); $data['records'] = $this->modellatihan->fetchdb(); $this->load->view('viewlatihan', $data); Database (cont d) Ubah file viewlatihan.php di folder views <html> <head> <title>view Latihan</title> </head> <body> foreach($records as $row) { echo $row->name."<br />"; </body> </html> 6
Database Active Record Query Ubah file modellatihan.php di folder models class Modellatihan extends CI_Model{ function fetchdb(){ $this->db->select("employee_id, name"); $fetch = $this->db->get('employees'); if($fetch->num_rows() > 0){ foreach($fetch->result() as $row){ $data[] = $row; return $data; Database Active Record Query(cont d) Ubah file modellatihan.php di folder models class Modellatihan extends CI_Model{ function fetchdb(){ $query = "SELECT name, salary FROM employees WHERE employee_id =?"; $fetch = $this->db->query($query, 2); if($fetch->num_rows() > 0){ foreach($fetch->result() as $row){ $data[] = $row; return $data; 7
Database Active Record Query(cont d) Ubah file modellatihan.php di folder models class Modellatihan extends CI_Model{ function fetchdb(){ $query = "SELECT name, salary FROM employees WHERE job_id =? AND dept_id =?"; $fetch = $this->db->query($query, array(2, 2)); if($fetch->num_rows() > 0){ foreach($fetch->result() as $row){ $data[] = $row; return $data; Perbedaan echo form_open('namafilecontroller/fungsi'); <form name=".." method=".." action=".."> 'namafilecontroller/fungsi' perintah action form akan diproses oleh filecontroller.php yang terdapat fungsi didalamnya 8
Perbedaan(cont d) $namauser = array( 'name' => 'nama', 'id' => 'nama', 'value' => set_value('nama') ); <input type="text" name="nama" id="nama" value=".."> Perbedaan(cont d) echo form_input($namauser); echo form_submit('submit', 'Submit'); echo form_close(); 9