You will find pagination in most of the web application. If you search something in Google, you will see paginated results with pages at the bottom of results.
Why pagination?
While we working with thousands of records from a database, we cannot display all the thousands/millions in a single page. That would be a bad effect on the page load.
And most of the times it will fail to load this big chunk of data on the browser. So we are dividing the huge result into several pieces to display, that called pagination.
For example, 10,000 records will divide into 1000 pages, 10 per page. This will load quickly in your browser if the user wants to see the next page they can simply navigate to next page by clicking the numbers listed on pagination.
How to do Pagination in CodeIgniter
Codeigniter has a well-written pagination library with a bunch configuration options. In this tutorial, we are showing how to use pagination library in CodeIgniter.
Before we start deep in to the pagination in Codeigniter we will configure CodeIgniter with necessary parameters like database, URL, session etc. This is important to start with CodeIgniter project.
1) Database configuration
application/config/database.php
We have updated with our test database details.
$db['default'] = array(
'dsn' => '',
'hostname' => 'localhost',
'username' => 'root',
'password' => '',
'database' => 'tutsplanet',
'dbdriver' => 'mysqli',
'dbprefix' => '',
'pconnect' => FALSE,
'db_debug' => (ENVIRONMENT !== 'production'),
'cache_on' => FALSE,
'cachedir' => '',
'char_set' => 'utf8',
'dbcollat' => 'utf8_general_ci',
'swap_pre' => '',
'encrypt' => FALSE,
'compress' => FALSE,
'stricton' => FALSE,
'failover' => array(),
'save_queries' => TRUE
);
2) In order to load the database library, we have to configure the autoload lib. So add the database param in the library array in the autoload.php
application/config/autoload.php
$autoload['libraries'] = array('database');
3) Next autload the helper functions in order to work the URLs properly base url, base dir etc..
$autoload['helper'] = array('url');
4) Finally you may need to update your application base url in application/config/config.php as some of you may keep the project in another folder inside document root.
$config['base_url'] = 'http://localhost/codeigniter_pagination/index.php';
You may be wondering why there are an index.php at the trailing slash. It’s necessary to keep the index.php unless you have a well-written .htacces file to bypass it. You can read this article to remove the index.php from CodeIgniter URL.
Now we are ready to start with CodeIgniter pagination.
First start with model:-
Well create a model called Country_model.php in application/models
Controller:-
Create a file called Country.php in the directory application/controllers as this is your controller file.
View:-
create a file called listing in the directory application/views/
So that’s with our pagination tutorials for codeigniter.
You can download the country database from GitHub
- Just want to thank us? Buy us a Coffee
- May be another day? Shop on Amazon using our links.
Your prices won't change but we get a small commission.
Yash says
When clicked on the pagination link it says 404 page not found how can I solve that ?
rintoug says
Check whether you pointed the proper URL segment in config option