This tutorial will show you how to generate pagination using Codeigniter. pagination is recommended when you have lots of data coming from database or external sources and you want to show data which do not fit into a single because it looks ugly and irritating to scroll down the page at bottom.

Read also Effective pagination example in jsf 2  and Create an effective pagination in PHP and MySQL

Pagination Class

CodeIgniter’s Pagination class is very easy to use, and it is 100% customizable, either dynamically or via stored preferences.

If you are not familiar with the term “pagination”, it refers to links that allows you to navigate from page to page, like this:

« First  < 1 2 3 4 5 >  Last »

For more information please read https://www.codeigniter.com/userguide3/libraries/pagination.html

Prerequisites

Netbeans 8.1
XAMPP in Windows
Codeigniter 3.0.6


Configure XAMPP and Netbeans

From Netbeans IDE go to Tools->Options. Click on PHP. Now on tab “General” browse the file for “PHP 5 Interpreter”. The php interpreter file generally placed inside the <physical drive in Windows OS>:xamppphpphp.exe

Configure Codeigniter and Netbeans

Create a new PHP project in Netbeans. Then remove the index.php file from the newly created project. Now copy the extracted files from Codeigniter 3.0.6 to the newly created project directory.

Directory Structure

A typical directory structure for the project would be as shown below. Here assests directory will contain static resources like css, js, images.

codeigniter pagination

Step 1. Now modify <root directory>/application/config/autoload.php file for auto-loading html, url, file, form and session

$autoload['helper'] = array('html', 'url', 'file', 'form');

$autoload['libraries'] = array('database', 'session');

Step 2. Now modify <root directory>/application/config/config.php file to define encryption key

$config['encryption_key'] = '2d8+e6]K0?ocWp7&`K)>6Ky"|.x|%nuwafC~S/+6_mZI9/17y=sKKG.;Tt}k';

Step 3. Create a view file under <root directory>/application/views which will be used to display blogs with pagination links at bottom

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title>Codeigniter Pagination Example</title>
        <link rel="stylesheet" type="text/css" href="<?php echo base_url(); ?>assets/css/pagination.css">
        <style type="text/css">

            ::selection { background-color: #E13300; color: white; }
            ::-moz-selection { background-color: #E13300; color: white; }

            body {
                background-color: #fff;
                margin: 40px;
                font: 13px/20px normal Helvetica, Arial, sans-serif;
                color: #4F5155;
            }            

            #body {
                margin: 0 15px 0 15px;
            }

            #container {
                margin: 10px;
                border: 1px solid #D0D0D0;
                box-shadow: 0 0 8px #D0D0D0;
            }

            h1 {
                margin-left: 15px;
            }

            .error {
                color: #E13300;
            }

            .success {
                color: darkgreen;
            }
        </style>
    </head>
    <body>
        <div id="container">
            <h1>CodeIgniter Pagination Example</h1>            
            <div id="body">
                <?php
                foreach ($blog_list->result() as $blog) {
                    ?>
                    <div class="post">
                        <h2 class="title"><?php echo $blog->blog_title; ?></h2>
                        <p class="meta">
                            <?php
                            echo $blog->blog_date;
                            ?>
                        <div class="entry">
                            <p><?php echo $blog->blog_content; ?></p>
                        </div>
                    </div>
                    <?php
                }
                if (strlen($pagination)) {
                    echo $pagination;
                }
                ?>
            </div>
        </div>
    </body>
</html>

Step 4. Create a Controller class under <root directory>/application/controllers for handling client’s request and response

<?php

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

/**
 * Description of Blog
 *
 * @author https://www.roytuts.com
 */
class Blog extends CI_Controller {

    function __construct() {
        parent::__construct();
        $this->load->model('blogmodel');
        $this->load->library('form_validation');
    }

    public function index($offset = 0) {
        //how many blogs will be shown in a page
        $limit = 5;
        $result = $this->blogmodel->get_blogs($limit, $offset);
        $data['blog_list'] = $result['rows'];
        $data['num_results'] = $result['num_rows'];
        // load pagination library
        $this->load->library('pagination');
        $config = array();
        $config['base_url'] = site_url("blog/index");
        $config['total_rows'] = $data['num_results'];
        $config['per_page'] = $limit;
        //which uri segment indicates pagination number
        $config['uri_segment'] = 3;
        $config['use_page_numbers'] = TRUE;
        //max links on a page will be shown
        $config['num_links'] = 5;
        //various pagination configuration
        $config['full_tag_open'] = '<div class="pagination">';
        $config['full_tag_close'] = '</div>';
        $config['first_tag_open'] = '<span class="first">';
        $config['first_tag_close'] = '</span>';
        $config['first_link'] = '';
        $config['last_tag_open'] = '<span class="last">';
        $config['last_tag_close'] = '</span>';
        $config['last_link'] = '';
        $config['prev_tag_open'] = '<span class="prev">';
        $config['prev_tag_close'] = '</span>';
        $config['prev_link'] = '';
        $config['next_tag_open'] = '<span class="next">';
        $config['next_tag_close'] = '</span>';
        $config['next_link'] = '';
        $config['cur_tag_open'] = '<span class="current">';
        $config['cur_tag_close'] = '</span>';
        $this->pagination->initialize($config);
        $data['pagination'] = $this->pagination->create_links();
        $this->load->view('list_blog', $data);
    }

}

Step 5. Create a Model class under <root directory>/application/models for performing database operatins

<?php

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

/**
 * Description of BlogModel
 *
 * @author https://www.roytuts.com
 */
class BlogModel extends CI_Model {

    private $blogs = 'blogs';   // blog table

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

    //fetch blogs
    function get_blogs($limit, $offset) {
        if ($offset > 0) {
            $offset = ($offset - 1) * $limit;
        }
        $result['rows'] = $this->db->get($this->blogs, $limit, $offset);
        $result['num_rows'] = $this->db->count_all_results($this->blogs);
        return $result;
    }

}

Step 6. Now modify <root directory>/application/config/routes.php file for pointing the default controller class

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

Step 7. Now if everything is fine run the application, you will see below output in the browser

codeigniter pagination

codeigniter pagination

Download Database script and assets directory

blogs   assets

Thanks for reading.

I am a professional Web developer, Enterprise Application developer, Software Engineer and Blogger. Connect me on JEE Tutorials Twitter Facebook  Google Plus Linkedin Or Email Me

Leave a Reply

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