CodeIgniter Pagination With Bootstrap

CodeIgniter Pagination With Bootstrap

Most of the time in the web application development, we have to show a large amount of data to the users. To show a large amount of data at once decrease the performance of our web application and we definitely want to avoid such problems in our web application.So at this time pagination plays a vital role to improve the performance of our web application. In this tutorial, we are going to learn how to use CodeIgniter pagination class in our web application. Here we are going to use PHP CodeIgniter framework for our backend and Twitter Bootstrap framework for our front end, So it is necessary to have the basic idea of these two frameworks.

Get Started

Codeigniter is a very famous PHP framework due to his large and easy to use libraries. Codeigniter pagination is one such library which is very helpful if we have to show a large amount of data to the users. Codeigniter pagination library breaks the data into the set of numbered links which allows us to navigate from one page to another.

Setting Database for CodeIgniter pagination

Here we use a simple database for the example purpose so that we can see how CodeIgniter pagination library works. Copy the below SQL query and run this to make a simple database of users.
  1. CREATE TABLE `users` ( `user_id` int(11) NOT NULL AUTO_INCREMENT, `user_name` varchar(256) NOT NULL, `user_email` varchar(256) NOT NULL, `create_date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,PRIMARY KEY (`user_id`)) ENGINE=InnoDB DEFAULT CHARSET=latin1;
Now We have our table, let’s insert some dummy data into this table. Run the below query to insert data into this table.
  1. INSERT INTO `users` (`user_id`, `user_name`, `user_email`, `create_date`) VALUES(1, 'John', 'john@gmail.com', '2017-07-20 16:22:27'),(2, 'Doe', 'doe@gmail.com', '2017-07-20 16:22:27'),(3, 'Sam', 'sam@gmail.com', '2017-07-20 16:25:35'),(4, 'Sara', 'sara@gmail.com', '2017-07-20 16:25:35'),(5, 'Sam', 'sam@gmail.com', '2017-07-20 16:25:35'),(6, 'Sara', 'sara@gmail.com', '2017-07-20 16:25:35'),(7, 'Adam', 'adam@gmail.com', '2017-07-20 16:25:35'),(8, 'Smith', 'smith@gmail.com', '2017-07-20 16:25:35'),(9, 'Ram', 'ram@gmail.com', '2017-07-20 16:25:35'),(10, 'Mohan', 'mohan@gmail.com', '2017-07-20 16:25:35'),(11, 'John', 'john@gmail.com', '2017-07-20 16:25:35'),(12, 'Ahmad', 'ahmad@gmail.com', '2017-07-20 16:25:35'),(13, 'Jorge', 'jorge@gmail.com', '2017-07-20 16:25:35'),(14, 'Ke', 'Ke@gmail.com', '2017-07-20 16:25:35'),(15, 'Xui', 'xui@gmail.com', '2017-07-20 16:25:35');
Till now we have setup our database, now it’s time to proceed further to understand the logic behind the CodeIgniter pagination library.

Frontend To Integrate Bootstrap with our CodeIgniter Pagination

Create a view file in the view folder of CodeIgniter to integrate bootstrap.Here we use bootstrap to customise the generated links from CodeIgniter pagination library.
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>Pagination Demo | CodesQuery</title>
  5. <meta charset="utf-8">
  6. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  7. <meta name="viewport" content="width=device-width, initial-scale=1">
  8. <!--bootstrap CSS -->
  9. <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-
  10. BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
  11. </head>
  12. <body>
  13. <div class="wrapper">
  14. <div class="container" style="margin: 100px;">
  15. <div class="row">
  16. <div class="pagination_demo text-center">
  17. <h4>Pagination Demo | CodesQuery</h4>
  18. <hr>
  19. <table class="table table-bordered table-hover table-responsive paginated">
  20. <thead>
  21. <tr>
  22. <th class="text-center"> S.No. </th>
  23. <th class="text-center"> Name </th>
  24. <th class="text-center"> Email </th>
  25. </tr>
  26. </thead>
  27. <tbody>
  28. <?php $sn= 1; foreach($users as $value) { ?>
  29. <tr>
  30. <td> <?php echo $sn; ?></td>
  31. <td> <?php echo $value['user_name']; ?></td>
  32. <td> <?php echo $value['user_email']; ?></td>
  33. </tr>
  34. <?php $sn++; } ?>
  35. </tbody>
  36. </table>
  37. <div class="pagination_links">
  38. <?php echo $links; ?> </div>
  39. </div>
  40. </div>
  41. </div>
  42. </div>
  43. </div>
  44. <!-- Latest compiled and minified JavaScript -->
  45. <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-
  46. Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous">
  47. </script>
  48. </body>
  49. </html>
In the above code, we use the CDN provided by the bootstrap to integrate it into our web application.Above code will looks like this at the end of this tutorial.

Backend for CodeIgniter pagination

The controller
Make a controller file and load the pagination library in the constructor and the write an index method in which we will write all our code for the CodeIgniter pagination using the bootstrap framework. I will recommend that do not copy the code and just paste it into the controller. Try to first understand it and then write it by yourself.
  1. <?phpdefined('BASEPATH') OR exit('No direct script access allowed');
  2. class Pagination extends CI_Controller {
  3. function __Construct(){
  4. parent::__Construct();
  5. $this->load->database();
  6. //load helper
  7. $this->load->helper('url');
  8. //load model
  9. $this->load->model('user_model');
  10. //load library
  11. $this->load->library('pagination');
  12. }
  13. public function index(){
  14. $config['base_url'] = base_url().'pagination/index';
  15. $config['total_rows'] = $this->user_model->count_all_users();
  16. $config['per_page'] = 5;
  17. $config['uri_segment'] = 3;
  18. $config['full_tag_open'] = '<ul class="pagination">';
  19. $config['full_tag_close'] = '</ul>';
  20. $config['first_link'] = 'First';
  21. $config['last_link'] = 'Last';
  22. $config['first_tag_open'] = '<li>';
  23. $config['first_tag_close'] = '</li>';
  24. $config['prev_link'] = '&laquo';
  25. $config['prev_tag_open'] = '<li class="prev">';
  26. $config['prev_tag_close'] = '</li>';
  27. $config['next_link'] = '&raquo';
  28. $config['next_tag_open'] = '<li>';
  29. $config['next_tag_close'] = '</li>';
  30. $config['last_tag_open'] = '<li>';
  31. $config['last_tag_close'] = '</li>';
  32. $config['cur_tag_open'] = '<li class="active"><a href="#">';
  33. $config['cur_tag_close'] = '</a></li>';
  34. $config['num_tag_open'] = '<li>';
  35. $config['num_tag_close'] = '</li>';
  36. $page = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;
  37. $this->pagination->initialize($config);
  38. $data['links'] = $this->pagination->create_links();
  39. $data['users'] = $this->user_model->get_users($config["per_page"], $page);
  40. $this->load->view('pagination_demo',$data);
  41. }
  42. }
Here we use the $config array to tell the CodeIgniter the behaviour of pagination library. Here we use bootstrap pagination class to show the links in the bootstrap pagination format and active class to tell the users what is the currently active link. Apart from this, we use a model user_model to fetch our data from the database and CodeIgniter pagination library create_links function to create the link for our data. Below is the meaning of some of the term we used above:
  • per_page: It is used to tell the CodeIgniter the number of how many entries will be shown on each page.
  • base_url: It is used to tell the paginated URL base to the CodeIgniter pagination library.
  • total_rows: Total number of entries in our database.
The Model
Create a model file to fetch the data from the database.If you are familiar with the CodeIgniter framework the below code is very easy to understand.
  1. <?phpdefined('BASEPATH') OR exit('No direct script access allowed');
  2. class User_model extends CI_Model{
  3. private function _get_users_data(){
  4. $this->db->select('*');
  5. $this->db->from('users');
  6. }
  7. public function get_users($limit, $start){
  8. $this->_get_users_data();
  9. $this->db->limit($limit, $start);
  10. $query = $this->db->get();
  11. return $query->result_array();
  12. }
  13. public function count_all_users(){
  14. $this->_get_users_data();
  15. $query = $this->db->count_all_results();
  16. return $query;
  17. }
  18. }

Summary

Now you know how to use the CodeIgniter pagination configuration using the bootstrap framework. You can learn other configuration option from CodeIgniter official docs. It is always a good practice to first check the official documentation of anything which you are going to work.
Hope You loved this tutorial if you have any query, suggestion or anything then comment below. We will get back to you as soon as possible.

Comments