Create Pagination CodeIgniter

In this post, I want to share how to create pagination page using CodeIgniter Framework. Example, I have a database such as:

spp=> \d transaksi;
View "public.transaksi"
Column     |         Type          | Modifiers
----------------+-----------------------+-----------
no_induk       | character varying(12) |
nama_siswa     | character varying(50) |
kelas          | character varying(4)  |
jumlah         | numeric               |
biaya_perbulan | numeric               |
pemasukan      | numeric               |
tunggakan      | numeric               |
bulan          | text                  |

spp=>


Now we have to know the number of rows in our database.

spp=> SELECT COUNT(*) FROM transaksi;
 count 
-------
    26
(1 row)

spp=> 

From the result, I want to share pages with 15 lines page limit.

spp=> SELECT * FROM transaksi LIMIT 15 OFFSET 0;
no_induk  |     nama_siswa      | kelas | jumlah | biaya_perbulan | pemasukan | tunggakan |                 bulan                  
------------+---------------------+-------+--------+----------------+-----------+-----------+----------------------------------------
 2010140401 | Fajar Sidik Nugroho | IX    | 288000 |          24000 |     48000 |    240000 | Februari,Januari
 2010140402 | Nuryamin Setiawan   | IX    | 288000 |          24000 |    144000 |    144000 | Februari,Maret,Juni,Mei,April,Januari,
 2010140403 | Syarif Hidayatullah | VII   | 240000 |          20000 |         0 |    240000 | 
 2010140404 | Muhammad Nurwahid   | VIII  | 264000 |          22000 |         0 |    264000 | 
 2010140405 | Riza Anggraini      | VIII  | 264000 |          22000 |     44000 |    220000 | Januari, Februari
 2010140406 | Yuli Rahmawati      | IX    | 288000 |          24000 |         0 |    288000 | 
 2010140407 | Wahid Irawan        | VII   | 240000 |          20000 |         0 |    240000 | 
 2010140409 | Rahma Wulandari     | VIII  | 264000 |          22000 |         0 |    264000 | 
 2010140410 | Mario Alvin         | IX    | 288000 |          24000 |         0 |    288000 | 
 2010140411 | Dasky Azzrazky      | VII   | 240000 |          20000 |         0 |    240000 | 
 2010140412 | Yuyun Maryuyun      | VIII  | 264000 |          22000 |         0 |    264000 | 
 2010140413 | Eti Suketi          | IX    | 288000 |          24000 |         0 |    288000 | 
 2010140414 | Markonah            | VII   | 240000 |          20000 |         0 |    240000 | 
 2010140415 | Marzuki Andi        | VIII  | 264000 |          22000 |         0 |    264000 | 
 2010140416 | Susilawati          | IX    | 288000 |          24000 |         0 |    288000 | 
(15 rows)

(END)

Doc and example pagination CodeIgniter, you can read on this url http://ellislab.com/codeigniter/user-guide/libraries/pagination.html.
So in this post, I want create another example and using Active Record.
Example my Models (m_database.php):

<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
class M_database extends CI_Model {
public function __construct() {
	$config["hostname"] = "localhost";
	$config["dbdriver"] = "postgre"; //You can change with your database, if you use mysql, you can change with mysql
	$config["database"] = "yourdatabasename";
	$config["username"] = "yourusername";
	$config["password"] = "yourpassword";
	$this->load->database($config);
}

public function record_count() {
        //create query to show number row on database
        return $this->db->count_all("transaksi");
    }
 
    public function fetch_siswa($limit, $start) {
        //create query with limit function
        return $this->db->limit($limit, $start)->get("transaksi");
    }
}
 
/* End of file m_database.php */
/* Location: ./application/models/m_database.php */

Example my Controllers (c_home.php):

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
session_start(); //we need to call PHP's session object to access it through CI
class C_home extends CI_Controller {
    public function __construct() {
        parent::__construct();
        //load library pagination
        $this->load->library("pagination");
        $this->load->helper('url');
        $this->load->model('m_database');
    }

    public function index() {
            //configurations pagination
            $config = array();
            $config["base_url"] = base_url("c_home/index"); //url c_home.php
            $config["total_rows"] = $this->m_database->record_count(); //get count row from model
            $config["per_page"] = 15; //get limit per page
            $config["uri_segment"] = 3; //example http://localhost/codeigniter/c_home/index/15 --> codeigniter is uri_segment 0, c_home = 1, index = 2, 15 is uri_segment = 3
            $config['first_link'] = 'First';
            $config['last_link'] = 'End';
            $config['next_link'] = 'Next &rarr;';
            $config['prev_link'] = '&larr; Prev';

            $this->pagination->initialize($config); //get all config pagination
            $page = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;
            $data["hslquery"] = $this->m_database->fetch_siswa($config["per_page"], $page); //send query with parameter to model fetch_siswa
            $data["paginator"] = $this->pagination->create_links(); //create pagination number

            $data["total_rows"] = $this->m_database->record_count();
            //load view
            $this->load->view('admin/home/v_home', $data);
     }
}
/* End of file c_home.php */
/* Location: ./application/controllers/c_home.php */

And then example my Views (v_home.php):

<!DOCTYPE html>
<html lang="id">
 <head>
     <title>Welcome</title>
 </head>
<body>   
<table class="table table-hover">
	<thead>
		<tr>
			<br />
			<p class="text-center">Jumlah Data: <?php echo $total_rows; ?></p>
		</tr>
		<tr>
      <th>No.</th>
      <th>No. Induk</th>
      <th>Nama Siswa</th>
      <th>Kelas</th>
      <th>Harus Bayar</th>
      <th>Tunggakan</th>
      <th>Aksi</th>
    </tr>
  </thead>
  <tbody>
  	<?php 
      $no = 1;
      foreach ($hslquery->result() as $row) 
        { 
    ?>
    <tr>
      <td><?php echo $no; ?></td>
      <td><?php echo $row->no_induk; ?></td>
      <td><?php echo $row->nama_siswa; ?></td>
      <td><?php echo $row->kelas; ?></td>
      <td><?php echo $row->jumlah; ?></td>
      <td><?php echo $row->tunggakan; ?></td>
    </tr>
    <?php 
      $no++; 
    } 
    ?>
    </tbody>
    </table>
    <?php echo $paginator; ?>
 </body>
</html>

And now, the result picture is:
CodeIgniter Pagination
And the last, if you are using bootstrap, you can add this configuration (Save on applications/Libraries/Pagination.php</b) Bootstrapt Paginations

Advertisements

12 comments

  1. the next page and next link did not work in my code. the page tell that the next page not found. i change the “$config[“base_url”] = base_url(“c_home/index”);” into “$config[“base_url”] = base_url(“index.php/c_home”);” but nothings happen the page tell the same error page not found. how can i fix this. thanks in reply..

    1. Sory for long answer.. If you’re using nginx for the server web. You can use site_url and not use base_url. For example from my code. I have function index() on c_home.php file. So for $config[“base_url”] = site_url(“c_home/index”). But if you’re using apache with .htaccess you can use $config[“base_url”] = base_url(“c_home/index”)

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s