How to create a Pagination in CodeIgniter

Installing CodeIgniter

If you don't have CodeIgniter installed yet, you can use this link to download the latest version of CodeIgniter which is 3.1.7 that I've used in this source code. After downloading, extract the file in the folder of your server. Since I'm using XAMPP as my localhost server, I've put the folder in htdocs folder of my XAMPP. Then, you can test whether you have successfully installed codeigniter by typing your app name in your browser. In my case, I named my app as codeigniter_pagination so I'm using the below code.
  1. localhost/codeigniter_pagination
codeigniter successful install

Removing index.php in our URLs

By default, index.php is included in every URL of our app. To remove this, please refer to my tutorial How to Remove Index.Php in the URL of Codeigniter Application.

Creating our Database

I've included a .sql file located in db folder in the downloadable of this tutorial. All you need to do is import the file into your phpMyAdmin. If you have no idea how to import, please refer to my tutorial How import .sql file to restore MySQL database. You should be able to add a database named mydatabase.

Connecting our App into our Database

Next, we're going to connect our codeigniter application to the database that we created earlier. 1. In your codeigniter app folder, open database.php located in application/config folder. 2. Update database.php with your credential the same as what I did below.
  1. $db['default'] = array(
  2. 'dsn' => '',
  3. 'hostname' => 'localhost',
  4. 'username' => 'root',
  5. 'password' => '',
  6. 'database' => 'mydatabase',
  7. 'dbdriver' => 'mysqli',
  8. 'dbprefix' => '',
  9. 'pconnect' => TRUE,
  10. 'db_debug' => (ENVIRONMENT !== 'production'),
  11. 'cache_on' => FALSE,
  12. 'cachedir' => '',
  13. 'char_set' => 'utf8',
  14. 'dbcollat' => 'utf8_general_ci',
  15. 'swap_pre' => '',
  16. 'encrypt' => FALSE,
  17. 'compress' => FALSE,
  18. 'stricton' => FALSE,
  19. 'failover' => array(),
  20. 'save_queries' => TRUE
  21. );

Configuring our App

Next, we configure our base url and remove index.php in our index page. 1. In your codeigniter app folder, open config.php located in application/config folder. 2. Find and edit the ff lines:
  1. $config['base_url'] = 'http://localhost/codeigniter_pagination';
  1. $config['index_page'] = '';

Creating our Model

Next, we create the model for our app. Take note that the first letter of your model name should be in CAPITAL letter and the name of the model should be the same as the file name to avoid confusion. Create a file named Members_model.php in application/models folder of our app and put the ff codes:
  1. <?php
  2. defined('BASEPATH') OR exit('No direct script access allowed');
  3.  
  4. class Members_model extends CI_Model {
  5. function __construct(){
  6. parent::__construct();
  7. $this->load->database();
  8. }
  9.  
  10. public function get_current_page($limit, $start) {
  11. $this->db->limit($limit, $start);
  12. $query = $this->db->get('members');
  13. $rows = $query->result();
  14.  
  15. if ($query->num_rows() > 0) {
  16. foreach ($rows as $row) {
  17. $data[] = $row;
  18. }
  19.  
  20. return $data;
  21. }
  22.  
  23. return false;
  24. }
  25.  
  26. public function get_total() {
  27. return $this->db->count_all('members');
  28. }
  29. }

Creating our Controller

