How to Add Codeigniter Pagination

Active Record | Codeigniter | Codeigniter Library | Pagination

Codeigniter CRUD Pagination


This entry is part 6 of 6 in the series CodeIgniter CRUD

Codeigniter Pagination: In Day 5 Delete Operation we finished all CodeIgniter CRUD operations. Which shows all database records for post table, update existing posts, delete post & create new post. If you haven’t followed CodeIgniter CRUD Course & don’t know what it is or how to implement, I highly recommend you do so. Or if you want, you can download source code files of CodeIgniter CRUD: Day 5 Delete Operation & continue with pagination.

We have a Codeigniter CRUD ready. Which shows list of post on post listing page. Now posts table will become too large with lots of posts. So in near future, It will take much time to load a single page, difficult to search. So we will introduce codeigniter pagination.

Codeigniter Pagination Implementation

In this CodeIgniter Pagination Tutorial, we will use codeigniter’s built-in library. Which has functionality for performing pagination in Codeigniter. So we have to load pagination library first. But before that we have to add our routes capability to handle paginated urls. which will be something like  http://localhost:8888/ci/index.php/post/2 . After post we have a number indicating which page to load for post listing.

Step 1: Open application/config/routes.php file and add following code at end of all routes.

Now what this will do? This will handle our every url containing post/<any number> and loads post controller’s index method.

Before working on controller code to implement codeigniter pagination, we have to make some changes to our post model. so that we can use it in controller. Like we need total post count, we only load particular range of posts instead of all posts, etc.

Step 2: Open application/models/postmodel.php file. Add new method countAll to fetch total number of posts in posts table. And also modify getAll method to add limit parameter so that we can fetch limited data from database instead of all data.

countAll method simply calls active record class’s count_all method to retrieve total number of records count and returns it. getAll method we added a optional argument to adopt new functionality while preserving old one unaffected. Argument containing array with offset & limit key. We pass it to active record class’ limit method which sets limit and offset for query.

Now we are ready to work on our controller to load codeigniter pagination library.

Step 2: Open post controller. and change or replace the index method’s code like below or wherever you want to put pagination.

First we load codeigniter pagination library. Then we call post model’s newly created method countAll to get total records count. Next thing we needed is current page. Now remember we set our routes in beginning and we have our page information on second segment which is after post in URL.  We check for second segment and if it’s available then we set $page variable to that segment. Otherwise default value for $page variable which is 1. Based on current page and total records, we have to calculate offset for fetching posts from database. We passed array containing offset and limit keys to getAll method of post model which we modified in previous step.

Next we create a basic configuration array for pagination containing following keys.

  • base_url – setting url without pagination segment.
  • use_page_numbers – set it to TRUE for url to store page number instead of starting index of records.
  • total_rows – total rows in post table.
  • per_page – how many records to display at a time. we set it to 2 so we dont have to enter too many dummy data.
  • uri_segment – segment position for current page. for our case it’s 2 because index is default method.
  • num_links – specifies how many links to display before and after current page.

After setting all configuration. Pass config array to initialise pagination by calling initialise method of codeigniter pagination library. final step is to call create_links method of codeigniter pagination library to generate pagination links HTML code. We store it in $data['pagination'] and pass it to view along with posts data.

In Next step we will display pagination in view.

Step 3: Open view file located in application/views/post/index.php and add single line of code to display pagination wherever you want it to appear. I am displaying it on top of listing table.

Here is what you will get on top of posts listing table.

Codeigniter Pagination Output

This is the default look of Codeigniter Pagination Library with few settings changed. We can customise further by tweaking settings.

Customise Codeigniter Pagination Library Appearance

Add following highlighted code in post controller’s index method.

Open browser and load listing page again and you will get different output similar to below screenshot.

Customise Codeigniter Pagination


Download Source Code Files


Please like our Facebook Fan page for instant updates on Tutorials.

Series Navigation<< Codeigniter CRUD: Day 5 Delete Operation

Bookmark Link using: bookmark at folkd