×

CodeIgniter Pagination Class

Pagination Class

The CodeIgniter framework provides a pagination class that is used in the data list to create pagination links in a web application. Generally, it is used to retrieve data records from a database and display some definite records on each page to load data faster, enhance the performance, and make the web application user-friendly.

Why we use Pagination?

Suppose, if you want to display thousands of records on a single web application, the length and complexity of the records are very high, due to which users may face many issues while browsing the site such as:

  1. It takes too much time to load all records
  2. It requires to scroll the same web page multiple times to see the information.
  3. It is not easy to search for portable devices like mobile, tablet, etc.
  4. It reduces the performance of the web application.

In order to resolve those conditions, the web application introduced the pagination process.

Example: Suppose there are a total 100 of records on a single page, and you want to show only 10 records on each page. As we solve simple math to get the 100 records like 100/10 = 10, it means we have to create 10 links in which there are 10 records available on each page.

Loading the pagination class

Before using the pagination class in the Codeigniter application, you must load this library in the controller file.

Syntax

$this->load->library( ‘pagination);

Class Reference

  1. initialize(): As the function name defines, it is used to initialize the Pagination class variables with the available option.

Syntax

initialize ([ $params = array() ]);

$params (array): In this parameter, you can define the configuration option of the pagination class.

  • create_links(): A create_links() function is used to create the links on the web pages, and if there is no pagination to show, it returns an empty string.

Structure of pagination example:

$this->load->library( ‘pagination’ );
$config[ ‘base_url’ ] = ‘http://example.com/index.php/Page_controller/test’;
$config[ ‘total_rows ] = 100; // it defines the total number of record stored in database
$config[ ‘per_page’ ] = 10;  // it display 10 records on per page
$this->pagination->initialize( $config );
echo $this->pagination->create_links(); // it is used to create the links on the web page. 

Important Notes:

The $config array defines the configuration variables that are helpful to initialize the pagination functionality in the Codeigniter application and passes it to the $this->pagination->initialize() method. Furthermore, the pagination class contains some twenty methods for configuration, but it required three main methods for creating the links and transfer the control to another page of the web application. There are some important methods of configuration variables which are as follows.

  • base_url: It consists of the full URL path to the controller class/ function that contains the pagination class and its function. In the above example, Page_controller is the controller class name, and the test is the function of the controller
  • total_rows: It represents the total number of records that are stored in the database table for using the pagination. For example, 100 is the records of the database table.
  • per_page: It is used to display the total number of records on each page. In the above example, 10 is the record that is available on per page.

Create a simple Example of a Pagination class in the Codeigniter application.

You have to require these three files for creating the pagination functionality:

  1. Pagination_controller.php file
  2. Hello_Model.php file
  3. fetch.php file

Create a controller file Pagination_controller.php and save it in application/controller/Pagination_controller.php. After that, write the following program in the controller file.

Pagination_controller.php

<?php
 defined('BASEPATH') OR exit('No direct script access allowed');
 class Pagination_controller extends CI_controller
 {
 public function page_link($start = 1)
 {   
         $this->load->database(); // load database 
         $this->load->helper('url');
 $this->load->library('pagination'); // load pagination class
 $this->load->model('Hello_Model');
 $config['base_url'] = 'http://localhost/codeIgniter-3.1.11/index.php/Pagination_controller/page_link';
 $config['total_rows'] = $this->Hello_Model->record_count(); /* count total record of DB. */ 
 $config['per_page'] = 6;
 $config['uri_segment'] = 3;
 $page = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;
         $this->pagination->initialize($config);
         $data['info'] = $this->Hello_Model->getUserPagintaion($config['per_page'], $page);
                 $this->load->view('fetch', $data); 
 }
 }
 ?> 

Create a model file Hello_Model.php and save it in application/model/Hello_Model.php. After that, write the following program in the controller file.

Hello_Model.php

<?php
 defined('BASEPATH') OR exit('No direct script access allowed');
 class Hello_Model extends CI_Model 
 {
 public function getUserPagintaion($limit, $start = 1)
  {       
             $this->db->limit($limit, $start);  
             $query = $this->db->get('users'); 
             return $result = $query->result_array();
 }
 public function record_count() 
 {
             return $this->db->count_all("users"); /* it counts all the rows from the database table. */ 
             }
 }
 ?> 

