Codeigniter Pagination

How to do codeigniter pagination with database?

Codeigniter pagination is a very simple method to show large amount of data with a limit. Displaying large amount of data at a time will be difficult and it will cause server down also it will take too much time to load the page.

Codeigniter pagination is a very simple method and it requires few steps only.

Here I am listing states, you can download the sql file from Countries States Cities database

For state only click this link States

Codeigniter :

$autoload['libraries'] =  array('database','pagination');
  1. Autoload the libraries database and pagination in config/autoload.php.
  2. Create a controller Pagination.php

Controllers/Pagination.php

<?php defined('BASEPATH') OR exit('No direct script access allowed');

class Pagination extends CI_Controller {

    public function __construct() {
        parent:: __construct();
        $this->load->model('Pagination_model');
    }

    public function index()
    {  
    	$config = array();
        $config["base_url"] = base_url() . "Pagination/index";
        $config["total_rows"] = $this->Pagination_model->get_count();
        $config["per_page"] = 10;
        $config["uri_segment"] = 3;
        $this->pagination->initialize($config);
        $page = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;
        $data["links"] = $this->pagination->create_links();
    	$data['states'] = $this->Pagination_model->getAllData($config["per_page"], $page);
    	$this->load->view('pagination',$data);
    }
}
?>
  1. Load the model Pagination_model.php inside the constructor function.
  2. Create a function index().
  3. We have to specify some configuration as follows in the $config array : The $config array is passed to the $this->pagination->initialize(). There are some twenty items you can configure, but you need atleast three configurations. For more details please visit this link Codeigniter pagination details
    1. base_url – url of your function
    2. total_rows – count the total number of rows. get_count() function declared in Pagination_model will return the total count.
    3. Specify how many records you want to show at a time in per_page.
    4. Specify uri_segment ; Allow you to retrieve a specific segment form URI string where n is a segment number. For example : http://localhost/test/Pagination/index/40 here Pagination is the first segment, index is the second segment and 40 is the third segment. So the segment starts with 1 and its counted immediately after our base_url.
  4. Initialize the pagination library by passing configuration. $this->pagination->initialize($config);
  5. Specify the start as page, if no segments are there the limit starts from 0 else with the third segment.
  6. Create links with the create_links() library function.
  7. Get the records from db with per_page limit and and the start.

Remember name of controller should start with capital letter.

Model/Pagination_model :

<?php if (!defined('BASEPATH')) exit('No direct script access allowed');


class Pagination_model extends MY_Model
{
	public function __construct()
	{
		parent::__construct();
	}
  
	public function get_count()
	{
		$result = $this->db->select('*')->from('states')->get()->num_rows();
		return $result;
	}

	public function getAllData($limit, $start)
	{
		$result = $this->db->select('states.id,states.name as state,countries.name as country')->from('states')->join('countries','countries.id=states.country_id')->limit($limit, $start)->get()->result();
		return $result;
	}
  
	

}
?>
  1. get_count() function will return total number of records in the states table.
  2. getAllData() function needs to parameters , per page limit and from where the fetching should start. The countries tble also joined to show the country name. In select() only the required fields specified.

Remember name of model should start with capital letter.

View/pagination.php

<!DOCTYPE html>
<html>
<head>
   <title></title>
   <link href="//netdna.bootstrapcdn.com/twitter-bootstrap/2.3.2/css/bootstrap-combined.min.css" rel="stylesheet" id="bootstrap-css">
    <script src="//netdna.bootstrapcdn.com/twitter-bootstrap/2.3.2/js/bootstrap.min.js"></script>
    <style type="text/css">
        .pagination>p>a 
	{ 
	     border: 1px solid #E3E3E3;
	     border-radius: 50% !important;
             margin: 0 5px;
	     padding: 10px;
	}
	.pagination>p>strong 
	{ 
             border: 1px solid #E3E3E3;
	     border-radius: 50% !important;
	     margin: 0 5px;
	     padding: 10px;
             color: #fff;
	     background-color: blue;
	}
    </style>
</head>
<body style="margin: 50px;">
<h3 class="text-center"><u>Codeigniter Pagination</u></h3>
<div class="well">
    <table class="table">
      <thead>
        <tr>
          <th>#</th>
          <th>State</th>
          <th>Country</th>
        </tr>
      </thead>
      <tbody>
      	<?php
      	if(isset($states))
      	{
      		$slno = 0;
      		foreach($states as $row)
      		{
      			$slno += 1;
  			?>
  			<tr>
	          	<td><?=$row->id?></td>
	          	<td><?=$row->state?></td>
	          	<td><?=$row->country?></td>
	        </tr>
  			<?php
      		}
      	}
      	?>
        
      </tbody>
    </table>
</div>
<div class="pagination">
    <p><?php echo $links; ?></p>
</div>
</body>
</html>
  1. Inside a foreach loop specify our returned result and display values.
  2. For showing pagination you have to specify the $links variable wherever you want to show the pagination.

Include bootstrap css and js for better look :

Bootstrap CSS : https://netdna.bootstrapcdn.com/twitter-bootstrap/2.3.2/css/bootstrap-combined.min.css

Bootstrap JS : https://netdna.bootstrapcdn.com/twitter-bootstrap/2.3.2/js/bootstrap.min.js

I given some extra css for the pagination numbers as follows

<style type="text/css">
.pagination>p>a 
{ 
    border: 1px solid #E3E3E3;
    border-radius: 50% !important;
    margin: 0 5px;
    padding: 10px;
}
.pagination>p>strong 
{ 
    border: 1px solid #E3E3E3;
    border-radius: 50% !important;
    margin: 0 5px;
    padding: 10px;
    color: #fff;
    background-color: blue;
}
</style>

For more codeigniter solutions please visit Codeigniter .

Related Posts

send mail in codeigniter

Send mail in CodeIgniter

How to send mail in CodeIgniter using the normal mail() email library. We can send mail in codeigniter using the mail() and SMTP methods. To know how…

smtp mail in codeigniter

Sending SMTP mail in CodeIgniter

How to send SMTP mail in CodeIgniter ? We have normal mail() function in CodeIgniter but in some servers we may have to send emails using SMTP…

generate qr code using codeigniter

Generate QR code using codeigniter

How to generate QR code using codeigniter ? In this tutorial we’ll discuss about generate QR code using codeigniter. QR code is an another form of barcode…

generate barcode in codeigniter

Generate barcode in codeigniter using zend library

How to generate barcode in codeigniter? Create display barcode using codeigniter with zend barcode library In this article we’ll discuss how to generate barcode in codeigniter using…

upload image from url

Upload image from URL in codeigniter

How to upload image from url in codeigniter? Rather than selecting a file from drive and uploading into folder we can upload image from URL. For that…

Create PDF in Codeigniter using Dompdf – flexible library

How to generate a PDF in Codeigniter using Dompdf ? In this tutorial we will create pdf in codeigniter using Dompdf. Here I used image, inline css,…

Leave a Reply

Your email address will not be published. Required fields are marked *