Adding Pagination within Codeigniter
I have had this post for quite some time now, but I have been busy lately with a lot of work, so I haven’t been able to dedicate as much time as I needed to. Finally here is the post. Today we are going to add a small feature to one of the posts I have written concerning codeigniter and the table class. I recommend reading about it, since I will not explain anything of it on this post. You can read it here: Codeigniter HTML Table Class?.
At some point, we will have the need to show the users information from the database, and in many cases, there will be hundreds of records, if not thousands. Showing all those records is not recommended as it may cause your site to run a bit slower. In order to divide the results into equal results per page, we would need what is called, pagination. In codeigniter, there is already a class that will help you with this and I am going to show you how.
Let’s take a look at our resulting code from the tables class post:
Controller: Books
PHP
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Books extends CI_Controller{
function __construct(){
parent::__construct();
}
public function index(){
// Load the tables library
$this->load->library('table');
// Query the database and get results
$data['books'] = $this->db->get('books');
// Create custom headers
$header = array('Book ID', 'Book Name', 'Book Description', 'Book Author');
// Set the headings
$this->table->set_heading($header);
// Load the view and send the results
$this->load->view('books_view', $data);
}
}
?>
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Books extends CI_Controller{
function __construct(){
parent::__construct();
}
public function index(){
// Load the tables library
$this->load->library('table');
// Query the database and get results
$data['books'] = $this->db->get('books');
// Create custom headers
$header = array('Book ID', 'Book Name', 'Book Description', 'Book Author');
// Set the headings
$this->table->set_heading($header);
// Load the view and send the results
$this->load->view('books_view', $data);
}
}
?>
View: Books_view
PHP
<html>
<head>
<title>Jotorres Table class example</title>
</head>
<body>
<div id='results'>
<?php echo $this->table->generate($books); ?>
</div>
</body>
</html>
<html>
<head>
<title>Jotorres Table class example</title>
</head>
<body>
<div id='results'>
<?php echo $this->table->generate($books); ?>
</div>
</body>
</html>
Load pagination and initialize
First thing we have to do is load the pagination library. We all know how to load libraries in codeigniter, so this one is loaded like this:
PHP
<?php
// Load Pagination
$this->load->library('pagination');
?>
<?php
// Load Pagination
$this->load->library('pagination');
?>
After loading, we need to send a config array with atleast 3 specific configuration variables(I use 4 of those). Let’s go ahead and add that to the controller and initialize:
PHP
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Books extends CI_Controller{
function __construct(){
parent::__construct();
}
public function index(){
// Load the tables library
$this->load->library('table');
// Load Pagination
$this->load->library('pagination');
// Config setup
$config['base_url'] = base_url().'/books/';
$config['total_rows = 20;
$config['per_page'] = 10;
// I added this extra one to control the number of links to show up at each page.
$config['num_links'] = 5;
// Initialize
$this->pagination->initialize($config);
// Query the database and get results
$data['books'] = $this->db->get('books');
// Create custom headers
$header = array('Book ID', 'Book Name', 'Book Description', 'Book Author');
// Set the headings
$this->table->set_heading($header);
// Load the view and send the results
$this->load->view('books_view', $data);
}
}
?>
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Books extends CI_Controller{
function __construct(){
parent::__construct();
}
public function index(){
// Load the tables library
$this->load->library('table');
// Load Pagination
$this->load->library('pagination');
// Config setup
$config['base_url'] = base_url().'/books/';
$config['total_rows = 20;
$config['per_page'] = 10;
// I added this extra one to control the number of links to show up at each page.
$config['num_links'] = 5;
// Initialize
$this->pagination->initialize($config);
// Query the database and get results
$data['books'] = $this->db->get('books');
// Create custom headers
$header = array('Book ID', 'Book Name', 'Book Description', 'Book Author');
// Set the headings
$this->table->set_heading($header);
// Load the view and send the results
$this->load->view('books_view', $data);
}
}
?>
Note that one of the configuration variables is the ‘base_url’. This is misleading, as people tend to confuse it with the website base_url. This base_url refers to the page that you are currently on. For example, in our case, we would be on www.yourdomain.com/books or www.yourdomain.com/index.php/books (if you haven’t modified your htaccess). This is, so that when the user clicks on any page link, they will be directed to that url, with the ‘offset’ number. Since the link will have an offset, then we need to send in that offset to the query, so that way, your result will be accordingly to the page. How to do that, just send in the offset as a parameter in the function. Also, we need to modify a bit the query, with the limit, so let’s do those changes now:
PHP
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Books extends CI_Controller{
function __construct(){
parent::__construct();
}
public function index( $offset = 0 ){
// Load the tables library
$this->load->library('table');
// Load Pagination
$this->load->library('pagination');
// Config setup
$config['base_url'] = base_url().'/books/';
$config['total_rows = 20;
$config['per_page'] = 10;
// I added this extra one to control the number of links to show up at each page.
$config['num_links'] = 5;
// Initialize
$this->pagination->initialize($config);
// Query the database and get results
// Here we add the limit and the offset
// The second parameter is the limit, since we are showing
// 10 per page, the limit should be 10
$data['books'] = $this->db->get('books', 10, $offset);
// Create custom headers
$header = array('Book ID', 'Book Name', 'Book Description', 'Book Author');
// Set the headings
$this->table->set_heading($header);
// Load the view and send the results
$this->load->view('books_view', $data);
}
}
?>
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Books extends CI_Controller{
function __construct(){
parent::__construct();
}
public function index( $offset = 0 ){
// Load the tables library
$this->load->library('table');
// Load Pagination
$this->load->library('pagination');
// Config setup
$config['base_url'] = base_url().'/books/';
$config['total_rows = 20;
$config['per_page'] = 10;
// I added this extra one to control the number of links to show up at each page.
$config['num_links'] = 5;
// Initialize
$this->pagination->initialize($config);
// Query the database and get results
// Here we add the limit and the offset
// The second parameter is the limit, since we are showing
// 10 per page, the limit should be 10
$data['books'] = $this->db->get('books', 10, $offset);
// Create custom headers
$header = array('Book ID', 'Book Name', 'Book Description', 'Book Author');
// Set the headings
$this->table->set_heading($header);
// Load the view and send the results
$this->load->view('books_view', $data);
}
}
?>
Everything is set within the controller. All we have to do now is add one line of code within the view, and should be ready to go. Go to where ever you want to show the page links, and add the following line. Here is where I placed it:
PHP
<html>
<head>
<title>Jotorres Table class example</title>
</head>
<body>
<div id='results'>
<?php echo $this->table->generate($books); ?>
<?php echo $this->pagination->create_links(); ?>
</div>
</body>
</html>
<html>
<head>
<title>Jotorres Table class example</title>
</head>
<body>
<div id='results'>
<?php echo $this->table->generate($books); ?>
<?php echo $this->pagination->create_links(); ?>
</div>
</body>
</html>
With this pagination library, we have saved us some time in the implementation, without stress. This library can be very useful for many of us when it comes to pagination. I hope this small feature will help someone in the future.