Home >>Codeigniter Tutorial >How to use pagination in codeigniter

How to use pagination in codeigniter

Pagination in Codeigniter with Step by Step Example

Pagination basically implies the client can navigate the pages of results, seeing a subset of them each time, as opposed to looking down the screen for a very long time.

Pagination is especially valuable when you are coding an application that interfaces with a database.

In this tutorial, I will use CodeIgniter's pagination library to demonstrate to you how you can make a paginated rundown of results from a MySQL database.

Given below topic we will cover in this tutorial,

  1. Database Configuration
  2. Pagination Controller
  3. Pagination Library
  4. Pagination Model

Database Configuration

First of all we have to create a database and Store some dummy Records inside the table. Inside a Single table we are going to store minumum 50 Records, and will display on View 10 Records on every page.

CREATE TABLE `student` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(50) NOT NULL,
  `email` varchar(50) NOT NULL,
  `course` varchar(100) NOT NULL,
  PRIMARY KEY (`id`)
)

Once created table insert 50 records in student table(Here email id is not unique so we can add multiple  same email id )

INSERT INTO `student`(`name`, `email`, `course`) VALUES ('test','[email protected]','PHP');
INSERT INTO `student`(`name`, `email`, `course`) VALUES ('test','[email protected]','PHP');
INSERT INTO `student`(`name`, `email`, `course`) VALUES ('test','[email protected]','PHP');
INSERT INTO `student`(`name`, `email`, `course`) VALUES ('test','[email protected]','PHP');
INSERT INTO `student`(`name`, `email`, `course`) VALUES ('test','[email protected]','PHP');
INSERT INTO `student`(`name`, `email`, `course`) VALUES ('test','[email protected]','PHP');
INSERT INTO `student`(`name`, `email`, `course`) VALUES ('test','[email protected]','PHP');
INSERT INTO `student`(`name`, `email`, `course`) VALUES ('test','[email protected]','PHP');
INSERT INTO `student`(`name`, `email`, `course`) VALUES ('test','[email protected]','PHP');
INSERT INTO `student`(`name`, `email`, `course`) VALUES ('test','[email protected]','PHP');
INSERT INTO `student`(`name`, `email`, `course`) VALUES ('test','[email protected]','PHP');
INSERT INTO `student`(`name`, `email`, `course`) VALUES ('test','[email protected]','PHP');
INSERT INTO `student`(`name`, `email`, `course`) VALUES ('test','[email protected]','PHP');
INSERT INTO `student`(`name`, `email`, `course`) VALUES ('test','[email protected]','PHP');
INSERT INTO `student`(`name`, `email`, `course`) VALUES ('test','[email protected]','PHP');
INSERT INTO `student`(`name`, `email`, `course`) VALUES ('test','[email protected]','PHP');
INSERT INTO `student`(`name`, `email`, `course`) VALUES ('test','[email protected]','PHP');
INSERT INTO `student`(`name`, `email`, `course`) VALUES ('test','[email protected]','PHP');
INSERT INTO `student`(`name`, `email`, `course`) VALUES ('test','[email protected]','PHP');
INSERT INTO `student`(`name`, `email`, `course`) VALUES ('test','[email protected]','PHP');
INSERT INTO `student`(`name`, `email`, `course`) VALUES ('test','[email protected]','PHP');
INSERT INTO `student`(`name`, `email`, `course`) VALUES ('test','[email protected]','PHP');
INSERT INTO `student`(`name`, `email`, `course`) VALUES ('test','[email protected]','PHP');
INSERT INTO `student`(`name`, `email`, `course`) VALUES ('test','[email protected]','PHP');
INSERT INTO `student`(`name`, `email`, `course`) VALUES ('test','[email protected]','PHP');
INSERT INTO `student`(`name`, `email`, `course`) VALUES ('test','[email protected]','PHP');
INSERT INTO `student`(`name`, `email`, `course`) VALUES ('test','[email protected]','PHP');
INSERT INTO `student`(`name`, `email`, `course`) VALUES ('test','[email protected]','PHP');
INSERT INTO `student`(`name`, `email`, `course`) VALUES ('test','[email protected]','PHP');
INSERT INTO `student`(`name`, `email`, `course`) VALUES ('test','[email protected]','PHP');
INSERT INTO `student`(`name`, `email`, `course`) VALUES ('test','[email protected]','PHP');
INSERT INTO `student`(`name`, `email`, `course`) VALUES ('test','[email protected]','PHP');
INSERT INTO `student`(`name`, `email`, `course`) VALUES ('test','[email protected]','PHP');
INSERT INTO `student`(`name`, `email`, `course`) VALUES ('test','[email protected]','PHP');
INSERT INTO `student`(`name`, `email`, `course`) VALUES ('test','[email protected]','PHP');
INSERT INTO `student`(`name`, `email`, `course`) VALUES ('test','[email protected]','PHP');
INSERT INTO `student`(`name`, `email`, `course`) VALUES ('test','[email protected]','PHP');
INSERT INTO `student`(`name`, `email`, `course`) VALUES ('test','[email protected]','PHP');
INSERT INTO `student`(`name`, `email`, `course`) VALUES ('test','[email protected]','PHP');
INSERT INTO `student`(`name`, `email`, `course`) VALUES ('test','[email protected]','PHP');
INSERT INTO `student`(`name`, `email`, `course`) VALUES ('test','[email protected]','PHP');
INSERT INTO `student`(`name`, `email`, `course`) VALUES ('test','[email protected]','PHP');
INSERT INTO `student`(`name`, `email`, `course`) VALUES ('test','[email protected]','PHP');
INSERT INTO `student`(`name`, `email`, `course`) VALUES ('test','[email protected]','PHP');
INSERT INTO `student`(`name`, `email`, `course`) VALUES ('test','[email protected]','PHP');
INSERT INTO `student`(`name`, `email`, `course`) VALUES ('test','[email protected]','PHP');
INSERT INTO `student`(`name`, `email`, `course`) VALUES ('test','[email protected]','PHP');
INSERT INTO `student`(`name`, `email`, `course`) VALUES ('test','[email protected]','PHP');
INSERT INTO `student`(`name`, `email`, `course`) VALUES ('test','[email protected]','PHP');
INSERT INTO `student`(`name`, `email`, `course`) VALUES ('test','[email protected]','PHP');
INSERT INTO `student`(`name`, `email`, `course`) VALUES ('test','[email protected]','PHP');
INSERT INTO `student`(`name`, `email`, `course`) VALUES ('test','[email protected]','PHP');
INSERT INTO `student`(`name`, `email`, `course`) VALUES ('test','[email protected]','PHP');
INSERT INTO `student`(`name`, `email`, `course`) VALUES ('test','[email protected]','PHP');
INSERT INTO `student`(`name`, `email`, `course`) VALUES ('test','[email protected]','PHP');
INSERT INTO `student`(`name`, `email`, `course`) VALUES ('test','[email protected]','PHP');
INSERT INTO `student`(`name`, `email`, `course`) VALUES ('test','[email protected]','PHP');
INSERT INTO `student`(`name`, `email`, `course`) VALUES ('test','[email protected]','PHP');
INSERT INTO `student`(`name`, `email`, `course`) VALUES ('test','[email protected]','PHP');
INSERT INTO `student`(`name`, `email`, `course`) VALUES ('test','[email protected]','PHP');

