How to use Codeigniter pagination

The pagination and the CodeIgniter framework

When you want to present a long list of data, the pagination becomes a useful way to give your users the best interactivity. This makes pagination supported as a class in CodeIgniter framework. Unfortunately, despite of what developers said on the user guide, using this class is not easily to understand for many people. In this post, I will introduce the basic of how to use CodeIgniter pagination class via an example. You can see my demo at the demo page

Our data source

Create a table in your database as below:

CREATE TABLE 'demo_page' (
'id' INT NOT NULL AUTO_INCREMENT PRIMARY KEY ,
'value_code' CHAR( 6 ) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL ,
'quantity' INT NOT NULL
)

Our table has three field: id – the id of each record; value_code – the identifying string of each item, in this case, it contains six capital Latin letters, and quantity – the number of each item in the storehouse.

The Model

In this demo, our Model involves two method: getting the total number of record in the data source and getting an certain of number of records based on passed argument. Create a file called demo_page.php in your ./application/models/ folder and make its content as these line:

<?php
class Demo_page extends CI_Model {
    function __construct() {
        parent::__construct();
        $this->load->library('database');
    }

    function count_items(){
        return $this->db->count_all('demo_page');
    }

    function get_items($limit, $offset){
        $data = array();
        $this->db->limit($limit, $offset);
        $Q = $this->db->get('demo_page');
        if($Q->num_rows() > 0){
            foreach ($Q->result_array() as $row){
                $data[] = $row;
            }
        }
        $Q->free_result();
        return $data;
    }
}

The Controller

What we do in the Controller is loading libraries and models, configuring the pagination class, getting data from database and calling the view. In your ./application/controllers/ folder, create a new php file as below and name it demo.php

<?php
class demo extends CI_Controller {

    function __construct() {
        parent::__construct();
    }

    function index() {
        // return to previous page
        redirect(base_url());
    }

    function ci_pagination() {
        $this->load->library('pagination');
        $this->load->library('table');
        $this->load->model('demo/Demo_page');

        $result_per_page = 5;  // the number of result per page

        $config['base_url'] = base_url() . '/demo/ci_pagination/';
        $config['total_rows'] = $this->Demo_page->count_items();
        $config['per_page'] = $result_per_page;
        $this->pagination->initialize($config);

        $datatable = $this->Demo_page->get_items($result_per_page, $this->uri->segment(3));

        $this->load->view('demo/ci_pagination', array(
            'datatable' => $datatable,
            'result_per_page' => $result_per_page
        ));
    }
}

I will explain these lines after we finish our View

The View

I use this View to present our data in the tabular format:

<!doctype html>
<html>
    <head>
        <title>Demo for CodeIgniter Pagination :: Demo cho việc phân trang trong CodeIgniter</title>
        <meta charset="utf-8" />
        <style>
            td{
                text-align: center;
            }
            td{
                border-top: 1px solid #ccc;
            }
            table{
                margin: 1em;
            }
        </style>
    </head>
    <body>
        <h1>Demo for CodeIgniter Pagination</h1>
        <?php

        // generate the table
        $this->table->set_heading('ID', 'ITEM CODE', 'QUANTITY');
        echo $this->table->generate($datatable);

        // generate the page navigation

        echo $this->pagination->create_links();
        ?>
    </body>
</html>

Now, we can test our demo with by typing this URL into your browser https://Your_CI_App_folder_URL/demo/ci_pagination

CodeIgniter pagination configuration

Now, when our demo runs perfectly, let look at our Controller again to learn what those lines do.

The first three lines loads the necessary modules. In this demo, we needs the pagination library (of course) to generate the navigation links, the table library to quickly generate a table based on what we got from our source and the demo_page model to communicate with the database.

The, we define a variable contains how many item will be display in a page. After that, in the next four lines, we configure the pagination class before initiate it:

  • $config[‘base_url’]: the full URL to the controller class/function contains the pagination.
  • $config[‘total_rows’]: the total number of records you want to display. This our demo, we will display all record in the table and get it by count_items method of the Model.
  • $config[‘per_page’]: the number of records you want to display in each page. You had better use a constant or a variable to make it easily to be modified later.

After initiate the pagination, we just simply echo the $this->pagination->create_links(); result in our View.

Conclusion

After reading this post, I hope you can understand how to use the useful pagination class of CodeIgniter. Remember, this utility does not play a roll as a Model, its objective is generate the navigation links and write the URL. You can learn more about it in the CodeIgniter user guide.

2 comments

Leave a Reply

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