November 1, 2014 9:19 am

How to create Pagination in CodeIgniter Framework

CodeIgniter has been one of the most powerful frameworks out there for a while and a lot of PHP developers still prefer it as the best solution for small and medium sized projects. It’s also recommended for the beginner developers who are starting to learn frameworks to start with. In this article we will demonstrate how to do pagination with CodeIgniter and we will also talk about.

How to create Pagination in CodeIgniter Framework

Note: This article requires knowledge of CodeIgniter framework. It’s mainly aimed for developers using the framework or developers who are in the process of learning it.

What happens in the controller can be described in the following 3 steps:

  • Loading the pagination class.
  • Set the values of the pagination array (where most the tricky situations can happen).
  • Call the initialize() function of the pagination class.

Note: We won’t discuss all the possible parameters of the pagination array because we want to focus on some of the tips that could save you time and effort. For the full reference you can see the official CodeIgniter page here:

The following code snippet gives an example for the controller function of the page that we want to load:

Public function test_page($offset) {
  $config['base_url'] = '';
  $config['total_rows'] = 200;
  $config['per_page'] = 20; 

In order to display the results in the view you should set a parameter for the $data array that contains the result returned from the database. Here in this example we assume that we have a model named “my_model” which contains a function named “get_my_data”. And this function returns the result array of the records that you want to retrieve from the database. We also assume that we have a view called “my_view” where we will display the results.

  $data[‘my_result’] = $this->my_model->get_my_data($config['per_page'], $offset);
  $this->load->view(‘my_view’, $data);

Now our work is all done with the controller. Now we should move to the view file. After fetching the data that you sent from the controller using HTML in the view file, you need to call the following function in order to display the following:

   echo $this->pagination->create_links();

Now the work is done and the data that you have just loaded form the database will appear in the view file and the pagination links will just look like that:

« First < 1 2 3 4 5 6 7 8 9 10 > Last »

If we clicked the link to the second page the URL that will be loaded will be:
Notice that the ‘20’ URL segment is the $offset variable that will be used in theget_my_data() model function to load the number of database rows that we want. The function should look like the following code snippet:

  Public function get_my_data($limit, $offset) {
  $query = $this->db->get(‘my_table’, $limit, $offset);

In our example, only links to ten pages will appear because we have previously set the $config[‘total_rows’] parameter to 200 and the $config[‘per_page’] parameter to 20.

In the next part we will talk about the best practices for providing pagination links for all the rows of the database table and we will dive more into the CodeIgniter pagination class.

Author Amr Abdou

A full-stack web developer, Linux enthusiast, freelance tech writer & open-source supporter. Loves finding quit corners in public places or cafes accompanied by his laptop to try changing the world from there. You can check his latest works at or follow him on facebook, twitter, Linkedin or Google+

Tutorial Categories:

9 responses to “How to create Pagination in CodeIgniter Framework”

  1. Dinesh Kumar says:

    nice tutorial

  2. Shahab uddin says:

    how to create pagination in php codeigniter with parameter—

    I can create pagination for those function (1) that has no parameter,But i can not do for those function that has parameter (2)like–


    public function forum_detail_info() {


    public function forum_detail_info($post_id) {

    • Shyam says:

      just pass the third parameter($post_id) to model function to get desired records


      Public function get_my_data($limit, $offset, $post_id) {

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



      same for total count

      function record_count($post_id)
      return $this->db->count_all(‘table_name’,array(‘column_name’=>$post_id));


  3. Anu says:

    hello, i am getting an error, Kindly Help me

    Fatal error: Call to undefined method Pagination::initialize() in C:wampwwwClientapplicationcontrollerswelcome.php on line 20

    here is my controller:





    public function example1() {

    $config = array();

    $config[“base_url”] = base_url() . “welcome/example1”;

    $config[“total_rows”] = $this->Create_firm_m->record_count();

    $config[“per_page”] = 20;

    $config[“uri_segment”] = 3;




    $page = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;

    $data[“results”] = $this->tbl_firm->

    fetch_firms($config[“per_page”], $page);

    $data[“links”] = $this->pagination->create_links();

    $this->load->view(“welcome_message”, $data);



    /* End of file welcome.php */

    /* Location: ./application/controllers/welcome.php */



    return $query->result_array();


    public function record_count()


    return $this->db->count_all(“tbl_firm”);


    public function fetch_firms($limit,$start)



    $query = $this->db->get(‘tbl_firm’);

    if ($query->num_rows()>0)


    foreach($query->result() as $row)




    return $data;


    return false;





    Firms Registered

    firm_name; ?>

  4. Ahmad Sayeed says:

    hello nice article, please keep sharing , and i am applying same pagination config to every controller , how to make reusable so can use multiple controller and views , please guide me with example bcoz i am new to CI

  5. Roland Walferman says:

    Hello sir in your code line number 5 is contain wrong statement!

  6. Martin Friis says:

    nice article, I also would recommend ArtDesignUI pagination framework because I am using it for a long time and I am very satisfied. you can check it if you want

Leave a Reply

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