In this tutorial, I will demonstrate how to use Codeigniter Pagination in your projects built on any PHP Hosting. I will start with the Model, then Controller and finally with the View.
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.
You might also like:Â How 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:
<?php class Departments extends CI_Model { Â Â Â public function __construct() { Â Â Â Â Â Â Â parent::__construct(); Â Â Â } Â Â Â public function record_count() { Â Â Â Â Â Â Â return $this->db->count_all("Departments"); Â Â Â } Â Â Â public function fetch_departments($limit, $start) { Â Â Â Â Â Â Â $this->db->limit($limit, $start); Â Â Â Â Â Â Â $query = $this->db->get("Departments"); Â Â Â Â Â Â Â if ($query->num_rows() > 0) { Â Â Â Â Â Â Â Â Â Â Â foreach ($query->result() as $row) { Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â $data[] = $row; Â Â Â Â Â Â Â Â Â Â Â } Â Â Â Â Â Â Â Â Â Â Â return $data; Â Â Â Â Â Â Â } Â Â Â Â Â Â Â return false; Â Â Â } }
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.
You Might Also Like: Setting up Easy Laravel Pagination With Vue
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 Codeigniter pagination library. Here is the code for the Controller:
<?php class Welcome extends CI_Controller { Â Â Â public function __construct() { Â Â Â Â Â Â Â parent:: __construct(); Â Â Â Â Â Â Â $this->load->helper("url"); Â Â Â Â Â Â Â $this->load->model("Departments"); Â Â Â Â Â Â Â $this->load->library("pagination"); Â Â Â } Â Â Â public function departmentdata() { Â Â Â Â Â Â Â $config = array(); Â Â Â Â Â Â Â $config["base_url"] = base_url() . "welcome/departmentdata"; Â Â Â Â Â Â Â $config["total_rows"] = $this->Departments->record_count(); Â Â Â Â Â Â Â $config["per_page"] = 5; Â Â Â Â Â Â Â $config["uri_segment"] = 3; Â Â Â Â Â Â Â $this->pagination->initialize($config); Â Â Â Â Â Â Â $page = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0; Â Â Â Â Â Â Â $data["results"] = $this->Departments-> Â Â Â Â Â Â Â Â Â Â Â fetch_departments($config["per_page"], $page); Â Â Â Â Â Â Â $data["links"] = $this->pagination->create_links(); Â Â Â Â Â Â Â $this->load->view("departmentdata", $data); Â Â Â } }
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:
<body> <div id="container">    <div id="body">        <?php        foreach($results as $data) {            echo $data->Departments . " - " . $data->Labs . "<br>";        }        ?>        <p><?php echo $links; ?></p>    </div> </div> </body>
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.
Stop Wasting Time on Servers
Cloudways handle server management for you so you can focus on creating great apps and keeping your clients happy.
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.
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 [email protected]