CodeIgniter Laravel PHP Example Javascript jQuery MORE
Need a Website Or Web Application Or Any Help.Contact Us: +91 9437911966 (Whatsapp) Note: Paid Service

Pagination in Codeigniter Mysqli With Example and Demo


Create a new model Students_model in application/models.

Students_model.php

<?php
class Students_model extends CI_Model {
	protected $table = 'students_data';
	public function __construct() {
        parent::__construct();
    }
	public function get_count() {
        return $this->db->count_all($this->table);
    }
	public function get_students($limit, $start) {
        $this->db->limit($limit, $start);
        $query = $this->db->get($this->table);
		return $query->result();
    }
}

Create a new file Students.php in application/controllers directory

Students.php

<?php 
defined('BASEPATH') OR exit('No direct script access allowed');
class Students extends CI_Controller {
	public function __construct() {
        parent:: __construct();
		$this->load->helper('url');
        $this->load->model('students_model');
        $this->load->library("pagination");
    }
	public function index() {
        $config = array();
        $config["base_url"] = base_url() . "students";
        $config["total_rows"] = $this->students_model->get_count();
        $config["per_page"] = 10;
        $config["uri_segment"] = 2;
		$this->pagination->initialize($config);
		$page = ($this->uri->segment(2)) ? $this->uri->segment(2) : 0;
		$data["links"] = $this->pagination->create_links();
		$data['students'] = $this->students_model->get_authors($config["per_page"], $page);
		$this->load->view('students/index', $data);
    }
}

Create a new file index.php in application/views/students/index.php

index.php

<!DOCTYPE html>
<html>
    <head>
        <title>CodeIgniter Pagination</title>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
     </head>
    <body>
        <div class="container">
            <h3>CodeIgniter Database Pagination</h3>
            <div class="column">
                <table>
                    <thead>
                        <tr>
                            <th>ID</th>
                            <th>First Name</th>
                            <th>Last Name</th>
                            <th>Email</th>
                            <th>Phone</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php foreach ($students as $student): ?>
                            <tr>
                                <td><?= $student->id; ?></td>
                                <td><?= $student->first_name; ?></td>
                                <td><?= $student->last_name; ?></td>
                                <td><?= $student->email; ?></td>
                                <td><?= $student->phone ;?></td>
                            </tr>
                        <?php endforeach; ?>
                    </tbody>
                </table>
                <p><?php echo $links; ?></p>
            </div>
        </div>
    </body>
</html>