A another feature why use framework for site that we can easily create pagination in codeigniter it’s own pagination library with less code and less time.

Pagination :-

When we are working with thousand or millions of record or working with bunch of data and need to show them then we need to divide into more pages to show all data in parts. to prevent millions of records show in a single page. records showing in parts is a good functionality to prevent query fails and long time process.For this we need to use pagination (refers to links that allows you to navigate from page to page).
We can easily create pagination in codeigniter with it’s own library. codeigniter provide an easy and customizable pagination. below we will see how to create pagination in codeigniter with showing total rows or record count and use query string with pagination in codeigniter.

Use pagination in codeigniter :-

Model :- create a “pagmodel.php” in application/models/pagmodel.php with adding below code.

if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class PagModel extends CI_Model
{
    public function __construct() {
       parent::__construct();
    }
    public function total_count() {
       return $this->db->count_all("users");
    }
    public function get_users($limit, $start) {
      $this->db->limit($limit, $start);
      $query = $this->db->get("users");
      if ($query->num_rows() > 0) {
        return $query->result_array();
      }
      return false;
   }
}

Controller :- create a “pag.php” in application/controllers/pag.php with adding below code.

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Pag extends CI_Controller {
    /**
     * Index Page for this controller.
     */
    public function __construct() {
        parent:: __construct();
        $this->load->helper("url");
        $this->load->model('PagModel', 'pag');
        $this->load->library("pagination");
    }
    public function index( $offset = 0 ) {
    $offset = ($this->uri->segment(3) != '' ? $this->uri->segment(3): 0);
    $config['total_rows'] = $this->pag->total_count();;
    $config['per_page']= 4;
    $config['first_link'] = 'First';
    $config['last_link'] = 'Last';
    $config['uri_segment'] = 3;
    $config['base_url']= base_url().'/pag/index';
    $config['suffix'] = '?'.http_build_query($_GET, '', "&");
    $this->pagination->initialize($config);
    $this->data['paginglinks'] = $this->pagination->create_links();
        // Showing total rows count
    if($this->data['paginglinks']!= '') {
      $this->data['pagermessage'] = 'Showing '.((($this->pagination->cur_page-1)*$this->pagination->per_page)+1).' to '.($this->pagination->cur_page*$this->pagination->per_page).' of '.$this->pagination->total_rows;
    }  
    $this->data['result'] = $this->pag->get_users($config["per_page"], $offset); 
    $this->load->view('pag/index', $this->data);
   }
}

View :- create a “index.php” in application/views/pag/index.php with adding below code.

<?php if(is_array($result) && sizeof($result)>0){ ?>
<div class="pagination" style="float:right;"> <?php echo $paginglinks; ?></div>
<div class="pagination" style="float:left;"> <?php echo (!empty($pagermessage) ? $pagermessage : ''); ?></div>
<table width="100%" cellspacing="0" cellpadding="4" border="0" class="data">
  <tbody>
    <tr>
      <th width="7%">Ref.</th>
      <th width="10%">Firstname</th>
      <th width="10%">Surname</th>
      <th width="10%">Email</th>
      <th width="10%">App Date</th>
    </tr>
    <?php foreach($result as $key=>$v) {?>
        <tr class="">
        <td><?php echo $v['u_ref']?></td>
        <td><?php echo $v['u_firstname']?></td>
        <td><?php echo $v['u_lastname']?></td>
        <td><?php echo $v['u_email']?></td>
        <td><?php echo date('d/m/Y H:i:s');?></td>
        <tr>
      <?php }?>
  </tbody>
</table>
<div class="pagination" style="float:right;"> <?php echo $paginglinks; ?></div>
<div class="pagination" style="float:left;"> <?php echo (!empty($pagermessage) ? $pagermessage : ''); ?></div>
<?php }else{?>
<p align="center" style="padding-top:20px;">
  <?php  echo 'No Record Found!' ;?>
</p>
<?php }?>

Now you can see pagination with total rows count display. above example working with model below we will see how to create pagination in codeigniter with model or using custom query in codeigniter.

pagination using custom query codeigniter :-

For this we need to use custom query in controller so there is no use of model or without model we can use query directly in controller. so only need to change controller code.

Controller :- create a “pag.php” in application/controllers/pag.php with adding below code.

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Pag extends CI_Controller {
    /**
     * Index Page for this controller.
     */
    public function __construct() {
        parent:: __construct();
        $this->load->helper("url");
        $this->load->library("pagination");
    }
    public function index( $offset = 0 ) {
    $per_page = 4; 
    $qry = "SELECT * FROM users ORDER BY `u_id` DESC";
    $offset = ($this->uri->segment(3) != '' ? $this->uri->segment(3):0);
    $config['total_rows'] = $this->db->query($qry)->num_rows();
    $config['per_page']= $per_page;
    $config['first_link'] = 'First';
    $config['last_link'] = 'Last';
    $config['uri_segment'] = 3;
    $config['base_url']= base_url().'/pag/index';
    $config['suffix'] = '?'.http_build_query($_GET, '', "&");
    $this->pagination->initialize($config);
    $this->data['paginglinks'] = $this->pagination->create_links();   
    $this->data['per_page'] = $this->uri->segment(3);     
    $this->data['offset'] = $offset ;
    if($this->data['paginglinks']!= '') {
      $this->data['pagermessage'] = 'Showing '.((($this->pagination->cur_page-1)*$this->pagination->per_page)+1).' to '.($this->pagination->cur_page*$this->pagination->per_page).' of '.$this->pagination->total_rows;
    }  
    $qry .= " limit {$per_page} offset {$offset} ";
    $this->data['result'] = $this->db->query($qry)->result_array();  
    $this->load->view('pag/index', $this->data);
   }
}

Now you are all set with pagination in codeigniter or pagination in codeigniter without using model or pagination in codeigniter using custom query.

How to get query string on first click in codeigniter :-

Default you will see on first click redirect to first page without query string or offset. now we will see get query string on first click in codeigniter pagination.

add below code before pagination initialize:-

$config['first_url'] = $config['base_url'].$config['suffix'];
 
or
 
$config['first_url'] = base_url().'?'.http_build_query($_GET, '', "&");