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:


Step1: 
Download CodeIgniter3 from the following link:

 https://codeigniter.com/download 


Step2:
Create a database named codeigniter_pagination

CREATE DATABASE codeigniter_pagination;


Step3:
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; 


Step4:
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 
 ); 


Step5:
Setup Base URL in config>config.php file:

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


Step6:
 Setup Routes in config>routes.php file:

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


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

public function index() 
             { 
                         $this->load->library("pagination"); 
                         
                         
                         $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 
                           
                         $this->pagination->initialize($config); 
   
         $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 
                         
                         $this->load->view('listing',$data); 
             } 
              


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

<?php 
 class Crud extends CI_Model{ 
     
     
   
             function get_data($table) 
             {   
                $data= $this->db->get($table); 
                return $data->result();                
             } 
   
             
             
             function selectdatainlimit($limit, $start,$table) 
             { 
                 $this->db->order_by('addeddate',"desc"); 
                 $this->db->limit($limit, $start); 
                         $data= $this->db->get($table); 
                         return $data->result(); 
             } 
   
             
   
             
 }


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

<!DOCTYPE html> 
 <html lang="en"> 
 <head> 
   <title>Blog Listing</title> 
   <meta charset="utf-8"> 
   <meta name="viewport" content="width=device-width, initial-scale=1"> 
   <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css"> 
   <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script> 
   <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.0/umd/popper.min.js"></script> 
   <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js"></script> 
 </head> 
 <body> 
   
 <div class="container"> 
   <h2>Blogs List</h2> 
   
   <table class="table table-bordered"> 
     <thead> 
       <tr> 
         <th>Date</th> 
         <th>Title</th> 
                          <th>Description</th> 
                           
       </tr> 
     </thead> 
     <tbody> 
             
             <?php 
 foreach($LISTDATA as $data) 
             {?> 
       <tr> 
         <td><?php echo $data->addeddate;?></td> 
         <td><?php echo $data->title;?></td> 
         <td><?php echo $data->description;?></td> 
   
       </tr> 
             <?php  } ?> 
             
     </tbody> 
   </table> 
   
                    <p><?php echo $links; ?></p> 
   
 </div> 
   
 </body> 
 </html>

Download Source Code

Categories: web development codeigniter

Trending Courses

CodeIgniter

Regular : 45 Days

Fastrack : 20 Days

Crash : 10 Days

Advance Digital Marketing

Regular : 6 Months

Fastrack : 3 Months

Crash : 2 Months

React JS

Regular : 45 Days

Fastrack : 20 Days

Crash : 7 Days

Laravel

Regular : 45 Days

Fastrack : 20 Days

Crash : 10 Days

Front End Developer

Regular : 7 Month

Fastrack : 4 Month

Crash : 2 Month

Related Blogs

Request For Demo