Next step is to create our controller. Controllers follow the same naming convention as models. Create a file named Member.php in application/controllers folder of our app and put the ff codes.
  1. <?php
  2. defined('BASEPATH') OR exit('No direct script access allowed');
  3.  
  4. class Member extends CI_Controller {
  5. function __construct(){
  6. parent::__construct();
  7. $this->load->library('pagination');
  8. $this->load->helper('url');
  9. $this->load->model('members_model');
  10. }
  11. public function index(){
  12. //set params
  13. $params = array();
  14. //set records per page
  15. $limit_page = 1;
  16. $page = ($this->uri->segment(3)) ? ($this->uri->segment(3) - 1) : 0;
  17. $total = $this->members_model->get_total();
  18.  
  19. if ($total > 0)
  20. {
  21. // get current page records
  22. $params['results'] = $this->members_model->get_current_page($limit_page, $page * $limit_page);
  23.  
  24. $config['base_url'] = base_url() . 'member/index';
  25. $config['total_rows'] = $total;
  26. $config['per_page'] = $limit_page;
  27. $config['uri_segment'] = 3;
  28.  
  29. //paging configuration
  30. $config['num_links'] = 2;
  31. $config['use_page_numbers'] = TRUE;
  32. $config['reuse_query_string'] = TRUE;
  33.  
  34. //bootstrap pagination
  35. $config['full_tag_open'] = '<ul class="pagination">';
  36. $config['full_tag_close'] = '</ul>';
  37. $config['first_link'] = '&laquo; First';
  38. $config['first_tag_open'] = '<li>';
  39. $config['first_tag_close'] = '</li>';
  40. $config['last_link'] = 'Last &raquo';
  41. $config['last_tag_open'] = '<li>';
  42. $config['last_tag_close'] = '</li>';
  43. $config['next_link'] = 'Next';
  44. $config['next_tag_open'] = '<li>';
  45. $config['next_tag_close'] = '<li>';
  46. $config['prev_link'] = 'Prev';
  47. $config['prev_tag_open'] = '<li>';
  48. $config['prev_tag_close'] = '<li>';
  49. $config['cur_tag_open'] = '<li class="active"><a href="#">';
  50. $config['cur_tag_close'] = '</a></li>';
  51. $config['num_tag_open'] = '<li>';
  52. $config['num_tag_close'] = '</li>';
  53.  
  54. $this->pagination->initialize($config);
  55.  
  56. // build paging links
  57. $params['links'] = $this->pagination->create_links();
  58. }
  59.  
  60. $this->load->view('member_table', $params);
  61. }
  62. }

Configuring our Default Controller

Next, we are going to set our default controller so that whenever we haven't set up a controller to use, this default controller will be used instead. Open routes.php located in application/config folder and set the default route to our user controller. Note: While we name controllers using CAPITAL letter in this first letter, we refer to them in SMALL letter.
  1. $route['default_controller'] = 'member';

Creating our View

Lastly, we create the views of our app. Take note that I've use Bootstrap in the views. You may download bootstrap using this link. Create the ff files inside application/views folder. member_table.php
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <title>CodeIgniter Pagination</title>
  6. <link rel="stylesheet" type="text/css" href="<?php echo base_url(); ?>bootstrap/css/bootstrap.min.css">
  7. </head>
  8. <body>
  9. <div class="container">
  10. <h1 class="page-header text-center">How to create a Pagination in CodeIgniter</h1>
  11. <div class="row">
  12. <div class="col-sm-8 col-sm-offset-2">
  13. <table class="table table-bordered table-striped">
  14. <thead>
  15. <tr>
  16. <th>ID</th>
  17. <th>Firstname</th>
  18. <th>Lastname</th>
  19. <th>Address</th>
  20. </tr>
  21. </thead>
  22. <tbody>
  23. <?php
  24. foreach($results as $row){
  25. ?>
  26. <tr>
  27. <td><?php echo $row->id; ?></td>
  28. <td><?php echo $row->firstname; ?></td>
  29. <td><?php echo $row->lastname; ?></td>
  30. <td><?php echo $row->address; ?></td>
  31. </tr>
  32. <?php
  33. }
  34. ?>
  35. </tbody>
  36. </table>
  37. <?php
  38. if(isset($links)){
  39. echo $links;
  40. }
  41. ?>
  42. </div>
  43. </div>
  44. </div>
  45. </body>
  46. </html>
That ends this tutorial. Happy Coding :)

Add new comment