How to Create Pagination in CodeIgniter


In this tutorial, We will learn How to create pagination using CodeIgniter. If you are working on a big database and fetching multiple records from the database then it’s not a good practice to fetch thousands of records on a single page. So the best practice is the split the records into pages.

First, create a database, then create a table tblusers for the user’s record.

Table Schema

CREATE TABLE `tblusers` (
`id` int(6) UNSIGNED NOT NULL,
`fullName` varchar(30) DEFAULT NULL,
`emailId` varchar(50) DEFAULT NULL,
`mobileNumber` bigint(10) DEFAULT NULL,

Now create a database connection. For creating a database connection update the application/config/database.php (ci-pagination/application/config/database.php) file.

defined(‘BASEPATH’) OR exit(‘No direct script access allowed’);
$active_group = ‘default’;
$query_builder = TRUE;

$db[‘default’] = array(
‘dsn’ => ”,
‘hostname’ => ‘localhost’,
‘username’ => ‘root’, // Database User name
‘password’ => ”, // Database User Password
‘database’ => ‘citutsdb’, // Your Database Name
‘dbdriver’ => ‘mysqli’,
‘dbprefix’ => ”,
‘pconnect’ => FALSE,
‘db_debug’ => (ENVIRONMENT !== ‘production’),
‘cache_on’ => FALSE,
‘cachedir’ => ”,
‘char_set’ => ‘utf8’,
‘dbcollat’ => ‘utf8_general_ci’,
‘swap_pre’ => ”,
‘encrypt’ => FALSE,
‘compress’ => FALSE,
‘stricton’ => FALSE,
‘failover’ => array(),
‘save_queries’ => TRUE

After this create a model User_Model ci-pagination/application/models/User_Model.php.

class User_Model extends CI_Model{
private $table = ‘tblusers’;
public function getCount() {
return $this->db->count_all($this->table);
public function getUsers($limit, $start) {
$this->db->limit($limit, $start);
$query = $this->db->get($this->table);
return $query->result();

After creating a model, We will create a controller ci-pagination/application/controllers/pagination.php

defined(‘BASEPATH’) OR exit(‘No direct script access allowed’);
class Pagination extends CI_Controller {
private $per_page;
public function __construct() {

public function index() {
$page = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;
$data[“links”] = $this->pagination->create_links();
$data[‘users’] = $this->User_Model->getUsers($this->per_page, $page);
$this->load->view(‘pagination’, $data);

public function pageConfig(){
$config = array();
$config[“base_url”] = base_url() . “index.php/Pagination/index”;
$config[“total_rows”] = $this->User_Model->getCount();
$config[“per_page”] = 10;
$config[“uri_segment”] = 3;
$config[‘full_tag_open’] = “<ul class=’pagination’>”;
$config[‘full_tag_close’] = ‘</ul>’;
$config[‘num_tag_open’] = ‘<li>’;
$config[‘num_tag_close’] = ‘</li>’;
$config[‘cur_tag_open’] = ‘<li class=”active”><a href=”#”>’;
$config[‘cur_tag_close’] = ‘</a></li>’;
$config[‘prev_tag_open’] = ‘<li>’;
$config[‘prev_tag_close’] = ‘</li>’;
$config[‘first_tag_open’] = ‘<li>’;
$config[‘first_tag_close’] = ‘</li>’;
$config[‘last_tag_open’] = ‘<li>’;
$config[‘last_tag_close’] = ‘</li>’;
$config[‘prev_link’] = ‘<i class=”fa fa-long-arrow-left”></i>Previous Page’;
$config[‘prev_tag_open’] = ‘<li>’;
$config[‘prev_tag_close’] = ‘</li>’;
$config[‘next_link’] = ‘Next Page<i class=”fa fa-long-arrow-right”></i>’;
$config[‘next_tag_open’] = ‘<li>’;
$config[‘next_tag_close’] = ‘</li>’;

Now, In the final step, we will create a view. That is used to fetch the records on multiple pages. ci-pagination/application/views/pagination.php

<!DOCTYPE html>
<html lang=”en”>
<title>Codeigniter Pagination</title>
<meta charset=”utf-8″>
<meta name=”viewport” content=”width=device-width, initial-scale=1″>
<link rel=”stylesheet” href=””>
<script src=””></script>
<script src=””></script>
<div class=”container”>
<center><h3><u>Users Data</u> </h3></center><br>
<table class=”table table-bordered table-striped”>
<tr bgcolor=”silver”>
<th>Email Id</th>
<th>Mobile No</th>
<th>Reg. Date</th>
foreach ($users as $result){ ?>
<td><?php echo $result->id;?></td>
<td><?php echo $result->fullName; ?></td>
<td><?php echo $result->emailId; ?></td>
<td><?php echo $result->mobileNumber; ?></td>
<td><?php echo $result->regDate; ?></td>
<?php } ?>
<p><?php echo $links; ?></p>

View Demo

Download CodeIgniter Pagination Script
Size: 2.62 MB
Version: V 1

The post How to Create Pagination in CodeIgniter appeared first on PHPGurukul.