Pagination in Codeigniter Made Easy
In this article, we’ll learn about Pagination In Codeigniter.
In the realm of web development, effective data navigation is paramount.
Pagination, a magical concept, allows users to gracefully traverse extensive datasets. CodeIgniter, a PHP framework celebrated for its elegance, equips developers with a plethora of tools to seamlessly implement pagination.
We’ll unveil the hidden gems of pagination in CodeIgniter.
Brace yourself for a unique journey where you’ll master the art of creating dynamic and intuitive paginated interfaces, captivating users and optimizing performance.
Pagination In Codeigniter
Pagination is one of the most frequently used features for web applications. Wherever we have a bunch of data and need to show them as a list, we require pagination to prevent hundreds/thousands of data in a single page. As a robust framework, Codeigniter provides a very efficient way to handle it with its integrated support. In this tutorial, we will see, how we can implement CodeIgniter pagination and enhance it further as per our needs.
This tutorial is based on the following 3 things:
1. Loading of the pagination class.
2. Setting the values of the pagination array.
3. Calling the initialize() function of the pagination class.
For additional reference, check out the official Codeigniter page: https://ellislab.com/codeigniter/user-guide/libraries/pagination.html
The code shown below demonstrates the controller function of the page you want to load:
$config['base_url'] = ‘http://www.example.com/index.php/default_page/’;
$config['no_rows'] = 100;
$config['_page'] = 10;
$this->pagination->initialize($config);
}
?>
To show the results in view, you must set a parameter for the $data array which contains the result returned from the database. We will assume that we have a model with the name “my_model” that consists of a function named “fetch_my_data“. These are the functions that will return the result array of the records that you want to fetch from the database. Also, we will assume that we have a view known as “my_view“, where the results will be displayed.
$data[‘my_result’] = $this->my_model->fetch_my_data($config['per_page'], $variant);
$this->load->view(‘my_view’, $data);
?>
Here, our work with the controller is completely done. Now, let’s move to the view file. Once, retrieving the data that was sent from the controller using HTML in the view file, you should call the below function to show the following:
?>
The work is done here as well. The data that was just loaded from the database will get displayed in the view file & the pagination links will look like shown below:
« First Last »
Notice that the ‘10’ URL segment is the “$variant” variable that will be used in the “fetch_my_data()” model function to load the no of database rows that you need. The function should look like the below code:
return$query->result_array();
}
?>
In this example, links to 10 pages will appear, as we have previously set the “$config[‘no_rows’]” parameter to 100 and the “$config[‘_page’]” parameter to 10.
This code can be used to implement pagination in Codeigniter application.
Congratulations! You have unveiled the secrets of pagination in CodeIgniter.
With the knowledge gained on this extraordinary journey, you possess the power to enchant users with seamless data navigation and captivating interfaces.
Embrace the art of pagination in CodeIgniter, infuse your web applications with elegance, and guide users through the vast realms of data effortlessly. Unleash your magic, and may your paginated interfaces leave a lasting enchantment. Happy coding!
Hope this article helps!