Pagination With Codeigniter

Leave a Comment
Hello Guy's,

Pagination is particularly useful when you are coding an application that interfaces with a database. A large dataset might have hundreds of possible results for one query, and pagination creates a much nicer user experience.
In this tutorial, I’ll use CodeIgniter’s pagination library to show you how you can create a paginated list of results from a MySQL database. Along the way, you’ll also see how to fix a problem with the pagination links that the library might produce.
I’ll assume you have an installation of CodeIgniter And you already ready with removing index.php from URL.
If yes then well and good otherwise you can check this tutorials How To Remove Index.php From url
let's Start Creating file in folder

After that create database test and get sql table country from Here
Let's Start with Model i.e mdl_pagination.php
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Mdl_pagination extends CI_Model {

 function __construct(){
  parent::__construct();
 }

 function countcountry(){
  $query = $this->db->get('countries');
  return $query->num_rows();           // return total number of country
 }

 function getcountries($limit,$offset){
  $query = $this->db->get('countries',$limit,$offset);
  return $query->result_array();           // return the country 
 }

}


In Controller i.e pagination.php

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

class Pagination extends CI_Controller {

 function __construct(){
  parent::__construct();
  $this->load->model('mdl_pagination');
  $this->load->library('table');
  $this->load->helper("url");
 }

 public function country($offset=null)
 {
  $this->load->library('pagination');

  $config['base_url'] = 'http://localhost/ci/pagination/country/';    // url of the page
  $config['total_rows'] = $this->mdl_pagination->countcountry(); //get total number of records 
  $config['per_page'] = 20;  // define how many records on page

  $this->pagination->initialize($config);

  $data['country'] = $this->mdl_pagination->getcountries($config['per_page'],$offset);
  $this->load->view('pagination',$data);
 }

}

In View i.e Pagination.php
<html>
<body>
 <div id="container">
  <h1>Countries</h1>
  <div id="body">
 <?php
 $this->table->set_heading('id', 'code', 'name');
 echo $this->table->generate($country);
 echo $this->pagination->create_links();
 ?>
  </div>
 </div>
</body>
</html>


Explanation: Run the page in browser with url http://localhost/ci/pagination/country
In url ci is the name of project, pagination is the name of controller after the last parameter is the name of function which is written in the controller of pagination.


When we call the country function after hitting the url in browser then first the __construct method execute from the pagination controller class it's load the model, table library, url helper and pagination library then it call the country method.In country method first we set the config of the pagination class $config['base_url'] which mean which url is called for the pagination after that how many records we have to fetch that all records we have to call the mdl_pagination which is our model class then we have set how much records we have to show in per_page.
$config['base_url'] = 'http://localhost/ci/pagination/country/';
$config['total_rows'] = $this->mdl_pagination->countcountry();
$config['per_page'] = 20;

we can also also write dynamic base_url like this
$config['base_url'] = base_url().'pagination/country/';

The base_url() function get url which is define in your config.php
$config['base_url'] = 'http://localhost/ci/';

Then we load the config or initialize config by using $this->pagination->initialize($config);. After that we fetch country data from country table using getcountries method of mdl_pagination class of model. We passed two arguments to this function $config['per_page '] as its limit then $offset as its offset of query.

First time when we load page then $config['per_page '] is 20 and $offset is null then its shows first 20 records from the table after than when we click on the next link then url change in to http://localhost/ci/pagination/country/20 then this time the value for the for the $config['per_page '] is 20 and $offset changes from null to 20. It will take 20 from the url which passed as argument in url after country method this time query will take next 20 records after leaving first 20 which is show.After that we fetch all data in $data['country'] and we pass all data in view

In View first we set heading of the table then we generate the table using echo $this->table->generate($country);.The table class which we loaded in __construct method of the controller class it calls the generate method of its by passing the $country array as argument. This generate method create the table of us with value passed as array.

That's it your pagination is ready it will look like this



we can add extra attributes in config like this
$config['next_link'] = ' >> ';
$config['prev_link'] = ' << ';
$config['first_link'] = 'First';
$config['last_link'] = 'Last';
$config['full_tag_open'] = '<p>';
$config['full_tag_close'] = '</p>';
If any error occurs then leave comment after this tutorials we will discuss about that error thank you. If you want to do pagination with ajax you check here.
video tutorials

0 comments:

Post a Comment

Powered by Blogger.