In this codeigniter pagination tutorial, We would love to share with you how to use pagination in listings. We will use pagination library for creating pagination links in codeigniter.
When we have to display large number of records from database table. If when we show the all the records, it’s not good practice. So Codeigniter provide a library of pagination, we can use pagination library for showing list of records easily.
Before you want to use this library ,you have to load. Codeigniter pagination library is 100% customizable. You can customize codeigniter pagination library as per your need.
Here you will learn each things from scratch. We will share each thing step by step and at the last of example, we provide demo of this tutorial.
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 step we will download the latest version of Codeigniter, Go to this link Download Codeigniter download the fresh setup of codeigniter and unzip the setup in your local system xampp/htdocs/ . And change the download folder name “demo”
Basic Configurations
Next we will set the some basic configuration on config.php file, so let’s go to application/config/config.php and open this file on text editor.
Set Base URL like this
$config['base_url'] = 'http://localhost/demo/';
Define Route
Next we will define routes in routes.php file, so let’s go to application/config/route.php and open this file on text editor. Put the below line into this file.
$route['users/(:num)'] = 'users';
Create Database With Table
In this step, we need to create database name demo, so let’s open your phpmyadmin and create the database with the name demo . After successfully create a database, you can use the below sql query for creating a table in your 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', '[email protected]', '9000000001', '2019-01-01'), (2, 'Admin', '[email protected]', '9000000002', '2019-01-02'), (3, 'User', '[email protected]', '9000000003', '2019-01-03'), (4, 'Editor', '[email protected]', '9000000004', '2019-01-04'), (5, 'Writer', '[email protected]', '9000000005', '2019-01-05'), (6, 'Contact', '[email protected]', '9000000006', '2019-01-06'), (7, 'Manager', '[email protected]', '9000000007', '2019-01-07'), (8, 'John', '[email protected]', '9000000055', '2019-01-08'), (9, 'Merry', '[email protected]', '9000000088', '2019-01-09'), (10, 'Keliv', '[email protected]', '9000550088', '2019-01-10'), (11, 'Herry', '[email protected]', '9050550088', '2019-01-11'), (12, 'Mark', '[email protected]', '9050550998', '2019-01-12');
Setup Database Credentials
In this step, We need to connect our project to database. we need to go application/config/ and open database.php file in text editor. After open the file in text editor, We need to setup database credential in this file like below.
$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 );
Make Model
Go to application/models/ and create model name User_model.php. In this model we will fetch all users list by using list() function and also we will get all counts of users by using totalUsers() function.
<?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 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); } }
Create Views
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 - Tutsmake.com</title> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous"> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></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> </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.
Rrecommended Posts
Learn Remove index.php in web url using .htaccess file Codeigniter
If you have any questions or thoughts to share, use the comment form below to reach us.