In this tutorial, you are going to learn how to paginate database results in CodeIgniter using the pagination library. This tutorial presumes that you are familiar with the basics of CodeIgniter Active Record. If you don’t, then you can read the previous tutorials. The other assumption made is that you have already downloaded CodeIgniter. In this tutorial, you will learn-

Database configuration
CodeIgniter Pagination Database Model
CodeIgniter Pagination Routes
CodeIgniter Pagination Controller

Database configuration

We will start with creating the database and inserting some dummy records in it. It will be a single table database with 50 records on it. Run the following script against MySQL to create the database authors table. Insert 50 dummy records. Now that we have created our database successfully and inserted dummy records in it, let’s configure our CodeIgniter application to communicate with the database. Open application/config/database.php Set the database connection parameters similar to the following HERE,

‘hostname’ => ‘localhost’, sets the host name of the database ‘username’ => ‘root’, specifies the database name ‘password’ => ‘openseseme’, sets the database password ‘database’ => ‘ci_pagination’, specifies the database name.

We will also be autoloading the database library when our application starts. Let’s do that. Open application/config/autoload.php Load the database library as shown by the code below That’s it for the database configuration. Let’s now work on the database model for pagination

CodeIgniter Pagination Database Model

Create a new model Authors_model in application/models Add the following code HERE,

protected $table = ‘authors’; define a protected variable that defines the database table name for the model public function __construct() {…} calls the parent constructor method public function get_count() {…} returns the total records in the database table. This is a need for pagination purposes public function get_authors($limit, $start) {…} defines a method that will be used to retrieve paginated results from the database. We will be passing in the limit and the start point. The limit defines the total number of records to be returned while start defines the number of records that should be skipped

that’s it for our database model. Let’s now create the routes that will be responding to our paginated results.

CodeIgniter Pagination Routes

Open the routes file in application/config/routes.php Add the following route HERE,

We define a route author that accepts an optional parameter of the number. The optional parameter is defined by using the opening and closing brackets. The defined route calls the index method in the Authors controller.

Let’s now move on to the controller for our paginated results

CodeIgniter Pagination Controller

Create a new file Authors.php in application/controllers directory Add the following code HERE,

class Authors extends CI_Controller {…} we define a class Authors that extends the CI_Controller class public function __construct() {…} this method initializes the parent constructor and loads the url helper, authors model and the pagination library. public function index() {…} defines the method that responds to our route authors

$config[“base_url”] = base_url() . “authors”; sets the pagination URL that will be used to generate pagination links $config[“total_rows”] = $this->authors_model->get_count(); sets the total row that need to be paginated. The value is retrieved from the authors model by calling the get_count method. $config[“per_page”] = 10; defines the rows that need to be displayed per page $config[“uri_segment”] = 2; specifies the URL segment that contains the value that will be used to skip records $this->pagination->initialize($config); initializes the pagination library using the config array value that we defined and assign the above values to. $page = ($this->uri->segment(2)) ? $this->uri->segment(2) : 0; checks if the skip number has been set in the second segment of the URI and if its not set then the value 0 is assigned to the variable $page $data[“links”] = $this->pagination->create_links(); creates the pagination links and assigns them to the links key of the $data array variable. data[‘authors’] = $this->authors_model->get_authors($config[“per_page”], $page); retrieved the paginated authors records and assigns them to the authors key of the $data array variable $this->load->view(‘authors/index’, $data); loads the index view in the authors directory and passes in the array variable $data.

That’s it for our model. Let’s now create the view that will display our database results. Create a new directory authors in application/views Create a new file index.php in application/views/authors/index.php Add the following code HERE,

loops through the results of the $authors variable and prints the results in table

prints the pagination links at the bottom of our table.

Let’s now start our application and see the results In this tutorial, we are using the built-in PHP web server, but you can use any web server that supports PHP. Open the terminal Run the following command HERE,

The above command browses to the application code directory and starts the built-in server on port 3000.

Note: the application path has to match the path where you downloaded CodeIgniter. For that, you can use any port number that is free on your computer. It’s not necessary to use port 3000. Open the web browser and load the following URL http://localhost:3000/authors You should be able to get results similar to the following

Summary

In this tutorial, we have covered the basics of pagination and took advantage of the pagination library in CodeIgniter and used it to create an application that paginates database results.