Pagination in codeigniter step by step

Pagination in codeigniter step by step

Pagination in codeigniter step by step : -

In This post we implement codeigniter pagination  with easy step and flexible way using codeigniter pagination library . Use this code in Your project and use pagination.

database creat phpcooker_script and import this 

-- Database: `phpcooker_script`

-- --------------------------------------------------------

-- Table structure for table `cipagination`

  `cipagination_id` int(10) NOT NULL AUTO_INCREMENT,
  `cipagination_title` varchar(120) NOT NULL,
  `cipagination_description` text NOT NULL,
  `cipagination_status` enum('0','1') NOT NULL,
  PRIMARY KEY (`cipagination_id`)

-- Dumping data for table `cipagination`

INSERT INTO `cipagination` (`cipagination_id`, `cipagination_title`, `cipagination_description`, `cipagination_status`) VALUES
(1, 'What is PHP ', 'is a widely-used open source general-purpose scripting language that is especially suited for web development and can be embedded into HTML', '1'),
(2, 'What is C ', 'C is a computer programming language. That means that you can use C to create lists of instructions for a computer to follow', '1'),
(3, 'What is c++ ', 'C++ is a general-purpose object-oriented programming (OOP) language, developed by Bjarne Stroustrup, and is an extension of the C language', '1'),
(4, 'What is java ', 'Java is a programming language and computing platform first released by Sun Microsystems in 1995.', '1'),
(5, 'What is javascript ', 'Javascript is a dynamic computer programming language. It is lightweight and most commonly used', '1'),
(6, 'What is HTML ', 'HTML is the standard markup language for creating Web pages.', '1');

Model Pagination_model.php

defined("BASEPATH") or exit("no direct script allowed");
class Pagination_model extends CI_Model{
    function __construct(){
    public function allrecord(){
        $rs = $this->db->get();
        return $rs->num_rows();
    public function data_list($limit,$offset){
        $rs = $this->db->get();
        return $rs->result_array();


Controller Pagination.php

defined('BASEPATH') OR exit('No direct script access allowed');
class Pagination extends CI_Controller {
    public function __construct(){
        parent :: __construct();
    public function index()
        $allrecord = $this->pgn->allrecord();
        $baseurl =  base_url().$this->router->class.'/'.$this->router->method;
        $paging['base_url'] =$baseurl;
        $paging['total_rows'] = $allrecord;
        $paging['per_page'] = 2;
        $paging['uri_segment']= 3;
        $paging['num_links'] = 2;
        $paging['first_link'] = 'First';
        $paging['first_tag_open'] = '<li>>';
        $paging['first_tag_close'] = '</li>';
        $paging['num_tag_open'] = '<li>';
        $paging['num_tag_close'] = '</li>';
        $paging['prev_link'] = 'Prev';
        $paging['prev_tag_open'] = '<li>';
        $paging['prev_tag_close'] = '</li>';
        $paging['next_link'] = 'Next';
        $paging['next_tag_open'] = '<li>';
        $paging['next_tag_close'] = '</li>';
        $paging['last_link'] = 'Last';
        $paging['last_tag_open'] = '<li>';
        $paging['last_tag_close'] = '</li>';
        $paging['cur_tag_open'] = '<li class="active"><a href="javascript:void(0);">';
        $paging['cur_tag_close'] = '</a></li>';
        $data['limit'] = $paging['per_page'];
        $data['number_page'] = $paging['per_page']; 
        $data['offset'] = ($this->uri->segment(3)) ? $this->uri->segment(3):'0';    
        $data['nav'] = $this->pagination->create_links();
        $data['datas'] = $this->pgn->data_list($data['limit'],$data['offset']);


View pagination.php

<!DOCTYPE html>
<html lang="en">
  <title>codeidniter pagination example</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="">
  <script src=""></script>

<div class="container">
  <h2>CodeIdniter Pagination Example</h2>
  <table class="table">
foreach($datas as $data)
      <td><?php echo $data["cipagination_title"]; ?></td>
      <td><?php echo $data["cipagination_description"]; ?></td>

.pagination-dive li {
    list-style: none;
    display: inline-block;
.pagination-dive a:hover, .pagination-dive .active a {
    background: #040404;

.pagination-dive a {
    display: inline-block;
    height: initial;
    background: #939890;
    padding: 10px 15px;
    border: 1px solid #fff;
    color: #fff;
  <div class="pagination-dive" >
<?php echo $nav; ?>



Download Here

Total Coments (3)

  1. user
    • 26 Oct 2017

    thanks alot , it really helps

  2. alpesh
    • 21 Nov 2017


  3. siji lestin
    • 05 Mar 2019

    Tanks,very much really helps.awesome