Create a view file fetch.php and save it in application/views/fetch.php. After that, write the following program in the controller file.

fetch.php

<h2> Pagination Example </h2>
 <table border = 1>
     <tr>
         <th>Id</th>
         <th>Name</th>
         <th>Email</th>
                         <th>Course</th> 
         <th>Gender</th>
             </tr>
     <?php foreach ($info as $row): ?>
              <tr>
                         <td> <?= $row['id'] ?></td>
                         <td> <?= $row['name'] ?> </td>
                         <td> <?= $row['email'] ?> </td> 
                         <td> <?= $row['course'] ?> </td>
                         <td> <?= $row['gender'] ?> </td>
         </tr>
     <?php endforeach; ?>
 </table>
 <br />
 <center> <?php echo $this->pagination->create_links(); ?> </center> 

To run the program in the localhost by invoking the URL localhost/CodeIgniter-3.1.11/index.php/Pagination_controller/page_link function; it shows the output, as shown below.

Pagination Class

Hiding the page number:

If you want that your pagination number should not be displayed except the “next” and “Last” links on the web page, you have to use the given below syntax:

$config[ ‘display_pages’ ] = FALSE;

Note: By setting the TRUE value of display_page, it shows the number of pages along with the next and last link.

Example: Create Pagination_controller.php file to the application/controller/ directory and write the following program as shown below: 

Pagination_controller.php

<?php
 defined('BASEPATH') OR exit('No direct script access allowed');
 class Pagination_controller extends CI_controller
 {
 public function page_hide()
 {
 echo “<title> Tutorial and Example </title>”;
 $this->load->library('pagination'); 
 $config['base_url'] = 'http://localhost/codeIgniter-3.1.11/index.php/Pagination_controller/page_link';
 $config['total_rows'] = 50;
 $config['per_page'] = 10;
 $config['display_pages'] = FALSE; 
 $this->pagination->initialize($config);
 echo $this->pagination->create_links(); 
 }
 }
 ?> 

To run the program in the localhost by invoking the URL localhost/CodeIgniter-3.1.11/index.php/Pagination_controller/page_hide function; it shows the output, as shown below.

Pagination Class

Add the additional attributes to the anchor’s tag:

It allows you to add an extra attribute that can be added to any link and called by the pagination class. It uses key-value pairs to set the functionality in the “attributes” config.

$config[ ‘attributes’ ] = array( ‘class’ => ‘classname’);

Customizing the Pagination class

There is a list of all the available preferences in the pagination function to enhance the display or UI of the Codeigniter application.

  1. $config[‘uri_segment’] = ‘’;

The pagination function is used to automatically detect the segment of the URI that contains the records of the table. You can also modify it according to the application needs.

For example:

When uri_segment is 2, It displays the following result

http://localhost/codeigniter-3.1.11/index.php/Pagination_controller/page_link

and then it shows only a single record on each link of the page.

uri_segment = 3; It displays the following result along with the record of each page to the URL as a third parameter.

http://localhost/codeigniter-3.1.11/index.php/Pagination_controller/page_link/6

  • $config[‘num_link’] = 2;

It is used to create the number of “digit” links before and after the selected page number. For example: here the number 2 will create the two numeric digits on either side of the pagination links.

  • $config[‘use_page_numbers’] = TRUE;

It uses a default URI segment to start the index for the paginating items. It also permits you to show the actual page number by setting this to TRUE.  

  • $config[‘page_query_string’] = TRUE;

It also uses a default pagination library to create the links which is similar to URI segments such as:

http://localhost/codeigniter-3.1.11/index.php/Pagination_controller/page_link/6

  • $config[‘full_tag_open’] = ‘<p>’;

It is used to set the opening tag on the left side of the pagination.

$config[‘full_tag_close’] = ‘</p>’;

It is used to close the full_tag on the left side of the pagination result.

  • Uses of First Link

$config[‘first_link’] = ‘First’;

It allows you to set the text as ‘First’ link on the left of the pagination. If you don’t want to use this link, you can set the value FALSE.

$config[‘first_tag_open’] = ‘<span>’;

It is used to show the opening tag for the ‘first’ link.

$config[‘first_tag_close’] = ‘</span>’; 

It is used to show the closing tag for the ‘first’ link.

