Menu

Pagination In CodeIgniter With Demo

Pagination In CodeIgniter With Demo

Pagination is one of the most frequently used features for web applications. So We have share in this tutorial, how we can implement CodeIgniter pagination and enhance it further as per our need. Pagination is the process of dividing a document into discrete pages.

Step 1: Create the database and Table- DB Name: pagination_DB, Table Name:employee
For this tutorial, you need a MySQL database with the following table:

//Table structure for table `employee`
CREATE TABLE `employee` (
`id` int(11) NOT NULL AUTO_INCREMENT COMMENT 'Primary Key',
`name` varchar(255) NOT NULL COMMENT 'Employee Name',
`email` varchar(255) NOT NULL COMMENT 'Email Address',
`salary` float(10,2) NOT NULL COMMENT 'Employee Salary',
`age` int(11) NOT NULL COMMENT 'Employee Age',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 COMMENT='datatable demo table' AUTO_INCREMENT=1;

//Dumping data for table `employee`

INSERT INTO `employee` (`id`, `name`, `email`, `salary`, `age`) VALUES
(1, 'Nixon Tiger', 'tiger@techarise.com', 3208000.00, 61),
(2, 'Garrett Winters', 'winters@techarise.com', 170750.00, 63),
(3, 'Ashton Cox', 'cox@techarise.com', 86000.00, 66),
(4, 'Cedric Kelly', 'kelly@techarise.com', 433060.00, 22),
(5, 'Airi Satouy', 'airi@techarise.com', 162700.00, 33),
(6, 'Brielle Williamson', 'will@techarise.com', 372000.00, 61),
(7, 'Herrod Chandler', 'herrod@techarise.com', 137500.00, 59),
(8, 'Rhona Davidson', 'rd@techarise.com', 327900.00, 55),
(9, 'Colleen Hurst', 'colleen@techarise.com', 205500.00, 39),
(10, 'Sonya Frost', 'frost@techarise.com', 103600.00, 23),
(11, 'John Philip', 'filip@techarise.com', 26584.00, 25),
(12, 'Sam Wood', 'sam@techarise.com', 26584.00, 27);


Step 2: Initialization CodeIgniter pagination library
Initialization CodeIgniter pagination library (file name: pagination.php) “application/config/” folder.

if (!defined('BASEPATH'))
exit('No direct script access allowed');
/**
* Pagination Config
*
* Just applying codeigniter's standard pagination config with twitter
* bootstrap stylings
*
* @author TechArise Team
* @link http://codeigniter.com/user_guide/libraries/pagination.html
* @email info@techarise.com
*
* @file pagination.php
* @version 1.0.0.1
* @date 24/09/2017
*
* Copyright (c) 2017
*/
/* -------------------------------------------------------------------------- */

$config['per_page'] = 10;
$config['num_links'] = 2;

$config['use_page_numbers'] = TRUE;
$config['page_query_string'] = FALSE;

$config['query_string_segment'] = '';
$config['full_tag_open'] = '
    ';
    $config['full_tag_close'] = '
';

$config['first_link'] = '« First';
$config['first_tag_open'] = '';

$config['last_link'] = 'Last »';
$config['last_tag_open'] = '';

$config['next_link'] = 'Next →';
$config['next_tag_open'] = '';

$config['prev_link'] = '← Previous';
$config['prev_tag_open'] = '';

$config['cur_tag_open'] = '
  • ';
    $config['cur_tag_close'] = '
  • ';

    $config['num_tag_open'] = '
  • ';
    $config['num_tag_close'] = '
  • ';

    $config['anchor_class'] = 'follow_link';
    ?>

    per_page: Refers to the number of how many entries will be shown on each page.
    base_url: Refers to the paginated url base.
    total_rows: Total numer of entries in database.
    use_page_numbers: Refers whether we want to use page number(1,2,3..) in the url or entry id(1,10, 20…) on uri segment.

    Step 3: Create Model
    Create a model file named Employee_model.php inside “application/models” folder.

    /*
    * To change this license header, choose License Headers in Project Properties.
    * To change this template file, choose Tools | Templates
    * and open the template in the editor.
    */

    /**
    * Description of Employee Model
    *
    * @author TechArise Team
    *
    * @email info@techarise.com
    */
    defined('BASEPATH') OR exit('No direct script access allowed');

    class Employee_model extends CI_Model {
    // Declare variables
    private $_limit;
    private $_pageNumber;
    private $_offset;
    // setter getter function
    public function setLimit($limit) {
    $this->_limit = $limit;
    }

    public function setPageNumber($pageNumber) {
    $this->_pageNumber = $pageNumber;
    }

    public function setOffset($offset) {
    $this->_offset = $offset;
    }
    // Count all record of table "employee" in database.
    public function getAllEmployeeCount() {
    $this->db->from('employee');
    return $this->db->count_all_results();
    }
    // Fetch data according to per_page limit.
    public function employeeList() {
    $this->db->select(array('e.id', 'e.name', 'e.email', 'e.salary', 'e.age'));
    $this->db->from('employee as e');
    $this->db->limit($this->_pageNumber, $this->_offset);
    $query = $this->db->get();
    return $query->result_array();
    }

    }

    ?>

    Load "pagination" class in controller.
     

    Step 4: Create controllers
    Create a controllers file named Employee.php inside “application/controllers” folder.

    /*
    * To change this license header, choose License Headers in Project Properties.
    * To change this template file, choose Tools | Templates
    * and open the template in the editor.
    */

    /**
    * Description of Employee Controller
    *
    * @author TechArise Team
    *
    * @email info@techarise.com
    */
    defined('BASEPATH') OR exit('No direct script access allowed');

    class Employee extends CI_Controller {

    //Load libraries in Constructor.
    public function __construct() {
    parent::__construct();
    // load pagination library
    $this->load->library('pagination');
    // load Employee Model
    $this->load->model('Employee_model', 'employee');
    }
    // listing recards
    public function index() {
    $config['total_rows'] = $this->employee->getAllEmployeeCount();
    $data['total_count'] = $config['total_rows'];
    $config['suffix'] = '';

    if ($config['total_rows'] > 0) {
    $page_number = $this->uri->segment(3);
    $config['base_url'] = base_url() . 'employee/index/';
    if (empty($page_number))
    $page_number = 1;
    $offset = ($page_number - 1) * $this->pagination->per_page;
    $this->employee->setPageNumber($this->pagination->per_page);
    $this->employee->setOffset($offset);
    $this->pagination->cur_page = $offset;
    $this->pagination->initialize($config);
    $data['page_links'] = $this->pagination->create_links();
    $data['employeeInfo'] = $this->employee->employeeList();
    }
    // load view
    $this->load->view('employee/index', $data);
    }
    }
    ?>

    Step 5: Create views
    Create a views file named index.php inside “application/views/employee” folder.

    $this->load->view('templates/header');
    ?>












    if (isset($employeeInfo) && !empty($employeeInfo)) {
    foreach ($employeeInfo as $key => $element) {
    ?>






    }
    } else {
    ?>






    Name Email Salary Age
    There is no employee.







    $this->load->view('templates/footer');
    ?>

    Demo  [sociallocker] Download[/sociallocker]

    Ads middle content1

    Ads middle content2