Chat with us, powered by LiveChat

This website uses cookies

Our website, platform and/or any sub domains use cookies to understand how you use our services, and to improve both your experience and our marketing relevance.

Use Codeigniter Pagination in Your Projects

November 20, 2017

2 Min Read
Reading Time: 2 minutes

In this tutorial, I will demonstrate how to use Codeigniter Pagination in your projects built on any PHP MySQL Hosting. I will start with the Model, then Controller and finally with the View.

pagination in codeigniter

CodeIgniter application usually have at least one database. In an average project, the database produces a lot of queries that need to be presented. The problem is that the display of the results could be messy. This is where pagination in CodeIgniter comes to the rescue.

You might also likeHow To Host CodeIgniter PHP On Cloud Using SSH

The Model

The model for the demo application carries out two important tasks: It provides a count of the records in the Departments table, and retrieve the list of departments from the table.

The path of the model is models/departments.php. Here is the code for the model:


class Departments extends CI_Model


   public function __construct() {



   public function record_count() {

       return $this->db->count_all("Departments");


   public function fetch_departments($limit, $start) {

       $this->db->limit($limit, $start);

       $query = $this->db->get("Departments");

       if ($query->num_rows() > 0) {

           foreach ($query->result() as $row) {

               $data[] = $row;


           return $data;


       return false;



Here’s a short definition of the methods in the model:

record_count() returns the number of records;  `fetch_departments()`  method retrieves a list of all the records from the Departments table.

You Might Also Like: Setting up Easy Laravel Pagination With Vue

The Controller

The controller for the application is located in the default folder and is named welcome.php. This controlled calls a function `departmentdata()` that offers common configuration options for the Codeigniter pagination library. Here is the code for the Controller:


class Welcome extends CI_Controller


   public function __construct() {

       parent:: __construct();





   public function departmentdata() {

       $config = array();

       $config["base_url"] = base_url() . "welcome/departmentdata";

       $config["total_rows"] = $this->Departments->record_count();

       $config["per_page"] = 5;

       $config["uri_segment"] = 3;


       $page = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;

       $data["results"] = $this->Departments->

           fetch_departments($config["per_page"], $page);

       $data["links"] = $this->pagination->create_links();

       $this->load->view("departmentdata", $data);

You might also like: How To Pass Data From Controller To View In CodeIgniter

The View

The View of the application is located in the views/departmentdata.php. Here is the complete code for the View:


<div id="container">

   <div id="body">


       foreach($results as $data) {

           echo $data->Departments . " - " . $data->Labs . "<br>";



       <p><?php echo $links; ?></p>




The Application

The URl of the demo application is http://domainname/welcome/departmentdata

Here is how it would look. You could see that the large dataset is cleanly displayed.


Pagination in CodeIgniter is a great way of displaying a large number of database query results in a clean manner. I hope that this tutorial would help you implement this technique into your projects.

Share your opinion in the comment section. COMMENT NOW

Share This Article

Launch PHP websites without the worry of Server Management.

Pre-Installed Optimized Stack with Git, Composer & SSH

Owais Alam

is the WordPress Community Manager at Cloudways - A Managed WooCommerce Hosting Platform and a seasoned PHP developer. He loves to develop all sorts of websites on WordPress and is in love with WooCommerce in particular. You can email him at

Get Our Newsletter
Be the first to get the latest updates and tutorials.

Do you like what you read?

Get the Latest Updates

Share Your Feedback

Please insert Content

Thank you for your feedback!

BFCM 2019