$config[‘first_url’] = ‘’;

It enables you to set an alternative URL for the ‘first page’ link.

  • Uses of Last Link

$config[‘last_link’] = ‘Last’;

You can also set any text link to the right side of the pagination such as ‘Last’ link. If you don’t want to use the link, you can set the value FALSE.

$config[‘last_tag_open’] = ‘<span>’;

It is used to show the opening tag for the ‘last’ link.

$config[‘last_tag_close’] = ‘</span>’; 

It is used to show the closing tag for the ‘last’ link.

  • Uses of “Next” link

$config[‘next_link’] = ‘&gt;’;

It allows you to set a text to the “next” page link. If you don’t want to use the link, you can set its value to FALSE.

$config[‘next_tag_open’] = ‘<span>’;

It is used to show the opening tag for the ‘next’ link.

$config[‘next_tag_close’] = ‘</span>’; 

It is used to show the closing tag for the ‘next’ link.

  • Uses of “Previous” link

$config[‘prev_link’] = ‘&lt;’;

It allows you to set a text to the “previous” page link. If you don’t want to use the link, you can set its value to FALSE.

$config[‘prev_tag_open’] = ‘<span>’;

It is used to show the starting tag for the ‘previous’ link.

$config[‘prev_tag_close’] = ‘</span>’; 

It is used to show the end tag for the ‘previous’ link.

  1. Uses of “Current Page” link

A Current Page link is used to show the status of the active page in the pagination.

$config[‘curr_tag_open’] = ‘<p>’;

It is used to show the opening tag for the ‘current’ link.

$config[‘curr_tag_close’] = ‘</p>’; 

It is used to show the closing tag for the ‘current’ link.

  1. Uses of “Digit” link

$config[‘num_tag_open’] = ‘<span>’;

It is used to show the opening tag for the ‘digit’ link.

$config[‘num_tag_close’] = ‘</span>’; 

It is used to show the closing tag for the ‘digit’ link.

For customizing the Pagination, you need to make some changes in the Pagination_controller.php only, and the remaining files, such as Hello_Model.php, fetch.php, will be the same as the above example of the pagination.

            Pagination_controller.php

<?php
 defined('BASEPATH') OR exit('No direct script access allowed');
 class Pagination_controller extends CI_controller
 {
 public function page_link($start = 1)  // start = 1; denotes the starting page index
 {   
         echo "<title> Tutorial and Example </title>";
         $this->load->database(); // load the database 
         $this->load->helper('url'); // load the URL helper 
 $this->load->library('pagination'); // load pagination class
 $this->load->model('Hello_Model'); // load model class
 $config['base_url'] = 'http://localhost/codeIgniter-3.1.11/index.php/Pagination_controller/page_link';
 $config['total_rows'] = $this->Hello_Model->record_count(); /* it count the total rows in a database table. */
 $config['per_page'] = 6; // it shows only 6 record per pages.
 $config['uri_segment'] = 3; // it defines the segment of each page
 $config['num_links'] = 4; 
 $config['use_page_numbers'] = TRUE;
 //$config['reuse_query_string'] = TRUE;
 $config['full_tag_open'] = '<p class = "pagination" width = "30">';
 $config['full_tag_close'] = '</p>';
 $config['first_link'] = 'First Page'; 
 $config['first_tag_open'] = '<span class = "first-link">';
 $config['first_tag_close'] = '</span>';
 $config['last_link'] = 'Last Page';
 $config['last_tag_open'] = '<span class = "last-link">';
 $config['last_tag_close'] = '</span>';
 $config['next_link'] = 'Next->';
 $config['next_tag_open'] = '<span class = "nextlink">'; 
 $config['next_tag_close'] = '</span>';
 $config['prev_link'] = '<- Prev';
 $config['prev_tag_open'] = '<span class = "prevlink">';
 $config['prev_tag_close'] = '</span>';
 $config['cur_tag_open'] = '<span class = "curlink">'; 
 $config['cur_tag_close'] = '</span>';
 $config['num_tag_open'] = '<span class = "numlink">';
 $config['num_tag_close'] = '</span>';
 $page = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;
         $this->pagination->initialize($config);
         $data['info'] = $this->Hello_Model->getUserPagintaion($config['per_page'], $page); 
                 $this->load->view('fetch', $data);  /* it load a view file and $data is used to send the data to the view file. */
 }
 }
 ?> 

