2012/11/27
Andi Wahju R E
[email protected]
05-05-12
Introduction • CodeIgniter – Framework untuk web yang dibuat dalam bentuk Web – Berbasis Model-View-Controller (MVC) • Model struktur data • View informasi yang disampaikan pe pengguna • Controller sebuah perantara antara Model dan View
PWL
05-05-12
1
2012/11/27
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 PWL
05-05-12
Starting CodeIgniter (cont’d) • 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 PWL
05-05-12
2
2012/11/27
Starting CodeIgniter (cont’d) • Buat sebuah file baru “latihan.php” simpan di folder controllers
Akses ke http://localhost/ci/index.php/latihan/
PWL
05-05-12
Starting CodeIgniter (cont’d) • Ubah file “latihan.php” di folder controllers
PWL Akses ke http://localhost/ci/index.php/latihan/fungsibaru
05-05-12
3
2012/11/27
Starting CodeIgniter (cont’d) • Buat sebuah file “viewlatihan.php” simpan di folder views
View Latihan Contoh halaman view latihan.
load->view('viewlatihan.php'); } }
PWL
?>
Akses ke http://localhost/ci/index.php/latihan/ 05-05-12
Starting CodeIgniter (cont’d) • Ubah file “latihan.php” di folder controllers load->view('viewlatihan.php', $pwl); } } ?>
View Latihan Contoh halaman view latihan.
Asisten Dosen di PWL ini adalah PWL
Akses ke http://localhost/ci/index.php/latihan/ 05-05-12
4
2012/11/27
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');
PWL
05-05-12
Database • Ubah file “database.php” di folder config $db['default']['hostname'] $db['default']['username'] $db['default']['password'] $db['default']['database'] $db['default']['dbdriver']
= = = = =
'localhost'; 'root'; ''; 'latihan'; 'mysql';
PWL
05-05-12
5
2012/11/27
Database (cont’d) • Ubah file “latihan.php” di folder controllers load->model('modellatihan'); $data['records'] = $this->modellatihan->fetchdb(); $this->load->view('viewlatihan', $data); } } ?>
PWL
05-05-12
Database (cont’d) • Ubah file “viewlatihan.php” di folder views
View Latihan name."
"; } ?>
PWL
05-05-12
6
2012/11/27
Database – Active Record Query • Ubah file “modellatihan.php” di folder models db->select("employee_id, name"); $fetch = $this->db->get('employees'); if($fetch->num_rows() > 0){ foreach($fetch->result() as $row){ $data[] = $row; } return $data; } }
PWL
} ?>
05-05-12
Database – Active Record Query (cont’d) • Ubah file “modellatihan.php” di folder models db->query($query, 2); if($fetch->num_rows() > 0){ foreach($fetch->result() as $row){ $data[] = $row; } return $data; } }
PWL
} ?>
05-05-12
7
2012/11/27
Database – Active Record Query (cont’d) • Ubah file “modellatihan.php” di folder models db->query($query, array(2, 2)); if($fetch->num_rows() > 0){ foreach($fetch->result() as $row){ $data[] = $row; } return $data; } }
PWL
} ?>
05-05-12
Perbedaan