How to Create Pagination in Codeigniter

Jun 26, 2012, by admin

codeigniter-logoNow a days most of the websites are database driven, we have seen that many websites show data as a long list. Well, there is nothing wrong in that but if you have to find any information in that long list, it will be quite boring and painful activity.

Note: This tutorial helps in answering the following questions

how to use pagination in codeigniter , how to do pagination in codeigniter , how to implement pagination in codeigniter , how to use pagination class in codeigniter

We think that the best way to present data is, by using Page numbers, simple to surf and finding information wont take any time, as the surfer can go directly to the desired page and find the relevant information.

Pagination-in-CodeigniterCreating page numbers can be a hard thing if you are using a technical way of coding. But if you are using CodeIgniter, creating page numbers can be as easy as a child’s play. CodeIgniter is an application Framework that uses MVC architecture, which allows great separation between Logic and Presentation.

Here is all you need to do:

The $config array contains your configuration variables. It is passed to the $this->pagination->initialize function as shown above. Although there are some twenty items you can configure, at minimum you need the three shown. Here is a description of what those items represent:

base_url : This is the full URL to the controller class/function containing your pagination. In the example above, it is pointing to a controller called “Test” and a function called “page”. Keep in mind that you can re-route your URI if you need a different structure.

total_rows : This number represents the total rows in the result set you are creating pagination for. Typically this number will be the total rows that your database query returned.

per_page : The number of items you intend to show per page. In the above example, you would be showing 20 items per page.

The create_links() function returns an empty string when there is no pagination to show.