Site icon WDB24

Simple Codeigniter Pagination Example

thumbnail codeigniter pagination

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

Create MySQL Database:

Create MySQL Table:

Insert Dummy records in Customer Table:

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:

Exit mobile version