Pagination is a number of rows to be fetched from the database and mostly found at the bottom of the page which allow you to navigate to a different page. Today I will show you to use pagination in CodeIgniter which is easy to use.
To Load the pagination library, following code is useful:
1 2 3 | $this->load->library('pagination'); |
The library comes with around 20 configurable items and you need only the three items to create the links.
To use Pagination in CodeIgniter, you must have the Pagination library must be loaded.
Let’s check it out here
First of all, add following code into the model
file.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | $this->load->library('pagination'); $result = $this->m_demo->getRows($page); $config['base_url'] = 'url_of_page'; //The base_url is the URL of your controller $config['total_rows'] = $this->m_demo->getTotalNumRows(); //total_rows is the number of rows you have in your database $config['per_page'] = 2; //per_page is the number of results that you want to show per page $this->pagination->initialize($config); foreach($result as $item) { echo $item['cat_name']; } echo $this->pagination->create_links(); |
To add above code into the function index and add one parameter $page=0
into the function.see in below:
1 2 3 | function index($page=0) |
Next,Move to the controller
file and add following function into it:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | function getRows($page) { $this->load->database(); $query = $this->db->query("select * from category limit $page,2"); return $query->result_array(); } function getTotalNumRows() { $this->load->database(); $query = $this->db->query("select * from category"); return $query->num_rows(); } |
Further Reading:
Image Upload in CodeIgniter
Get IP address in CodeIgniter
Prepping Functions in CodeIgniter
That’s it.This is all that you need in order to create pagination.I hope this post will help you. Don’t Forget to Follow us on Twitter or Subscribe us to Get the Latest Updates.