Skip to content
  • Github
  • Facebook
  • twitter
  • About Us
  • Contact Us
  • Privacy Policy
  • Terms & Conditions
  • Site Map

XpertPhp

Expertphp Is The Best Tutorial For Beginners

  • Home
  • Javascript
    • Jquery
    • React JS
    • Angularjs
    • Angular
    • Nodejs
  • Codeigniter
  • Laravel
  • Contact Us
  • About Us
  • Live Demos
Create Pagination Using The Codeigniter Pagination Library

create pagination using the codeigniter pagination library

Posted on September 10, 2019December 17, 2022 By XpertPhp No Comments on create pagination using the codeigniter pagination library

In this tutorial, We will inform you How to create Pagination using the Codeigniter pagination library. so you can see this article that how to use Codeigniter Pagination Library for pagination.

Pagination means document content dividing into multiple pages. whenever we have one long page at that time we dividing into multiple pages.

When we create pagination at that time use limit clause, it takes two arguments first is “offset” and the second is the “end”. the end means a number of records returned from the database.

Overview

Step 1: Create a Database in table
Step 2: Connect to Database
Step 3: Create Controller
Step 4: Create a Model
Step 5: Create View File

Step 1: Create a Database in table
In this step, we have to create a table in the database, so we will create a database using the below code.

1
2
3
4
5
6
7
8
9
CREATE TABLE IF NOT EXISTS `register` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `first_name` varchar(64) NOT NULL,
  `last_name` varchar(64) NOT NULL,
  `address` text NOT NULL,
  `email` varchar(64) NOT NULL,
  `mobile` varchar(12) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=15 ;

Step 2: Connect to Database
Go to the config folder and open database.php file some changes in this file like hostname, database username, database password, and database name.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
$db['default'] = array(
'dsn' => '',
'hostname' => 'localhost',
'username' => 'root',
'password' => '',
'database' => 'enter here database name',
'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
);

Step 3: Create Controller
In this step, we will create a Users.php file in the “application/controller” directory and paste the below code in this controller.
application/controller/Users.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
<?php
class Users extends CI_Controller {
  
    public function __construct()
    {
        parent::__construct();
        $this->load->model('user_model');
        $this->load->helper('url_helper');
        $this->load->library('pagination');
    }
  
    public function index($offset=0)
    {    
         $config['total_rows'] = $this->user_model->totalUsers();
  
          $config['base_url'] = base_url()."users";
          $config['per_page'] = 5;
          $config['uri_segment'] = '2';
          
          $config['full_tag_open'] = '<div class="pagination"><ul>';
          $config['full_tag_close'] = '</ul></div>';
          
          $config['first_link'] = '« First';
          $config['first_tag_open'] = '<li class="prev page">';
          $config['first_tag_close'] = '</li>';
          
          $config['last_link'] = 'Last »';
          $config['last_tag_open'] = '<li class="next page">';
          $config['last_tag_close'] = '</li>';
          
          $config['next_link'] = 'Next →';
          $config['next_tag_open'] = '<li class="next page">';
          $config['next_tag_close'] = '</li>';
          
          $config['prev_link'] = '← Previous';
          $config['prev_tag_open'] = '<li class="prev page">';
          $config['prev_tag_close'] = '</li>';
          
          $config['cur_tag_open'] = '<li class="active"><a href="">';
          $config['cur_tag_close'] = '</a></li>';
          
          $config['num_tag_open'] = '<li class="page">';
          $config['num_tag_close'] = '</li>';
         $this->pagination->initialize($config);
          
         $page = ($this->uri->segment(2)) ? $this->uri->segment(2) : 0;
         $query = $this->user_model->list($config["per_page"], $page);
            
         $data['users'] =  $query;
         $data['title'] = 'User List';
         $this->load->view('list', $data);
    }
}
?>
See also  CodeIgniter 4 Image Upload With Preview Example

