Codeigniter Pagination Library Example
Codeigniter Pagination – This tutorial will teach you how to create pagination links in CodeIgniter for your listings. We will use a pagination library to do this.
When we have a lot of records in a database table, it’s not a good idea to show all of them at once. To solve this problem, CodeIgniter provides a pagination library which helps us to easily show a list of records one page at a time.
Codeigniter Pagination
Contents
- Download Codeigniter Latest
- Basic Configurations
- Create Database With Table
- Setup Database Credentials
- Create User Model
- Make New Controller
- Create Views
- Start Development server
- Conclusion
Download Codeigniter Project
In this next step, we’re going to download the newest version of Codeigniter. To do this, go to the website Download Codeigniter and get the most up-to-date setup. After you’ve downloaded it, unzip the setup in your local system’s xampp/htdocs/ folder. You can also rename your project folder to whatever you’d like.
Basic Configurations
Now it’s time to do some basic configuration settings on the config.php file. To do this, we’ll navigate to the application/config/config.php file and open it up in a text editor.
set base url:-
$config['base_url'] = 'http://localhost/demo/';
Define Route
After this, we need to create paths for our application in the “routes.php” file. To do this, we’ll go to the “route.php” file located in “application/config” and open it in a text editor. Then, we’ll add the following line of code to the file.
$route['users/(:num)'] = 'users';
Create Database With Table
For this next step, we’ll need to create a database with the name “demo” To do this, we’ll open up PHPMyAdmin and create the database with that name. Once it’s successfully created, we can use the following SQL query to create a table in the database.
CREATE TABLE users (
id int(11) NOT NULL AUTO_INCREMENT COMMENT 'Primary Key',
name varchar(100) NOT NULL COMMENT 'Name',
email varchar(255) NOT NULL COMMENT 'Email Address',
contact_no varchar(50) NOT NULL COMMENT 'Contact No',
created_at varchar(20) NOT NULL COMMENT 'Created date',
PRIMARY KEY (id)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 COMMENT='datatable demo table' AUTO_INCREMENT=1;
INSERT INTO users(id, name, email, mobile_number, created_at) VALUES
(1, 'Team', 'info@test.com', '9000000001', '2023-01-01'),
(2, 'Admin', 'admin@test.com', '9000000002', '2023-01-02'),
(3, 'User', 'user@test.com', '9000000003', '2023-01-03'),
(4, 'Editor', 'editor@test.com', '9000000004', '2023-01-04'),
(5, 'Writer', 'writer@test.com', '9000000005', '2023-01-05'),
(6, 'Contact', 'contact@test.com', '9000000006', '2023-01-06'),
(7, 'Manager', 'manager@test.com', '9000000007', '2023-01-07'),
(8, 'John', 'john@test.com', '9000000055', '2023-01-08'),
(9, 'Merry', 'merry@test.com', '9000000088', '2023-01-09'),
(10, 'Keliv', 'kelvin@test.com', '9000550088', '2023-01-10'),
(11, 'Herry', 'herry@test.com', '9050550088', '2023-01-11'),
(12, 'Mark', 'mark@test.com', '9050550998', '2023-01-12');
Setup Database Credential
In the following step, we’ll connect our CRUD app project to the database. To do this, we’ll go to the
application/database.php
file and set up our database credentials. In this file, we’ll add our database name, username, and password.
$db['default'] = array(
'dsn' => '',
'hostname' => 'localhost',
'username' => 'root',
'password' => '',
'database' => 'demo',
'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
);
Create Controller
Now we need to create a controller name Users.php. In this controller we will create some method/function. We will build some of the methods like :
- Index() – This is used to show the users list.
<?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);
}
}
Make Model
Now, we need to navigate to the “models” folder in our application and create a new file named “User_model.php”. In this file, we’ll create two functions: “list()” function to fetch a list of all users and “totalUsers()” function to get the total count of users.
<?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');
}
}
Create View
Go to application/views/ and create a one view name ajax-form.php.
Now we need to create list.php, go to application/views/ folder and create list.php file. Here put the below code into list.php file.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no" />
<title>Codeigniter pagination Example - torqueprogramming.co.in</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css"/>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container">
<div class="container">
<div class="row mt40">
<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 class="row">
<div class="col-md-12">
<div class="row"><?php echo $this->pagination->create_links(); ?></div>
</div>
</div>
</div>
</div>
</div>
</body>
</html>
Start Development server
For start development server, Go to the browser and hit below the url.
http://localhost/demo/users
Conclusion
In this codeigniter pagination tutorial, We have successfully created users list with pagination.
You may also like
- Codeigniter jQuery Ajax Form Submit with Validation
- Implement Dynamic Google Pie Charts With PHP Codeigniter
- First Codeigniter 3 CRUD (Create,Read,Update,Delete) via Mysql
- Overlay Preloader Image over Div using CSS
- CodeIgniter left join, right, inner, Outer, Cross, full join
- Jquery UI Autocomplete Search In Codeigniter With Live Demo
- Interactive Product Card Using HTML, CSS And JavaScript – Torque Programming
- Codeigniter 4 Jquery image upload with preview example
- Integrate Razorpay with PHP Codeigniter
- Implement Google Column Chart With PHP Codeigniter
- Laravel 10 Form Validation Tutorial Example
- Toggle Profile Card Info Using JavaScript – Torque Programming
- Codeigniter PHP Google Recaptcha Form Validation Example
- Prismatic Forms Using HTML, CSS And JavaScript
- Implement Dynamic Google Pie Charts With PHP Codeigniter
- Ubuntu PHP bz2 Extension Install Commands Example

