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`
--

CREATE TABLE IF NOT EXISTS `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`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=7 ;

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



<?php 
defined("BASEPATH") or exit("no direct script allowed");
class Pagination_model extends CI_Model{
    function __construct(){
    parent::__construct();    
        $this->load->library(array('session','cart','pagination'));
        $this->load->library('');
        $this->load->helper('url');
        $this->load->database();    
    }
    
    
    public function allrecord(){
        
        $this->db->select('*');
        $this->db->from('cipagination');
        $rs = $this->db->get();
        return $rs->num_rows();
    }
    
    public function data_list($limit,$offset){
        
        $this->db->select('*');
        $this->db->from('cipagination');
        $this->db->order_by('cipagination_id','desc');
        $this->db->limit($limit,$offset);
        $rs = $this->db->get();
        return $rs->result_array();
    }
    

}
?>

Controller Pagination.php


<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Pagination extends CI_Controller {
    public function __construct(){
        parent :: __construct();
        $this->load->model('Pagination_model',"pgn");
    }
    public function index()
    {   
    
        $allrecord = $this->pgn->allrecord();
        $baseurl =  base_url().$this->router->class.'/'.$this->router->method;
        
        $paging=array();
        $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>';
        
        $this->pagination->initialize($paging);    
        
        $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']);
        $this->load->view('pagination',$data);
    }
    
    
    
        
}

?>

View pagination.php


<!DOCTYPE html>
<html lang="en">
<head>
  <title>codeidniter pagination example</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
</head>
<body>

<div class="container">
  <h2>CodeIdniter Pagination Example</h2>
           
  <table class="table">
    <thead>
      <tr>
      <th>title</th>
    <th>description</th>
      </tr>
    </thead>
    <tbody>
<?php
foreach($datas as $data)
{
?>
      <tr>
      <td><?php echo $data["cipagination_title"]; ?></td>
      <td><?php echo $data["cipagination_description"]; ?></td>
      </tr>
      <?php     
}
    

?>
    </tbody>
  </table>
  
<style>
.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;
}
</style>
  
  <div class="pagination-dive" >
<?php echo $nav; ?>
</div>
  
</div>


</body>
</html>

smileysmileysmileysmileysmiley

Download Here

Total Coments (3)

  1. user
    • 26 Oct 2017

    thanks alot , it really helps

  2. ghklhkl
    • 24 Oct 2017

    jljlj

  3. alpesh
    • 21 Nov 2017

    awesome