How to create pagination using CodeIgniter
Now 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 tedious and painful activity.
We think that the best way to present data is, by using Page numbers, easy to surf and finding information wont take any time, as the surfer can go directly to the desired page and find the relevant information.
Creating page numbers can be a difficult thing if you are using a procedural 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:
$this->load->library(‘pagination’);
$config['base_url'] = ‘http://www.gwtindia.com/index.php/test/page/’;
$config['total_rows'] = ’200′;
$config['per_page'] = ’20′;
$this->pagination->initialize($config);
echo $this->pagination->create_links();
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.
**Note: Please change the base_url to your website / application url **
Reads:1651
Comments (2)
Aug 30 2010
Posted: under CodeIgniter.
Tags: CodeIgniter, How to create page numbers using CodeIgniter