CodeIgniter Pagination Database Model

Create a Model "StudentPagination_Model.php" and save in application/model

add the following code in "StudentPagination_Model" class

<?php
class StudentPagination_Model extends CI_Model 
{

    public function get_count() 
	{
        return $this->db->count_all("student");
    }

    public function get_students($limit, $start) 
	{
        $this->db->limit($limit, $start);
        $query = $this->db->get("student");
        return $query->result();
    }
}
?>

CodeIgniter Pagination Controller

Create a Controller "StudentPagination_Controller.php" and save in application/controller

add the following code in "StudentPagination_Controller" class

<?php defined('BASEPATH') OR exit('No direct script access allowed');

class StudentPagination_Controller extends CI_Controller {

    public function __construct() 
	{
        parent:: __construct();

        $this->load->helper('url','form');
        $this->load->library("pagination");
		$this->load->model('StudentPagination_Model');
    }

    public function index() 
	{
        $config = array();
        $config["base_url"] = base_url() . "index.php/StudentPagination_Controller/index";
        $config["total_rows"] = $this->StudentPagination_Model->get_count();
        $config["per_page"] = 10;
        $config["uri_segment"] = 3;

        $this->pagination->initialize($config);

		
		$page = ($this->uri->segment(3))? $this->uri->segment(3) : 0;
		
        $data["links"] = $this->pagination->create_links();

        $data['student'] = $this->StudentPagination_Model->get_students($config["per_page"], $page);

        $this->load->view('pagination', $data);
    }
}
?>

CodeIgniter Pagination View

Create a View "pagination.php" and save in application/views

add the following code in "pagination.php" Page

<!DOCTYPE html>
<html>
    <head>
        <title>CodeIgniter Pagination Examples</title>
    </head>
    <body>
        <div class="container">
            <h2 class="text-primary">CodeIgniter Pagination Example</h2>
               <table class="table table-bordered">
                        <tr>
                            <th>Id</th>
                            <th>Name</th>
                            <th>Email</th>
                            <th>Course</th>
                        </tr>
						<tbody>
                        <?php foreach ($student as $res): ?>
                            <tr>
                                <td><?php echo  $res->id ?></td>
                                <td><?php echo  $res->name ?></td>
                                <td><?php  echo  $res->email ?></td>
                                <td><?php echo  $res->course ?></td>
                               </tr>
                        <?php endforeach; ?>
                    </tbody>
                </table>
                <p><?php echo $links; ?></p>
            </div>
        </div>
    </body>
</html>

Total Downloads : 287

Login / Register To Download

No Sidebar ads