Home > CodeIgniter, PHP > How To Use CodeIgniter Pagination Class with Database

How To Use CodeIgniter Pagination Class with Database

Pagination class is very useful when we are handling a very large set of data. With it, users can see a page of data sets for only 10 or 20 rows and provided with page navigation where user can go to the next and previous page or directly jump to a page number. CodeIgniter has a class for doing this, but we must make a link to the data to be paginated to make it works correctly.

This article show you how to make the link between pagination class and database class to make the pagination works on a table that has a large set of data inside it.

First, in your controller class, load the pagination class and the database class (if it was not automatically loaded by the the autoload.php configuration). In this example I use  a controller named Units that handle data from a table called units. It has 4 columns: id, name, imei, and owner.

class Units extends Controller {

    function Units()
    {
        parent::Controller();
        $this->load->library(‘pagination’);
    }

Then at the index method of the class, first we read the value of the third segment of the URI which is the current offset number to a variable called $offset. If the segment is empty the we set the value as 0.

Then we do the query to the units table to retrieve it’s records. Before retrieving the records, we set the limit and offset by calling db->limit() method. Setting this will limit our query result only $limit records starting from record number $offset.

Next we set the configuration for the pagination class, which are the base_url that points to the current controller itself, total_rows that is the number of all rows of the table, and per_page that is the number of rows per page to display read by calling the db->count_all() method. Then we create the pagination links by calling the pagination->create_links() method.

Then we fill up an array variable $data that contains the $query result of the table and the $paginator links. After that, we pass the data to the view.

    function index()
    {

        $offset=$this->uri->segment(3);

        $limit=10;

        $this->db->limit($limit, $offset);
        $query = $this->db->get(‘units’);

        $config[‘base_url’] = site_url() . ‘/units/index/’;
        $config[‘total_rows’] = $this->db->count_all(‘units’);
        $config[‘per_page’] = $limit;
        $this->pagination->initialize($config);
        $paginator=$this->pagination->create_links();

        $data = array(
            ‘query’ => $query,
            ‘paginator’=>$paginator
        );

        $this->load->view(‘units_index’,$data);
    }

In the view, we render the $query result into a table and print the pagination link at the bottom of the table.

<h3>Master Data Units</h3>
<a href="<?=site_url()?>/units/add">create</a>
<table id="box-table-a">
<tr>
    <th></th>
    <th></th>
    <th>Name</th>
    <th>Imei</th>
    <th>Owner</th>
    <th></th>
</tr>
<? foreach ($query->result() as $row) { ?>
<tr>
    <td><a href="<?=site_url()?>/units/edit/<?=$row->id?>">Edit</a></td>
    <td><a href="<?=site_url()?>/units/delete/<?=$row->id?>">Delete</a></td>
    <td><?=$row->name?></td>
    <td><?=$row->imei?></td>
    <td><?=$row->owner?></td>
    <td></td>
</tr>

<?}?>
</table>
<?=$paginator?>

Notice that in the view I put a link to create a new record that point to “units/add” URL. This is useful for adding new record and should be handled by other method in the units controller. Also I created links for deleting and editing each records, each pointing to “units/delete” and “units/edit”.

Note: the above example is only for simplicity purpose. In the real world you should use model class to access to databases.

Here is the complete list of our controller above:

<?php
class Units extends Controller {

    function Units()
    {
        parent::Controller();
        $this->load->library(‘pagination’);
    }

    function index()
    {

        $offset=$this->uri->segment(3);

        $limit=10;

        $this->db->select($select);
        $this->db->limit($limit, $offset);
        $query = $this->db->get(‘units’);

        $config[‘base_url’] = site_url() . ‘/units/index/’;
        $config[‘total_rows’] = $this->db->count_all(‘units’);
        $config[‘per_page’] = $limit;
        $this->pagination->initialize($config);
        $paginator=$this->pagination->create_links();

        $data = array(
            ‘query’ => $query,
            ‘paginator’=>$paginator
        );

        $this->load->view(‘units_index’,$data);
    }
}
?>

This is the screen shot of our controller called with http://localhost/index.php/units/index.

CodeIgniter Pagination

And here is the URL if we click the second page http://localhost/index.php/units/index/10 and the result.

CodeIgniter Pagination Page 2

To make the table nice I used CSS table style from http://www.smashingmagazine.com/2008/08/13/top-10-css-table-designs/.

  • Share/Bookmark
  1. Zonk
    March 28th, 2010 at 02:18 | #1

    bagaimana membuat ‘pagination’ untuk sub class
    misal

    http://localhost/zonk/index.php/news/sport/

    kalau kondisi anda sekarang kan cuma di fungsi index di class news, seperti ini
    http://localhost/zonk/index.php/news/

    saya coba buat jd ngaco gitu paging nya. terima kasih

  2. June 30th, 2010 at 21:50 | #2

    It didnt work.. because 1 become stuck you cant go back … BUG here

  3. October 15th, 2010 at 03:26 | #3

    Awesome Post, thanks for your useful Post. I will come back soon . Great information : filemaker web hosting

  4. October 21st, 2010 at 18:39 | #4

    Thank you for taking your time in creating this stuff! I’m going to use this in my project. Thumbs up!

  5. May 29th, 2011 at 10:07 | #5

    Hello there! Would you mind if I share your blog with my myspace group? There’s a lot of folks that I think would really enjoy your content. Please let me know. Thanks

  1. No trackbacks yet.
This site uses a Hackadelic PlugIn, Hackadelic SEO Table Of Contents 1.6.0.