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: https://ellislab.com/codeigniter/user-guide/libraries/pagination.html.

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

<?php
Public function test_page($offset) {
  $this->load->library('pagination');
  $config['base_url'] = 'http://www.yourwebsite.com/index.php/test_page/';
  $config['total_rows'] = 200;
  $config['per_page'] = 20; 
  $this->pagination->initialize($config); 
}
?>

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.

<?php
  $this->load->model(‘my_model’);
  $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:

<?php
   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: http://yoursite.com/index.php/my_controller/test_page/20
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:

<?php
  Public function get_my_data($limit, $offset) {
  $query = $this->db->get(‘my_table’, $limit, $offset);
  return$query->result_array();
}
?>

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 social-evolvea.com 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–

    (1)

    public function forum_detail_info() {
    ——
    }

    (2)

    public function forum_detail_info($post_id) {
    —-}

    • Shyam says:

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

      example

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

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

      return$query->result_array();

      }

      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:

    load->helper(‘url’);

    $this->load->model(‘Create_firm_m’);

    $this->load->library(‘pagination’);

    }

    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;

    $this->load->library(‘pagination’);

    $autoload[‘config’]=array(‘pagination’);

    $this->pagination->initialize($config);

    $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 */

    Model:

    db->get(‘tbl_firm’,$limit,$offset);

    return $query->result_array();

    }

    public function record_count()

    {

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

    }

    public function fetch_firms($limit,$start)

    {

    $this->db->limit($limit,$start);

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

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

    {

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

    {

    $data[]=$row;

    }

    return $data;

    }

    return false;

    }

    ///////////////////////////////////////////////////////////

    ?>

    View:

    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. ghghhjk says:

    haha

  6. Roland Walferman says:

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

  7. 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 *