To run the program in the localhost by invoking the URL localhost/CodeIgniter-3.1.11/index.php/Pagination_controller/page_link function; it shows the output, as shown below.

Pagination Class

Related Topics

CodeIgniter Array Helper

The Array Helper contains some predefined functions that are used to perform the various operation with array. Load Array helper It is used to load the helper class. We can pass the helper function in the...

6 minutes read.

CodeIgniter URI Class

URI Class The CodeIgniter contains a URI class that is used to retrieve information from URI strings. It also provides suitability to get information from the re-routed segments. The system automatically initializes this...

6 minutes read.

CodeIgniter Email Class

CodeIgniter provides an e-mail library that helps to send a message from one application to another. You can create and send text messages easily in CodeIgniter application even you can set email...

9 minutes read.

Structure of Codeigniter

The File structure of CodeIgniter is divided into three parts: ApplicationSystemuser_guide Application -: As the name represents Application, which contains all the main parts of the project like a controller, libraries, view, models, and others....

3 minutes read.

CodeIgniter Libraries

The library is an important part of the CodeIgniter framework. It has a large collection of libraries files that are used to increase the performance of an application. By default, all CodeIgniter libraries...

4 minutes read.

CodeIgniter ZIP Encoding Class

ZIP Encoding Class The CodeIgniter provides a Zip encoding class that are used to compress a large file into a Zip archives. And this archives files can be downloaded either to your desktop or...

5 minutes read.

CodeIgniter File Helper

The file helper function is used to perform various tasks with files such as accessing files, writing data to files, deleting files, and more. Loading the File Helper Before using the file helper, you must...

7 minutes read.

CodeIgniter Helpers

The Helpers (functions) are stored in the Helper file. It helps CodeIgniter to perform different tasks. The helper file is the collection of many functions that is used to perform specific task. It...

3 minutes read.

CodeIgniter Shopping Cart Class

It contains the various function that enables the user to add or update, display, and delete data item from the shopping carts while browsing the site. The items added in the cart...

10 minutes read.

CodeIgniter Date helper

A date helper file contains some predefined functions that work with date functions. Before using the date function in CodeIgniter, you have to load the date helper in the controller. For example: $this->load->helper(‘date’); Functions of...

12 minutes read.

How to remove an index.php file from the Codeigniter URLs?

As we know, a URL in CodeIgniter is designed in such a way that it would be search engine and human friendly, too, rather than using a standard approach to create a...

2 minutes read.

CodeIgniter Benchmarking Class

The CodeIgniter contains the Benchmarking class that is used to calculate the time difference between two points or the memory usage of the passed statements in the application. By default, the Benchmarking...

7 minutes read.

CodeIgniter Output class

Output class The CodeIgniter framework contains a core class that is an Output class. It  includes the various functions that are used to send the final web page to the requesting browser. In...

8 minutes read.

Retrieve data from Database

Retrieve data from the Database Now we will discuss how we can retrieve data from the database that we have inserted to the employees table of the database in our previous topic “inserted...

2 minutes read.

CodeIgniter Models

Models are the classes that deals with backend operations. It is used to fetch the data from the database and send it to the Controller. All the database related information provided inside...

5 minutes read.

CodeIgniter Calendaring Library

The calendar library contains various functions that are used to create a dynamic calendar. It also enables you to pass the data to calendar cells and 100% control over the calendar design. Load...

7 minutes read.

CodeIgniter String Helper

The string helper file contains functions that allows separate operations with strings in CodeIgniter. Loading the String Helper Before using the string helper in the CodeIgniter application, you must load it in the controller...

7 minutes read.

Delete a record from Database CodeIgniter

Delete a record from the Database After inserting, retrieving, and updating table records, we will now learn how we can delete a particular record from a database table. To delete a record, first, we...

6 minutes read.

CodeIgniter Security Class

Security Class The security class provides various function that helps to create a secure application and process input data securely to the application. This class is preloaded in the CodeIgniter application, so you...

4 minutes read.

Installation of CodeIgniter

Before installing CodeIgniter, you need to make sure that the wamp or xampp server in the system is preinstalled. The installation procedure for CodeIgniter is very easy. You can download CodeIgniter by following these...

2 minutes read.