Pagination in CodeIgniter

Pagination in CodeIgniter

In this blog, we will learn about pagination in CodeIgniter. Pagination is used when you don’t want to load your all data at once, if we load all data at once it slows down our page so the solution is pagination. You saw on the website in blogs list, product lists there is the number at the bottom like 1,2,3 next, etc.

The purpose of pagination on a website is that to maintain website performance, it loads data in parts like to load only 5 data or 10 data not all at once.

Lets’ see the following steps to use pagination in CodeIgniter:

Download CodeIgniter3 from the following link: 

Create a database named codeigniter_pagination

CREATE DATABASE codeigniter_pagination;

Create table named blogs:

CREATE TABLE `blogs` ( 
   `id` int(11) NOT NULL, 
   `addeddate` date DEFAULT NULL, 
   `title` text COLLATE utf8mb4_unicode_ci DEFAULT NULL, 
   `description` longtext COLLATE utf8mb4_unicode_ci DEFAULT NULL, 
   `updatedat` date DEFAULT NULL 
 ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci; 

Setup your database connection in config>database.php

$db['default'] = array( 
             'dsn'      => '', 
             'hostname' => 'localhost', 
             'username' => 'root', 
             'password' => '', 
             'database' => 'codeigniter_pagination', 
             '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 

Setup Base URL in config>config.php file:

$config['base_url'] = 'http://localhost/codeigniter-pagination/';  

 Setup Routes in config>routes.php file:

$route['blogs'] = 'welcome/index'; 
 $route['blogs/(:num)'] = 'welcome/index/$1';  

 In Default Controller Welcome.php, write the following code:

public function index() 
                         $config = array();  
         $config["base_url"] = base_url().'/blogs'; 
         $config["total_rows"] = count($this->crud->get_data('blogs')); 
                         $config["per_page"] = 3; // per page how many data you want to show 
         $config["uri_segment"] = 2; // your url segment 
         $page = ($this->uri->segment(2)) ? $this->uri->segment(2) : 0; 
         $data["links"] = $this->pagination->create_links();  // this code create links of pagination like 1 2 3 ... last etc. 
                         $data['LISTDATA']= $this->crud->selectdatainlimit($config["per_page"], $page,'blogs'); // this code fetch data in limit like in first time 3 data second time 4 to 6 data 

Create a file in the model's folder named crud.php, write the following code:

 class Crud extends CI_Model{ 
             function get_data($table) 
                $data= $this->db->get($table); 
                return $data->result();                
             function selectdatainlimit($limit, $start,$table) 
                 $this->db->limit($limit, $start); 
                         $data= $this->db->get($table); 
                         return $data->result(); 

Create a listing.php file in the views folder, write the following code:

<!DOCTYPE html> 
 <html lang="en"> 
   <title>Blog Listing</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> 
   <script src=""></script> 
 <div class="container"> 
   <h2>Blogs List</h2> 
   <table class="table table-bordered"> 
 foreach($LISTDATA as $data) 
         <td><?php echo $data->addeddate;?></td> 
         <td><?php echo $data->title;?></td> 
         <td><?php echo $data->description;?></td> 
             <?php  } ?> 
                    <p><?php echo $links; ?></p> 

Download Source Code