Pagination With CodeIgniter

The Model

We’ll start by creating a model in the application which needs to do two things: provide a count of all the records in the Country table, and retrieve a list of countries from the table. Save the following as models/countries.php:

CodeIgniter’s database class with Active Record is used to count and retrieve the data from the database.

The record_count() method returns the number of records and is needed because one of the options in the $config array for the pagination library is $config["total_rows"]. The library will use this along with other options we set to work out how to divide the results up into pages.

The fetch_countries() method retrieves a list of all the records from the Country table. There are two arguments for this method: $limit and $start. They will be passed into the query to determine how many records to return, and what record to start from. The arguments will be set in the controller.

The Controller

Next, we need to create a method in the default Welcome controller (controllers/welcome.php) called example1(). But just before we do that, we’ll need to load the model and also the pagination library in the class’ constructor.

The example1() method starts by providing some of the most common configuration options for the pagination library. The options are placed in an array which is then passed as an argument to the library’s initialize method. The library requires the options must pass a base URL, a total row count, how many rows per page we want, and which part of the URL will contain the page section the user is using.

Remember those parameters that were set required by the query for a list of countries ($limit and $start)? Those are set next in the call to the model’s fetch_countries() method. The $page variable uses the ternary operator to either set its value to whatever is in the third segment of the URI string or to zero (meaning the user is on the first page).

Finally, the create_links() method of the pagination library creates the pagination links and then we load the view to display the outcome.

The View

For the view file, we can copy the views/welcome_message.php and called it example1.php and replace most of the content of the body with the following:

So now when you visit http://yourdomain/welcome/example1

More Config Options

Click the pagination links and you should find yourself moving through the 239 records in the Country table. But you may notice that the number of linked digits change. Initially there were there digits, and later there five!

There is a config item that might help to solve this problem. It’s probably useful at this stage to remember that we are retrieving records from a database and CodeIgniter has to re-calculate the digit links as each page is accessed. The first thing we can do is to disable the first/last previous/next links to tidy up the navigation. It doesn’t stop the expanding digits, but it does tidy things up.

The second thing we can do is calculate how many pages there are by dividing the total rows by the number of rows required per page, round the result, and pass it to the $config["num_links"] parameter.

Here’s what the example1() method looks like with those changes: