Pagination Using Php Codeigniter


This tutorial will introduce you to codeigniter pagination concept. What you will find in this tutorial is that codeigniter pagination encompasses many areas , including pagination configuration , pagination controller , pagination model and pagination view page. With the help of this example you can easily learn codeigniter pagination concept.

In the real world scenario, most of time we have to display large number of records from database table and its not good programming practice to display all the records in one fetch. To avoid this you can divide your result set into number pages base on configuration. So that user can easily navigate through result set with the help of pagination links.

Tools and Technologies

I am using below tools and technologies to implement pagination.

  1. Php Codeigniter 1.7.3
  2. XAMPP / Php 5.2.3
  3. Mysql 5.0.4

Database table trn_movies

Create trn_movies table with the help of below mysql table script. Once you are done with the table creation in mysql database, Our next step is to set up php Codeigniter project.

Insert few records in trn_movies table. Use below script to insert some of the movie names

Codeigniter Database Configuration

As you know that Php Codeigniter is popular for minimum configuration (almost Zero configuration) framework. To connect to database you need to provide username, password and database name configuration details in database.php file.

The config file is located at "application/config/database.php". Like in my case my local mysql database setting is as below.

Codeigniter Base Url Configuration

Pagination Configuration and Customization

First you have to load the pagination library before using its any method.

Codeigniter pagination library is 100% customizable, you change the configuration as per your need, In below example I have did some customization, Its hard to me, to go through each and every configuration but I will cover some of them.

$config['per_page'] = 5; – Display number of records per page

$config['uri_segment'] = '2'; – The pagination function automatically determines which segment of your URI contains the page number. As per your logic you specific the segment number , where your page number available

$config['full_tag_open']... – If you would like to add container to the entire pagination with some mark up tags then you use this option. Here I am using bootstrap pagination <div class="pagination"><ul> component. For other customization you can refer the CodeIgniters Pagination official website.

Once you are done with the customization then you need to initialize the pagination library.

Pagination Controller (home.php)

Lets creates the Controller class called Home under folder name application/controllers along with default method name index().

Pagination Model (MovieModel.php)

Lets create the Model class which will query the database table and fetch the records. Navigate to the application/models folder of your codeigniter project and create a new php file called MovieModel.php

The totalMovies() method returns total number of records available in the trn_movies table and which will used to set $config['total_rows'] configuration option.

And another method is used to return the records base on $limit and $offse parameters which is used to determine how many records to fetch , and what record to start from.


Lets create index.php file under folder name application/views. Here we are going to display the movie records. Add below code to creat the pager links in your view page <?php echo $this->pagination->create_links(); ?>


Enter the url http://localhost/codeigniterdemo/index.php/home in your browser, It will display the result in pagination format,

Leave A Reply

Your email address will not be published.