Categories
PHP

Membuat Pagination Menggunakan CodeIgniter dan Bootstrap

membuat pagination menggunakan codeigniter

Kesempatan kali ini kami akan share bagaimana membuat pagination menggunakan CodeIgniter dan Botstrap. Pagination sangat banya digunakan untuk entri data yang banyak, dan banyak digunakan untuk menampilkan artikel website sesuai jumlah yang di inginkan pengguna.

Langsung saja mari kita masuk kedalam pembahasan intinya.

1. (Base Url) Buka folder application / config / config.php disini anda melakukan konfigurasi base url.

$config['base_url'] = 'http://localhost/cipagi';

2. Buat database dengan nama mydatabase kemudian masukkan struktur database dibawah ini.

CREATE TABLE `members` (
  `id` int(11) NOT NULL,
  `firstname` varchar(30) NOT NULL,
  `lastname` varchar(30) NOT NULL,
  `address` text NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;


INSERT INTO `members` (`id`, `firstname`, `lastname`, `address`) VALUES
(1, 'Arman', 'Malino', 'Gowa City'),
(2, 'Arman', 'Malino', 'Gowa City'),
(3, 'Arman', 'Malino', 'Gowa City'),
(4, 'Arman', 'Malino', 'Gowa City'),
(5, 'Arman', 'Malino', 'Gowa City'),
(6, 'Arman', 'Malino', 'Gowa City'),
(7, 'Arman', 'Malino', 'Gowa City'),
(8, 'Arman', 'Malino', 'Gowa City'),
(9, 'Arman', 'Malino', 'Gowa City'),
(10, 'Arman', 'Malino', 'Gowa City');


ALTER TABLE `members`
  ADD PRIMARY KEY (`id`);

3. (Koneksi Database) Buka folder application / config / database.php

$db['default'] = array(
 'dsn' => '',
 'hostname' => 'localhost',
 'username' => 'root',
 'password' => '',
 'database' => 'mydatabase',
 'dbdriver' => 'mysqli',
 'dbprefix' => '',
 'pconnect' => TRUE,
 'db_debug' => (ENVIRONMENT !== 'production'),
 'cache_on' => FALSE,
 'cachedir' => '',
 'char_set' => 'utf8',
 'dbcollat' => 'utf8_general_ci',
 'swap_pre' => '',
 'encrypt' => FALSE,
 'compress' => FALSE,
 'stricton' => FALSE,
 'failover' => array(),
 'save_queries' => TRUE
);

4. (Models) Buka folder application / models kemudian buat file dengan nama Members_model.php

<?php
defined('BASEPATH') OR exit('No direct script access allowed');
 
class Members_model extends CI_Model {
    function __construct(){
      parent::__construct();
      $this->load->database();
    }
 
    public function get_current_page($limit, $start) {
        $this->db->limit($limit, $start);
        $query = $this->db->get('members');
        $rows = $query->result();
 
        if ($query->num_rows() > 0) {
            foreach ($rows as $row) {
                $data[] = $row;
            }
             
            return $data;
        }
 
        return false;
    }
     
    public function get_total() {
        return $this->db->count_all('members');
    }
}

5. (Controllers) Buka folder application / controllers kemudian buat file dengan nama Member.php

<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Member extends CI_Controller {
 function __construct(){
  parent::__construct();
  $this->load->library('pagination');
  $this->load->helper('url');
  $this->load->model('members_model');
 }
 public function index(){
  //set params
  $params = array();
  //set records per page
        $limit_page = 5;
        $page = ($this->uri->segment(3)) ? ($this->uri->segment(3) - 1) : 0;
        $total = $this->members_model->get_total();

        if ($total > 0) 
        {
            // get current page records
            $params['results'] = $this->members_model->get_current_page($limit_page, $page * $limit_page);
             
            $config['base_url'] = base_url() . 'member/index';
            $config['total_rows'] = $total;
            $config['per_page'] = $limit_page;
            $config['uri_segment'] = 3;

            //paging configuration
            $config['num_links'] = 2;
            $config['use_page_numbers'] = TRUE;
            $config['reuse_query_string'] = TRUE;
            
            //bootstrap pagination 
            $config['full_tag_open'] = '<ul class="pagination">';
   $config['full_tag_close'] = '</ul>'; 
   $config['first_link'] = '« First';
   $config['first_tag_open'] = '<li>';
   $config['first_tag_close'] = '</li>';
   $config['last_link'] = 'Last &raquo';
   $config['last_tag_open'] = '<li>';
   $config['last_tag_close'] = '</li>';
   $config['next_link'] = 'Next';
   $config['next_tag_open'] = '<li>';
   $config['next_tag_close'] = '<li>';
   $config['prev_link'] = 'Prev';
   $config['prev_tag_open'] = '<li>';
   $config['prev_tag_close'] = '<li>';
   $config['cur_tag_open'] = '<li class="active"><a href="#">';
   $config['cur_tag_close'] = '</a></li>';
   $config['num_tag_open'] = '<li>';
   $config['num_tag_close'] = '</li>';
             
            $this->pagination->initialize($config);
             
            // build paging links
            $params['links'] = $this->pagination->create_links();
        }
         
        $this->load->view('member_table', $params);
 }
}

6. (Default Controller) Buka folder application / config / routes.php

$route['default_controller'] = 'member';

7. (Views) Buka folder application / views kemudian buat file dengan nama member_table.php

<!DOCTYPE html>
<html>
<head>
 <meta charset="utf-8">
 <title>CodeIgniter Pagination</title>
 <link rel="stylesheet" type="text/css" href="<?php echo base_url(); ?>bootstrap/css/bootstrap.min.css">
</head>
<body>
 <nav class="navbar navbar-default navbar-static-top">
  <div class="container-fluid">
   <div class="navbar-header">
    <a class="navbar-brand" href="https://jasawebsite.ooo/">jasawebsite</a>
    <ul class="nav navbar-nav">
     <li class="active"><a href="#">Create a Pagination in CodeIgniter</a></li>
    </ul>
   </div>
  </div>
 </nav>
 <div class="container">
  <div class="row">
   <div class="col-sm-8 col-sm-offset-2">
    <br/>
    <table class="table table-bordered table-striped">
     <thead>
      <tr>
       <th>ID</th>
       <th>Firstname</th>
       <th>Lastname</th>
       <th>Address</th>
      </tr>
     </thead>
     <tbody>
      <?php
      foreach($results as $row){
       ?>
       <tr>
        <td><?php echo $row->id; ?></td>
        <td><?php echo $row->firstname; ?></td>
        <td><?php echo $row->lastname; ?></td>
        <td><?php echo $row->address; ?></td>
       </tr>
       <?php
      } 
      ?>
     </tbody>
    </table>
    <?php
    if(isset($links)){
     echo $links;
    } 
    ?>
   </div>
  </div>
 </div>
</body>
</html>

Kalian bisa mendownload filenya langsung dengan menekan link button dibawah ini.

Jika kalian belum begitu paham silahkan tinggalkan komentar di bawah dan mulai berdiskusi dengan kami.

Leave a Reply

Your email address will not be published. Required fields are marked *