• 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','test2@gmail.com','PHP');
    INSERT INTO `student`(`name`, `email`, `course`) VALUES ('test','test2@gmail.com','PHP');
    INSERT INTO `student`(`name`, `email`, `course`) VALUES ('test','test2@gmail.com','PHP');
    INSERT INTO `student`(`name`, `email`, `course`) VALUES ('test','test2@gmail.com','PHP');
    INSERT INTO `student`(`name`, `email`, `course`) VALUES ('test','test2@gmail.com','PHP');
    INSERT INTO `student`(`name`, `email`, `course`) VALUES ('test','test2@gmail.com','PHP');
    INSERT INTO `student`(`name`, `email`, `course`) VALUES ('test','test2@gmail.com','PHP');
    INSERT INTO `student`(`name`, `email`, `course`) VALUES ('test','test2@gmail.com','PHP');
    INSERT INTO `student`(`name`, `email`, `course`) VALUES ('test','test2@gmail.com','PHP');
    INSERT INTO `student`(`name`, `email`, `course`) VALUES ('test','test2@gmail.com','PHP');
    INSERT INTO `student`(`name`, `email`, `course`) VALUES ('test','test2@gmail.com','PHP');
    INSERT INTO `student`(`name`, `email`, `course`) VALUES ('test','test2@gmail.com','PHP');
    INSERT INTO `student`(`name`, `email`, `course`) VALUES ('test','test2@gmail.com','PHP');
    INSERT INTO `student`(`name`, `email`, `course`) VALUES ('test','test2@gmail.com','PHP');
    INSERT INTO `student`(`name`, `email`, `course`) VALUES ('test','test2@gmail.com','PHP');
    INSERT INTO `student`(`name`, `email`, `course`) VALUES ('test','test2@gmail.com','PHP');
    INSERT INTO `student`(`name`, `email`, `course`) VALUES ('test','test2@gmail.com','PHP');
    INSERT INTO `student`(`name`, `email`, `course`) VALUES ('test','test2@gmail.com','PHP');
    INSERT INTO `student`(`name`, `email`, `course`) VALUES ('test','test2@gmail.com','PHP');
    INSERT INTO `student`(`name`, `email`, `course`) VALUES ('test','test2@gmail.com','PHP');
    INSERT INTO `student`(`name`, `email`, `course`) VALUES ('test','test2@gmail.com','PHP');
    INSERT INTO `student`(`name`, `email`, `course`) VALUES ('test','test2@gmail.com','PHP');
    INSERT INTO `student`(`name`, `email`, `course`) VALUES ('test','test2@gmail.com','PHP');
    INSERT INTO `student`(`name`, `email`, `course`) VALUES ('test','test2@gmail.com','PHP');
    INSERT INTO `student`(`name`, `email`, `course`) VALUES ('test','test2@gmail.com','PHP');
    INSERT INTO `student`(`name`, `email`, `course`) VALUES ('test','test2@gmail.com','PHP');
    INSERT INTO `student`(`name`, `email`, `course`) VALUES ('test','test2@gmail.com','PHP');
    INSERT INTO `student`(`name`, `email`, `course`) VALUES ('test','test2@gmail.com','PHP');
    INSERT INTO `student`(`name`, `email`, `course`) VALUES ('test','test2@gmail.com','PHP');
    INSERT INTO `student`(`name`, `email`, `course`) VALUES ('test','test2@gmail.com','PHP');
    INSERT INTO `student`(`name`, `email`, `course`) VALUES ('test','test2@gmail.com','PHP');
    INSERT INTO `student`(`name`, `email`, `course`) VALUES ('test','test2@gmail.com','PHP');
    INSERT INTO `student`(`name`, `email`, `course`) VALUES ('test','test2@gmail.com','PHP');
    INSERT INTO `student`(`name`, `email`, `course`) VALUES ('test','test2@gmail.com','PHP');
    INSERT INTO `student`(`name`, `email`, `course`) VALUES ('test','test2@gmail.com','PHP');
    INSERT INTO `student`(`name`, `email`, `course`) VALUES ('test','test2@gmail.com','PHP');
    INSERT INTO `student`(`name`, `email`, `course`) VALUES ('test','test2@gmail.com','PHP');
    INSERT INTO `student`(`name`, `email`, `course`) VALUES ('test','test2@gmail.com','PHP');
    INSERT INTO `student`(`name`, `email`, `course`) VALUES ('test','test2@gmail.com','PHP');
    INSERT INTO `student`(`name`, `email`, `course`) VALUES ('test','test2@gmail.com','PHP');
    INSERT INTO `student`(`name`, `email`, `course`) VALUES ('test','test2@gmail.com','PHP');
    INSERT INTO `student`(`name`, `email`, `course`) VALUES ('test','test2@gmail.com','PHP');
    INSERT INTO `student`(`name`, `email`, `course`) VALUES ('test','test2@gmail.com','PHP');
    INSERT INTO `student`(`name`, `email`, `course`) VALUES ('test','test2@gmail.com','PHP');
    INSERT INTO `student`(`name`, `email`, `course`) VALUES ('test','test2@gmail.com','PHP');
    INSERT INTO `student`(`name`, `email`, `course`) VALUES ('test','test2@gmail.com','PHP');
    INSERT INTO `student`(`name`, `email`, `course`) VALUES ('test','test2@gmail.com','PHP');
    INSERT INTO `student`(`name`, `email`, `course`) VALUES ('test','test2@gmail.com','PHP');
    INSERT INTO `student`(`name`, `email`, `course`) VALUES ('test','test2@gmail.com','PHP');
    INSERT INTO `student`(`name`, `email`, `course`) VALUES ('test','test2@gmail.com','PHP');
    INSERT INTO `student`(`name`, `email`, `course`) VALUES ('test','test2@gmail.com','PHP');
    INSERT INTO `student`(`name`, `email`, `course`) VALUES ('test','test2@gmail.com','PHP');
    INSERT INTO `student`(`name`, `email`, `course`) VALUES ('test','test2@gmail.com','PHP');
    INSERT INTO `student`(`name`, `email`, `course`) VALUES ('test','test2@gmail.com','PHP');
    INSERT INTO `student`(`name`, `email`, `course`) VALUES ('test','test2@gmail.com','PHP');
    INSERT INTO `student`(`name`, `email`, `course`) VALUES ('test','test2@gmail.com','PHP');
    INSERT INTO `student`(`name`, `email`, `course`) VALUES ('test','test2@gmail.com','PHP');
    INSERT INTO `student`(`name`, `email`, `course`) VALUES ('test','test2@gmail.com','PHP');
    INSERT INTO `student`(`name`, `email`, `course`) VALUES ('test','test2@gmail.com','PHP');
    INSERT INTO `student`(`name`, `email`, `course`) VALUES ('test','test2@gmail.com','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;
    	$config['full_tag_open'] = '<ul class="pagination justify-content-center">';
            $config['full_tag_close'] = '</ul>';
            $config['num_tag_open'] = '<li class="page-item">';
            $config['num_tag_close'] = '</li>';
            $config['cur_tag_open'] = '<li class="page-item active"><a class="page-link" href="#">';
            $config['cur_tag_close'] = '</a></li>';
            $config['next_tag_open'] = '<li class="page-item">';
            $config['next_tag_close'] = '</a></li>';
            $config['prev_tag_open'] = '<li class="page-item">';
            $config['prev_tag_close'] = '</li>';
            $config['first_tag_open'] = '<li class="page-item ">';
            $config['first_tag_close'] = '</li>';
            $config['last_tag_open'] = '<li class="page-item">';
            $config['last_tag_close'] = '</a></li>';
            $config['attributes'] = array('class' => 'page-link');
            $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>

  • 0 comments:

    Post a Comment

    FAVOURITE LINE

    To steal ideas from one is plagiarism. To steal from many is Research.

    ADDRESS

    Mumbai , Maharashtra

    EMAIL

    shikha.pathak6@gmail.com
    shikha.the.swt.pari@gmail.com

    Skype

    shikha_pari