Step 4: Create a Model
In this step, we will create a User_model.php file in the “application/models” directory and paste the below code in this model.
application/models/User_model.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<?php
class User_model extends CI_Model {
  
    public function __construct()
    {
        $this->load->database();
    }
    
    public function list($limit, $offset)
    {
      $this->db->select("*");
      $this->db->from('users');
      $this->db->limit($limit, $offset);
      $query = $this->db->get();
      return $query->result();
    }
    function totalUsers(){
      return $this->db->count_all_results('users');
   }
    
}
?>

Step 5: Create View File
So finally, we will create the user_list.php file in the “application/views/” directory.
application/views/user_list.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
<!DOCTYPE html>
<html lang="en">
<head>
  <title>Create Pagination Using The Codeigniter Pagination Library - XpertPhp</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container">
    <div class="row">
    <div class="col-lg-12">
<table class="table table-bordered">
   <thead>
  <tr>
<th>Id</th>
<th>Name</th>
<th>Email</th>
<th>Mobile</th>
  </tr>
   </thead>
   <tbody>
  <?php if($users): ?>
  <?php foreach($users as $user): ?>
  <tr>
<td><?php echo $user->id; ?></td>
<td><?php echo $user->name; ?></td>
<td><?php echo $user->email; ?></td>
<td><?php echo $user->mobile_number; ?></td>
  </tr>
<?php endforeach; ?>
<?php endif; ?>
   </tbody>
</table>
</div>
    </div>
<div class="row">
<div class="col-md-12">
<?php echo $this->pagination->create_links(); ?>
</div>
</div>
</div>
</body>
</html>

Codeigniter, MySql Tags:create pagination in PHP

Post navigation

Previous Post: Codeigniter Google Recaptcha Form Validation Example
Next Post: How To Create Bar Chart In Codeigniter With AmChart

Latest Posts

  • Laravel 12 Ajax CRUD Example
  • Laravel 12 CRUD Example Tutorial
  • How to Create Dummy Data in Laravel 11
  • Laravel 11 Yajra Datatables Example
  • Laravel 11 Ajax CRUD Example
  • Laravel 11 CRUD Example Tutorial
  • Laravel 10 Ajax CRUD Example Tutorial
  • Laravel 10 CRUD Example Tutorial
  • How to disable button in React js
  • JavaScript Interview Questions and Answers

Tools

  • Compound Interest Calculator
  • Hex to RGB Color Converter
  • Pinterest Video Downloader
  • Birthday Calculator
  • Convert JSON to PHP Array Online
  • JavaScript Minifier
  • CSS Beautifier
  • CSS Minifier
  • JSON Beautifier
  • JSON Minifier

Categories

  • Ajax
  • Angular
  • Angularjs
  • Bootstrap
  • Codeigniter
  • Css
  • Htaccess
  • Interview
  • Javascript
  • Jquery
  • Laravel
  • MongoDB
  • MySql
  • Nodejs
  • Php
  • React JS
  • Shopify Api
  • Ubuntu

Tags

angular 10 tutorial angular 11 ci tutorial codeigniter 4 image upload Codeigniter 4 Tutorial codeigniter tutorial CodeIgniter tutorial for beginners codeigniter with mysql crud operation eloquent relationships file upload File Validation form validation Image Upload jQuery Ajax Form Handling jquery tutorial laravel 6 Laravel 6 Eloquent Laravel 6 Model laravel 6 relationship laravel 6 relationship eloquent Laravel 6 Routing laravel 7 Laravel 7 Eloquent laravel 7 routing laravel 7 tutorial Laravel 8 laravel 8 example laravel 8 tutorial laravel 9 example laravel 9 tutorial Laravel Framework laravel from scratch laravel social login learn jquery nodejs pagination payment gateway php with mysql react js example react js tutorial send mail validation wysiwyg editor wysiwyg html editor

Copyright © 2018 - 2025,

All Rights Reserved Powered by XpertPhp.com