CodeIgniter pagination for Bootstrap 4

This is a quick guide for configuring the pagination links generated by CodeIgniter with Bootstrap 4

CodeIgniter allows to configure the link generated using and configuration array with given key value pairs. The complete documentation on the pagination library on what are the configurable elements are available at https://www.codeigniter.com/userguide3/libraries/config.html?highlight=config Here we will see how to use that for generating HTML markup for Bootstrap 4. 

Step 1

Create a new file named "pagination.php" in the application/config direcotry of the CodeIgniter framework installation. Include the following code in it.


<?php 
$config['pagination_config'] = array(
            'per_page' => '1',
            'full_tag_open' => '<ul class="pagination">',
            'full_tag_close' => '</ul>',
            'num_tag_open' => '<li class="page-item"><span class="page-link">',
            'num_tag_close' => '</span></li>',
            'cur_tag_open' => '<li class="page-item active"><a class="page-link" href="#">',
            'cur_tag_close' => '</a></li>',
            'prev_tag_open' => '<li class="page-item"><span class="page-link">',
            'prev_tag_close' => '</span></li>',
            'next_tag_open' => '<li class="page-item"><span class="page-link">',
            'next_tag_close' => '</span></li>',
            'prev_link' => '<i class="icon-backward"></i>',
            'next_link' => '<i class="icon-forward"></i>',
            'last_tag_open' => '<li class="page-item"><span class="page-link">',
            'last_tag_close' => '</span></li>',
            'first_tag_open' => '<li class="page-item"><span class="page-link">',
            'first_tag_close' => '</span></li>',
            'suffix' => '.html');
?>

Step 2

In the controller write the following code to load pagination library and the configuration from the configuration file.


<?php
        $this->load->library('pagination');
        $this->load->config('pagination');
        $config = $this->config->item('pagination_config');
        $config['base_url'] = $base_url;
        $config['total_rows'] = 100; // Replace to dynamically pass actual result count
        $this->pagination->initialize($config);
        $data['content'] = 'view_name'; //template view in next step will load the view passed
        $this->load->view('template', $data);
?>

 

Step 3

In the view have the following code to generate and display the pagination link.


<?php
        echo $this->pagination->create_links();
?>

This is end of this quick guide. However if you use our PHP Website Generator here then you don't need to worry about all these things as pagination is automatically generated with SEO friendly URL. You can configure number of items for page in the relevant action type when generating code.

Website in minutes

Do you know that you can generate a complete PHP websites in minutes with PHP Website Generator?

Features

  • Free admin dashboard
  • Role based security
  • Login with Facebook, Google and more
  • Queue based email processing
  • Price at $5 for website
  • Uses CodeIgniter framework

Generate website now See Pricing



You can generate an entire website in CodeIgniter using our PHP Website Generator using easy to use wizards. PHP Website Generator provide many features including search with pagination, AJAX based grid data editors, Group based security and free Admin dashboard etc. Please connect with Google or Facebook and to generate your CodeIgniter website in minutes.