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');
- Autoload the libraries database and pagination in config/autoload.php.
- 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);
}
}
?>
- Load the model Pagination_model.php inside the constructor function.
- Create a function index().
- 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- base_url – url of your function
- total_rows – count the total number of rows. get_count() function declared in Pagination_model will return the total count.
- Specify how many records you want to show at a time in per_page.
- 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.
- Initialize the pagination library by passing configuration. $this->pagination->initialize($config);
- Specify the start as page, if no segments are there the limit starts from 0 else with the third segment.
- Create links with the create_links() library function.
- 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;
}
}
?>
- get_count() function will return total number of records in the states table.
- 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>
- Inside a foreach loop specify our returned result and display values.
- 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 .