Codeigniter MySQL Pagination Example

Codeigniter MySQL Pagination Example

The benefit of using any web application framework is that you don’t have to worry about the common tasks like input handling, form validation and the like, as the framework already provides wrappers for those features. Thus, it allows you to concentrate on the business logic of the application rather than reinventing the wheel over and over again.

Today, we’re going to explore an important library in the CodeIgniter framework—the pagination library.

CodeIgniter provides a very simple, but flexible pagination library that is simple to theme, works with the model, and capable of supporting multiple paginators on a single page.

Let me highlight the topics that we’re going to cover in the course of this article:

Steps to implement pagination

Below are the simple steps to create pagination in Codeigniter and MySQL.

Step 1 : Create Table In MySQL.

CREATE TABLE test.user ( id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY, name VARCHAR(30) NOT NULL, email VARCHAR(50), mobno bigint(10) ).

Step 2 : Insert Some Data Into Table.

INSERT INTO `user` (`id`, `name`, `email`, `mobno`) VALUES (NULL, 'gfgfd', 'test@gmail.com', '65464877');INSERT INTO `user` (`id`, `name`, `email`, `mobno`) VALUES (NULL, 'gfgfd', 'test@gmail.com', '65464877');INSERT INTO `user` (`id`, `name`, `email`, `mobno`) VALUES (NULL, 'gfgfd', 'test@gmail.com', '65464877');INSERT INTO `user` (`id`, `name`, `email`, `mobno`) VALUES (NULL, 'gfgfd', 'test@gmail.com', '65464877');INSERT INTO `user` (`id`, `name`, `email`, `mobno`) VALUES (NULL, 'gfgfd', 'test@gmail.com', '65464877');INSERT INTO `user` (`id`, `name`, `email`, `mobno`) VALUES (NULL, 'gfgfd', 'test@gmail.com', '65464877');INSERT INTO `user` (`id`, `name`, `email`, `mobno`) VALUES (NULL, 'gfgfd', 'test@gmail.com', '65464877');INSERT INTO `user` (`id`, `name`, `email`, `mobno`) VALUES (NULL, 'gfgfd', 'test@gmail.com', '65464877');INSERT INTO `user` (`id`, `name`, `email`, `mobno`) VALUES (NULL, 'gfgfd', 'test@gmail.com', '65464877');INSERT INTO `user` (`id`, `name`, `email`, `mobno`) VALUES (NULL, 'gfgfd', 'test@gmail.com', '65464877');INSERT INTO `user` (`id`, `name`, `email`, `mobno`) VALUES (NULL, 'gfgfd', 'test@gmail.com', '65464877');INSERT INTO `user` (`id`, `name`, `email`, `mobno`) VALUES (NULL, 'gfgfd', 'test@gmail.com', '65464877');INSERT INTO `user` (`id`, `name`, `email`, `mobno`) VALUES (NULL, 'gfgfd', 'test@gmail.com', '65464877');INSERT INTO `user` (`id`, `name`, `email`, `mobno`) VALUES (NULL, 'gfgfd', 'test@gmail.com', '65464877');INSERT INTO `user` (`id`, `name`, `email`, `mobno`) VALUES (NULL, 'gfgfd', 'test@gmail.com', '65464877');INSERT INTO `user` (`id`, `name`, `email`, `mobno`) VALUES (NULL, 'gfgfd', 'test@gmail.com', '65464877');INSERT INTO `user` (`id`, `name`, `email`, `mobno`) VALUES (NULL, 'gfgfd', 'test@gmail.com', '65464877');

Step 3 : Update application/config/database.php.

$db['default'] = array(
	'dsn'	=> '',
	'hostname' => 'localhost',
	'username' => 'root',
	'password' => '',
	'database' => 'dbname',
	'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
);

Step 3 : Create a new model User_Model in application/models.

<?php 
class User_Model extends CI_Model{    
    private $table = 'user';    
    public function getCount() {
        return $this->db->count_all($this->table);
    }
    public function getUsers($limit, $start) {
        $this->db->limit($limit, $start);
        $query = $this->db->get($this->table);
        return $query->result();
    }
}
?>

Step 4 :Create a new file pagination.php in application/controllers directory.

<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Pagination extends CI_Controller {

    private $per_page;

    public function __construct() {
        parent::__construct();
        $this->load->database();
        $this->load->helper('url');
        $this->load->model('User_Model');
        $this->load->library("pagination");
    }
    
	public function index() {	    
	   $this->pageConfig();
          $page = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;
          $data["links"] = $this->pagination->create_links();
          $data['users'] = $this->User_Model->getUsers($this->per_page, $page);
          $this->load->view('pagination', $data);
    }
	
	public function pageConfig(){     
	   $config = array();
          $config["base_url"] = base_url() . "index.php/Pagination/index";
          $config["total_rows"] = $this->User_Model->getCount();
          $config["per_page"] = 5;
         $config["uri_segment"] = 3;
         $config['full_tag_open'] = "<ul class='pagination'>";
         $config['full_tag_close'] = '</ul>';
         $config['num_tag_open'] = '<li>';
         $config['num_tag_close'] = '</li>';
         $config['cur_tag_open'] = '<li class="active"><a href="#">';
         $config['cur_tag_close'] = '</a></li>';
         $config['prev_tag_open'] = '<li>';
         $config['prev_tag_close'] = '</li>';
         $config['first_tag_open'] = '<li>';
         $config['first_tag_close'] = '</li>';
         $config['last_tag_open'] = '<li>';
         $config['last_tag_close'] = '</li>';
         $config['prev_link'] = '<i class="fa fa-long-arrow-left"></i>Previous Page';
         $config['prev_tag_open'] = '<li>';
         $config['prev_tag_close'] = '</li>';
         $config['next_link'] = 'Next Page<i class="fa fa-long-arrow-right"></i>';
         $config['next_tag_open'] = '<li>';
         $config['next_tag_close'] = '</li>';
         $this->per_page=$config["per_page"]; 
         $this->pagination->initialize($config);	    
	}
}

Step 5 :HOw To Create a new file pagination.php in application/views

<!DOCTYPE html>
<html lang="en">
<head>
  <title>Codeigniter MySQL Pagination Example</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container">
  <center><h3><u>Codeigniter MySQL Pagination Example</h3></u></center><br>  
 <table class="table table-bordered table-striped">  
<thead>  
<tr bgcolor="silver">  
<th>Id</th>
<th>Name</th>
<th>Email</th>
 <th>Mob No</th>
</tr>  
<thead>  
<tbody>  
<?php foreach ($users as $user): ?> 
             <tr>
                     <td><?= $user->id ?></td>
                    <td><?= $user->name ?></td>
                    <td><?= $user->email ?></td>
                    <td><?= $user->mobileno ?></td>
            </tr>
<?php endforeach; ?>
</tbody>  
</table>  
 <p><?php echo $links; ?></p>
</div>
</body>
</html>

Vue Js Insert Data

Codeigniter MySQL Pagination Example Pagination

Exit mobile version