Use Codeigniter Pagination in Your Projects

by Owais Alam  November 20, 2017

CodeIgniter application usually have at least one database. In an average project, the database produces a lot of queries that need to be presented. The problem is that the display of the results could be messy. This is where pagination in CodeIgniter comes to the rescue.

pagination in codeigniter

In this tutorial, I will demonstrate how to use this technique in your CodeIgniter projects. I will start with the Model, then Controller and finally with the View.

You might also likeHow To Host CodeIgniter PHP On Cloud Using SSH

The Model

The model for the demo application carries out two important tasks: It provides a count of the records in the Departments table, and retrieve the list of departments from the table.

The path of the model is models/departments.php. Here is the code for the model:

Here’s a short definition of the methods in the model:

record_count() returns the number of records;  fetch_departments()  method retrieves a list of all the records from the Departments table.

The Controller

The controller for the application is located in the default folder and is named welcome.php. This controlled calls a function departmentdata() that offers common configuration options for the pagination library. Here is the code for the Controller:

You might also like: How To Pass Data From Controller To View In CodeIgniter

The View

The View of the application is located in the views/departmentdata.php. Here is the complete code for the View:

The Application

The URl of the demo application is http://domainname/welcome/departmentdata

Here is how it would look. You could see that the large dataset is cleanly displayed.

Conclusion

Pagination in CodeIgniter is a great way of displaying a large number of database query results in a clean manner. I hope that this tutorial would help you implement this technique into your projects.

Start Creating Web Apps on Managed Cloud Servers Now!

Easy Web App Deployment for Agencies, Developers and E-Commerce Industry

About Owais Alam

is the WordPress Community Manager at Cloudways – A Managed WooCommerce Hosting Platform and a seasoned PHP developer. He loves to develop all sorts of websites on WordPress and is in love with WooCommerce in particular. You can email him at owais.alam@cloudways.com

Stay Connected:

You Might Also Like...