How to Use Pagination Library in Codeigniter ?

How to Use Pagination Library in Codeigniter ?

This tutorial illustrates how to use pagination library in codeigniter application.In our project where we have a database with heavy data, pagination plays a key role to show data in a very clean manner and less time execution. Pagination is simply useful to take users in a pages of result. Codeigniter pagination library is very easy to use rather than any extra effort. With the help of this example you can easily use pagination into your codeigniter application.Let see with an example of pagination.

I am assuming that you already know installation of codeigniter.If any problem you having in installation then you can go through this link How to install and setup codeigniter framework.

1.Database table creation

First of all create a database table with the help of below sql. This table will contain lots of data so that we can understand how pagination really works.

CREATE TABLE `tbl_user` (
`id` int(11) NOT NULL ,
`name` varchar(255) NOT NULL,
`mobile` varchar(255) NOT NULL,
`address` varchar(255) NOT NULL,

2.Controller for Pagination configuration

Next, we need to create a controller named UserListController.php inside application/controllers directory where we will load pagination library and put all pagination configuration. Please find below the code.


defined('BASEPATH') OR exit('No direct script access allowed');
class UserListController extends CI_Controller {
public function __construct()
$this->load->library('Pagination'); /*load pagination library */
public function index() {
$total_rows = $this->db->from('tbl_user')->get()->num_rows();
$config = $this->pagination_configuration(base_url("users"), $total_rows, 10, 2, 2, true);
$page = ($this->uri->segment(2)) ? $this->uri->segment(2) : 0;
$page_num = $page-1;
$page_num = ($page_num<0)?'0':$page_num;
$page = $page_num*$config["per_page"];
$data["links"] = $this->pagination->create_links();
//Pagination ends

$obj_result = $this->userlistmodel->fetch_data($config["per_page"], $page);
$data['result'] = $obj_result;
$data["total_rows"] = $total_rows;
$this->load->view('userlistview', $data);
public function pagination_configuration($base_url, $total_rows, $per_page='10', $uri_segment='2', $num_links='2', $use_page_numbers=TRUE) {
$config = array();
$config["base_url"] = $base_url;
$config["total_rows"] = $total_rows;
$config["per_page"] = $per_page;
$config["uri_segment"] = $uri_segment;
$choice = $total_rows/$per_page;
$config['num_links'] = round($choice);
$config['use_page_numbers'] = $use_page_numbers;
$config['full_tag_open'] = '<ul class="pagination">';
$config['full_tag_close'] = '</ul>';
//First Link
$config['first_link'] = 'First';
$config['first_tag_open'] = '<li>';
$config['first_tag_close'] = '</li>';
//Last Link
$config['last_link'] = 'Last';
$config['last_tag_open'] = '<li>';
$config['last_tag_close'] = '</li>';
//Next Link
$config['next_link'] = 'Next';
$config['next_tag_open'] = '<li>';
$config['next_tag_close'] = '</li>';
//Previous Link
$config['prev_link'] = 'Prev';
$config['prev_tag_open'] = '<li>';
$config['prev_tag_close'] = '</li>';
//Current link
$config['cur_tag_open'] = '<li class="active"><a href="#">';
$config['cur_tag_close'] = '</li></a>';
//Digits Link
$config['num_tag_open'] = '<li>';
$config['num_tag_close'] = '</li>';
return $config;

Note: You can also load pagination library globally. To load pagination globally, we just need to put pagination inside application/config/autoload.php as i am showing below.

$autoload[‘libraries’] = array(‘database’,’form_validation’);

3. Setting up Model

Next, create a model called UserListModel.php inside application/models where we gonna fetch records of table users.Here is code for model.


<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class UserListModel extends CI_Model {
public function __construct() {
// Fetch data according to per_page limit.
public function fetch_data($limit, $page) {
$data = array();
$query = $this->db->get("tbl_user");
if ($query->num_rows() >0) {
foreach ($query->result() as $row) {
$data[] = $row;
return $data;

4. Creating View for Pagination

Next, Create a view called userlistview.php inside application/views directory. And here we will display all data in a tabular format with pagination. Please find below the code for view.

<link rel="stylesheet" href="">
<title>Codelgniter pagination</title>
body {
font-family: 'Raleway', sans-serif;
width: 1015px;
position: absolute;
top: 10%;
left: 20%;
<section class="content">
<div class="row">
<div class="col-xs-12">
<div class="box">
<div class="box-header">
<h3 class="box-title">All Users</h3>
<div class="paginationWrap"><?php echo ($result)?$links:'';?></div>
<div class="box-body table-responsive">
<div class="clearfix text-right"style="padding:10px;">
Total Records: <strong><?php echo $total_rows;?>
<table id="example2"class="table table-bordered table-hover">
foreach($result as $row){
<td valign="middle"><?php echo $row->id; ?></td>
<td valign="middle"><?php echo $row->name;?></td>
<td valign="middle"><?php echo $row->mobile;?></td>
<td valign="middle"><?php echo $row->address;?></td>


<div class="paginationWrap"><?php echo ($result)?$links:'';?></div>

5. Routes Configuration:

Configure routes as showing below inside application/config/routes.php file.

$route['users']     = 'userlistcontroller';
$route['users/(:num)']  = 'userlistcontroller/index/$1';

Codeigniter Pagination

Pagination using Codeigniter

Thank you for reading this post. we hope you like this Post, Please feel free to comment below, your suggestion. if you face any issue with this code let us know. We’d love to help!