Create a new file in a folder -> Controller
1 |
/application/controller/movies.php |
Write Code like this
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); class Movies extends CI_Controller { public function display($sort_by = 'judul', $sort_order = 'asc', $offset = 0) { $limit = 20; $data['fields'] = array( 'id' => 'ID', 'judul' => 'Judul', 'deskripsi' => 'Deskripsi', 'kategori' => 'Kategori', 'artis' => 'Artis' ); $this->load->model('movies_mod'); $results = $this->movies_mod->search($limit, $offset, $sort_by, $sort_order); $data['movies'] = $results['rows']; $data['num_results'] = $results['num_rows']; // pagination $this->load->library('pagination'); $config = array(); $config['base_url'] = site_url('movies/display'); $config['total_rows'] = $data['num_results']; $config['per_page'] = $limit; $config['uri_segment'] = 3; $this->pagination->initialize($config); $data['pagination'] = $this->pagination->create_links(); $data['sort_by'] = $sort_by; $data['sort_order'] = $sort_order; $this->load->view('movies_view', $data); } } |
Create a new file in a folder -> Models
1 |
/application/models/movies_mod.php |
Write Code like this
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); class Movies_mod extends CI_Model { public function search($limit, $offset, $sort_by, $sort_order) { $sort_order = ($sort_order == 'desc') ? 'desc' : 'asc'; $sort_columns = array('id', 'judul', 'deskripsi', 'kategori', 'artis'); $sort_by = (in_array($sort_by, $sort_columns)) ? $sort_by : 'judul'; //result query $select = array( 'id', 'judul', 'deskripsi', 'kategori', 'artis' ); $query = $this->db->select($select) ->from('movies') ->limit($limit, $offset) ->order_by($sort_by, $sort_order); $ret['rows'] = $query->get()->result(); //count query $query = $this->db->select('COUNT(*) as count', FALSE) ->from('movies'); $tmp = $query->get()->result(); $ret['num_rows'] = $tmp[0]->count; return $ret; } } |
Create a new file in a folder -> Views
1 |
/application/views/movies_view.php |
Write Code like this
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Movies</title> <style type="text/css"> * { font: Arial 12px; } table { border-collapse: collapse; } td, th { border: 1px solid #666666; padding: 4px; } div { margin: 4px; } .sort_asc:after { content: "^"; } .sort_desc:after { content: ""; } </style> </head> <body> <div> Found <?php echo $num_results; ?> Movie </div> <table> <thead> <?php foreach ($fields as $field_name => $field_display) : ?> <th <?php if ($sort_by == $field_name) echo "class=\"sort_$sort_order\"" ?>> <?php echo anchor("movies/display/$field_name/" . (($sort_order == 'asc' && $sort_by == $field_name) ? 'desc' : 'asc' ) , $field_display); ?> </th> <?php endforeach; ?> </thead> <tbody> <?php foreach ($movies as $movie) : ?> <tr> <?php foreach ($fields as $field_name => $field_display) : ?> <td><?php echo $movie->$field_name; ?></td> <?php endforeach; ?> </tr> <?php endforeach; ?> </tbody> </table> <?php if (strlen($pagination)) : ?> <div>Pages : <?php echo $pagination; ?></div> <?php endif; ?> </body> </html> |