May 11, 2012 CodeIgniter Image

Adding pagination within Codeigniter

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

View: Books_view

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:

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:

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 or (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:

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:

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.

4 thoughts on “Adding pagination within Codeigniter

  1. Personally I like to set ‘page_numbers’ = TRUE, instead of using the offset in the url to get a cleaner url. As when you are on page 2 in your example the link would be books/20 instead of books/2.

    Easily accomplished in the model by:
    $offset = ($page_num – 1) * $max_rows;

    Just my $.02. Nice examples though, keep them coming.

  2. Thanks for introducing pagination in Codeigniter. Is there a way to do pagination with a large array? All examples I am seeing online are about getting records from database for each page in piecemeal and displaying them.

    I already built an array of all the results from database. I would like to display only portion of array on each page.


    1. There is a way actually. You would need to use the array function: array_slice(). This function is somewhat the same as when querying with an offset.

      An example usage would be:

      Hope that helps.

      1. Cool!. I will try out this solution. Thanks.

        This way, with result array, I can avoid having to query multiple database tables multiple times as I am building query based on form submission.

Leave a Reply

Your email address will not be published. Required fields are marked *

The qTranslate Editor has disabled itself because it hasn't been tested with your Wordpress version yet. This is done to prevent Wordpress from malfunctioning. You can reenable it by clicking here (may cause data loss! Use at own risk!). To remove this message permanently, please update qTranslate to the corresponding version.