article

Sunday, November 23, 2014

Pagination Using Php Codeigniter

Pagination Using Php Codeigniter 

Create Database table
CREATE TABLE `country` (
  `id` smallint(5) UNSIGNED NOT NULL,
  `printable_name` varchar(255) NOT NULL,
  `CountryAbbrev` varchar(10) DEFAULT NULL,
  `CurrencyAbbr` varchar(4) DEFAULT NULL,
  `CurrencyRate` float DEFAULT NULL,
  `CurrencyCode` varchar(3) DEFAULT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
 
Codeigniter Base Url Configuration 
$config['base_url'] = "http://localhost/codeIgniter3_1_10_pagination/";
 

 
Pagination Controller (pagenation.php) application\controllers\pagenation.php

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

class Pagenation extends CI_Controller {
 public function __construct() {
        parent:: __construct();
        $this->load->helper("url");
        $this->load->model("Countries");
        $this->load->library("pagination");
    }
 public function pagenum() 
 {
  $config = array();
        $config["base_url"] = base_url() . "pagenation/pagenum";
   $config["total_rows"] = $this->Countries->record_count();
   $config["per_page"] = 20;
  $config["uri_segment"] = 3;
  $choice = $config["total_rows"] / $config["per_page"];
  $config["num_links"] = round($choice);
     $this->pagination->initialize($config);

    $page = ($this->uri->segment(3))? $this->uri->segment(3) : 0;
    $data["results"] = $this->Countries->fetch_countries($config["per_page"], $page);
    $data["links"] = $this->pagination->create_links();
 
    $this->load->view("expagenation", $data);

 }
}
Pagination Model (Countries.php) application\models\Countries.php
<?php
class Countries extends CI_Model
{
    public function __construct() {
        parent::__construct();
    }
  public function record_count() {
        return $this->db->count_all("country");
    }
 
 public function fetch_countries($limit, $start) {
        $this->db->limit($limit, $start);
        $query = $this->db->get("country");
  if ($query->num_rows() > 0) {
            foreach ($query->result() as $row) {
                $data[] = $row;
            }
            return $data;
        }
        return false;
      
   }


}
create expagenation.php file under folder name application/views/expagenation.php
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Codeigniter 3.1.10 Dev - Pagination</title>
  <link href="https://netdna.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css" rel="stylesheet" id="bootstrap-css">
<script src="https://netdna.bootstrapcdn.com/bootstrap/3.2.0/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container">
  <h1>Countries</h1>
  <div id="body">
<?php
foreach($results as $data) {
    echo $data->printable_name . " - " . $data->CountryAbbrev . "<br>";
}
?>
   <p>
   <div class="pagination">
   <?php echo $links; ?>
   </div>
   </p>
  </div>
  <p class="footer">Page rendered in <strong>{elapsed_time}</strong> seconds</p>
 </div>
  <style>
 .pagination a { 
     padding: .5rem .75rem;
    margin-left: -1px;
    line-height: 1.25;
    color: #007bff;
    background-color: #fff;
    border: 1px solid #dee2e6;
 }
 .pagination strong{ 
     z-index: 2;
    color: #fff;
    cursor: default;
    background-color: #428bca;
    border-color: #428bca;
 padding: 6px 16px;
    margin-left: -1px;
    color: #fff;
    text-decoration: none;
 }
 </style>
</body>
</html>
Run
http://localhost/codeIgniter3_1_10_pagination/pagenation/pagenum

Download http://bit.ly/2Ey5xIu

Related Post