Use Pagination when you have to display many records in a web page. Instead of displaying all the records in one page, it is better to spread the data in multiple pages. CodeIgniter 3 provides a Pagination Library using which you can easily implement pagination in a web aplication using CodeIgniter 3.
In this topic, we will develop simple pagination in CodeIgniter 3 and MySQL. The Application will select student data from the database and the data fetched from the database will be displayed on multiple pages. CodeIgniter Pagination Library gives a very easy way by creating pagination links.
Step 1 - Create a MySQL table for student details
Let us create the table named 'students' in MySQL database. I have a database named 'demo'. So, this table will be created in demo database. If you have an existing database other than demo you can also use it. Just make sure the same table does not exist already. Table structures is given below:
This table stores details of all students.
Below are the columns in this table.
- student_id - It is the primary key and auto incremented id of the student
- first_name - First name of the student
- last_name - Last name of the student
- school - School name of the student
- marks - Marks obtained by the student
Create table script for this table are given below; you can use this code to create the table.
After you create the table, verify that the table structure is same as above and the rows are inserted in the table. There should be 23 rows in the table as below:
Before I go to next step, let me show the folder structure I am using. I have created a folder named 'pagination' under xampp/htdocs and kept all CodeIgniter folder/files in this folder along with our custom files. So, the root directory of the application is xampp/htdocs/pagination.
You can see, we have Controller -
StudentController.php, Model -
ModelStudent.php and the view
css/style.css for our application.
Step 2 - Write Controller code to create pagination (StudentController.php)
Let's see the code:
Controller gets the data from database and creates pagination links.
index() function defines few configuration parameters required for pagination and using pagination library, links are created using
create_links() method. Also, using a model method, student rows are populated. Both these links and student data are sent to the view and view displays the data with pagination. Let's see the controller code.
First few lines of
index() method are for setting few parameters required for pagination library. Pagination library is added in
autoload.php. It basically gets the total number of rows from database and then depending on the value of number of rows per page to be displayed, it generates few pagination links.
get_student_count() of the model gives total number of rows. It calls the model method
get_student($limit, $offset) to get the student rows per page.
Step 3 - Write Model code to get data from database (ModelStudent.php)
Our model will have two methods which are used in the controller. Method
get_student_count() returns all the rows from 'students' table. Method
get_student() returns a set of students as defined by LIMIT and OFFSET values. Let's see below code of the model:
Our controller and model are developed. Next, let us see the view.
Step 4 - Create the view to display list of students with pagination
In the view, we will define a html table and display the student rows along with the pagination links.
While loading the view in the controller, dataset
$pagination were sent to the view. So in the view we will just display them. You can see the rows are displayed in the html table using a loop. For paginatiopn, all we have to do is just display
$pagination and all the links will be displayed.
There is a custom stylesheet used in the views. It is given below:
Few simple styles are added here, you can always add better styles.
Our views, controller and models are developed. Now we need to do some config changes before we run the application. Let's do that in next step.
Step 5 - Update Configuration files and Test the Application
Let us now update few setup files before we run the application.
We will update below files in config folder:
- Update autoload.php (application/config/autoload.php)
I have added 'database' and 'pagination' in autoload libraries.
Also, 'url' are added in helper array.
- Update config file (application/config/config.php)
Set 'base_url' and 'index_page' as below in this file:
- Update database setup file (application/config/database.php)
As I mentioned before, I am using a database named 'demo' in localhost with user 'root' and blank password, so I updated database.php as below:
- Update routes file (application/config/routes.php)
- Update .htaccess (hypertext access) file to remove index.php from the URL
.htaccess file is used to do configuration changes for a directory. I have kept
.htaccess file in my root folder which is xampp/htdocs/pagination.
.htaccess file is given below:
Using URL rewriting rule, it redirects all request except those which are not existing files or directories.
Test The Application
Make sure in your XAMPP control panel Apache server and MySQL services are running. Open the browser and run localhost/pagination. You will see the list of students page displayed as below:
You will see first five students (in the order of student_id) are displayed and all page numbers are displayed below the table. Click on any page number and see the rows are diaplayed for that page. When you click on a page number link, it calls the index method of the controller with the required offset.
Download Source Code
I have put codes for controller, model and view in a zip file. You can download it by clicking on the Download button below. You do not need to register yourself to download it. You can use the code as is or you can modify them as per your requirements.
This was an example of using CodeIgniter pagination using pagination library. You can create your own pagination as well. In that case you have to get the total number of rows and then divide it by the number of rows you want to display per page. Then using a loop create a series of links, for each link use a parameter for offset and fetch data from database.