Simple Codeigniter Pagination Example

Pagination is one of the best way to split and present data into page by page order. Order of pages are either ascending or descending based on specific column (usually id or date). Codeigniter is come with built-in pagination library which allows php developer to create pagination in a very simple manner.

In this tutorial I am going to show you simple codeigniter pagination example. I will describe step by step process so that pagination will easy for you.

Steps: Simple Codeigniter Pagination Example

  • Download codeigniter from its official website https://www.codeigniter.com/download
  • Setup database, libraries and base path.
  • Create model as Page_model.php and create 2 functions. One function will return total no. of records of a table and second function will use to show records according to current page.
  • Use welcome controller as our pagination example. Load page model in constructer and use index function for pagination.
  • Use welcome_message.php (default codeigniter view) as a view of pagination. I will also create style.css to give better style to the page numbers.

demo codeigniter pagination

Create MySQL Database:

Create MySQL Table:

Insert Dummy records in Customer Table:

Download Codeigniter:

download codeigniter

Set Base URL:

Goto foldername\application\config\config.php and set base_url. I have renamed codeigniter folder to ci so in my case it will look like this.


Setup Database:

Goto foldername\application\config\database.php and add database creadentials.

Add autoload libraries and helpers:

Best thing about codeigniter is you can autoload any library, helpers, packages.There is a file autoload.php in foldername\application\config folder.

I will add database library and url helper in this file.

Library:

Helper:


Create Model (Page_model.php):

Frist I have wrote constructor and call parent constructor. Then I wrote getTotalCustomers() that count all result of customers table. Then I have created getCustomersByLimit($limit, $start) that takes 2 parameters and return an array. Parameter one takes limit and parameter two takes initial record.

Controller (welcome.php):

I used default welcome controller for pagination. First I load page_model in constructor then I load pagination library on index function.

You may wonder on this point that why I load pagination library on index function. I should have load pagination library in autoload file. Answer is, we add most common libraries and helper functions in autoload file. Pagination library wouldn’t be use on every page.

$getTotalCustomers is calling getTotalCustomers() function of page_model which returns the total no. of records in customer table. $perPage is the total no. of records that will show on every page. $page check the page number. $data["page_results"] calls getCustomersByLimit() functions which returns records according to page.
$config is an array that holds pagination config settings. $this->pagination->initialize($config) initializes pagination. $this->pagination->create_links() returns pagination based on given config.

View (welcome_message.php):

Style.css:

Also Read:

 

Author: Ahsan Zameer

I am a web developer and love to search new stuff on web. Trying different approaches and ideas to make web developing more interesting and enjoyable.