How To Work With CodeIgniter Pagination

 How To Work With CodeIgniter Pagination  

Pagination is one of the most frequently used features for web applications. Wherever we have a bunch of data and need to show them as a list, we require pagination to prevent hundreds/thousands of data in a single page. As a robust framework, codeigniter provides very efficient way to handle it with its integrated support. In this tutorial, we will see, how we can implement codeigniter pagination and enhance it further as per our need.

I am assuming you have already have basic experience of working with codeigniter framework before. Also, I will be sharing only partial code snippet related to pagination functionality. If you are looking for the complete code reference. You can find it on demo example page.

Checkout The CodeIngiter Pagination Demo!


As per codeigniter’s internal structure, you can add a new php page named ‘pagination.php’ in ‘application/config’ directory and set all initialization parameters like entry per page, whether to use page number or not etc there. However, in such case, if you have some different configuration based of section, like uri segment of page number, then you will need to overwrite the existing config with them on controller function. On the other hand, if your application has more dynamic values on different sections, then it will be good idea to keep the initialization on your own helper or library function and pass necessary parameters from different section. This is what I like to use. Below is a simple codeigniter pagination initialization function:

 public function init_pagination($uri,$total_rows,$per_page=10,$segment=4){
        $ci                          =& get_instance();
        $config['per_page']          = $per_page;
        $config['uri_segment']       = $segment;
        $config['base_url']          = base_url().$uri;
        $config['total_rows']        = $total_rows;
        $config['use_page_numbers']  = TRUE;

        return $config;    

as you can see above, all keys inside ‘config’ associative array are defined by codeigniter and we need to pass necessary values for them. Brief on each item are given below:

  • ‘per_page’: Refers to the number of how many entries will be shown on each page.
  • base_url: Refers to the paginated url base.
  • total_rows: Total numer of entries in database.
  • use_page_numbers: Refers whether we want to use page number(1,2,3..) in the url or entry id(1,10, 20…) on uri segment.

Read The Complete CodeIgniter Tutorials Series By

Retrieve Data Range From Database:

I am using a simple contact form database table for this tutorial:
simple mysql contact table structure
If you are a regular reader of my blog, you may know that I normally use doctrime ORM in codeigniter
. With use of doctrine model plugin for codeigniter, it became a lot more easier to retrieve the paginated data, as the php code example below:

$contact_messages = $this->messagemodel->get_by_range((($page-1) * $pagingConfig['per_page']),$pagingConfig['per_page']);

where ‘messagemodel’ is a simple codeigniter model extending the doctrine CRUD model as below:

class Messagemodel extends My_DModel{
    function __construct() {

If you are not comfortable using doctrine, you can use the following model function to retrieve data using codeigniter active record class:

function get_by_range(){
    $query = $this->db->get('PdMessages', $pagingConfig['per_page'], (($page-1) * $pagingConfig['per_page']);
    return $query->result();

Showing Paginated Links On View:

To show pagination links, it is fairly easy to use. After you show current page’s data, just use a simple function as below:


Integrate Twitter Bootstrap Styles With Codeigniter Pagination Links:

well, you will may always want to customize the generated links to add some beautiful styles. As twitter bootstrap being the most popular front-end css framework now a days, here is the code snippet to integrate its style to our pagination. Set the following config items before initialization of pagination library:

$config['first_tag_open'] = $config['last_tag_open']= $config['next_tag_open']= $config['prev_tag_open'] = $config['num_tag_open'] = '<li>';
        $config['first_tag_close'] = $config['last_tag_close']= $config['next_tag_close']= $config['prev_tag_close'] = $config['num_tag_close'] = '</li>';
        $config['cur_tag_open'] = "<li><span><b>";
        $config['cur_tag_close'] = "</b></span></li>";

Also, in view, use the code as follows:

<div class="pagination">

And you should be fine with bootstrap styled pagination links :) .

Checkout The CodeIngiter Pagination Demo!

Final Words:

Hope this simple codeigniter pagination tutorial will help you in some aspect in the way to your codeigniter application development. Don’t hesitate to ask me if you are have any question or issue in understanding the tutorial. Happy coding :)


  1. Nisha says

    Your demo of Pagination is correct as i have need of this but the code you explained is not clear to me.

  2. says

    {$pagination_helper->create_links()} should not be in view, instead put the creeate link in controler like this $data[“pagination_helper”] = $this->pagination->create_links();
    then in view

Leave a Reply