Kamis, 05 Mei 2011

Membuat Paging dengan CodeIgniter - PHP

Paging, fitur untuk mempercantik list daftar dihalaman website. Tentunya supaya rapi dan enak diliat. Maka ketika saya belajar Code Igniter, langsung googling2 gimana caranya. Berikut scriptnya:
1. Buat halaman untuk menampilkan list data kita, contoh category.php di application/views . Letakkan diantara tag <body></body>

<table class="list" width="54%">
 <tr class="list">
  <th class="list">Code</th><th>Category</th>
 </tr>
 <?php foreach ($datacategory as $row): ?>
 <tr class="list">
  <td class="list"><?php echo $row -> code ?></td>
  <td class="list"><?php echo $row -> name ?></td>
 </tr>
 <?php endforeach ?>
</table>
<?php echo $this->pagination->create_links(); ?>

2. Buat file category.php di application/controlles

<?php
class Category extends CI_Controller {
 function __construct()
 {
   parent::__construct();
   $this->load->model('Querypage');
   $this->load->library('pagination'); //load library pagination
 }
 public function index($offset = 0)
 {
        $perpage =10; //tentukan jumlah data per halaman
   //untuk konfigurasi pagination
        $config = array(
            'base_url' => base_url() . 'index.php/category/index/',
            'total_rows' => count($this->Querypage->get_type()),
            'per_page' => $perpage,
        );    //inisialisasi pagination dn config di atas
        $this->pagination->initialize($config);
        $data['datacategory'] = $this->Querypage->get_type(array('perpage' => $perpage, 'offset' => $offset));
 $this->load->view('category', $data);
      }
}
?>

3. File untuk modelnya, buat quarypage.php di application/models

class Querypage extends CI_Model {
    function __construct()
    {
        parent::__construct();
    }
     function get_type($limit = array()) //paging category
 {
  if ($limit == NULL)
   return $this->db-> order_by("id", "desc") -> get('type') -> result();
  else
   return $this->db->limit($limit['perpage'], $limit['offset'])-> order_by("id", "desc") -> get('type') -> result();
 }
}

4. Tambahkan file pagination.php di application/config/

<?php
$config['full_tag_open']  = '<div class="pagination">';
$config['full_tag_close']  = '</div>';
$config['next_link']  = 'Next &raquo;';
$config['prev_link']   = '&laquo; Prev';
$config['num_tag_open']  = '<div class="digit">';
$config['num_tag_close']  = '</div>';
$config['cur_tag_open']  = '<div class="digit current">';
$config['cur_tag_close']  = '</div>';
$config['num_links']   =  1 ;
$config['last_link']   = '<b>Last &rsaquo;</b>';
$config['first_link']   = '<b>&lsaquo; First</b>';
?>

5. Tambahkan script ini di css, agar paging terlihat lebih cantik

/**
@author Cheyuz => untuk paging
**/
.pagination{margin: 5px; padding: 10px 0px;}
.pagination .digit{padding: 0 10px 0 10px; display: inline;}
.pagination .digit:hover, .pagination .current{padding: 2px 10px 2px 10px; background: #eee; font-weight: 900; -moz-border-radius:5px}

6. Selesai. Mudahkan ^_^

Referensi: UserGuide

Tidak ada komentar:

Posting Komentar