File: paginator.php
<?php
require_once 'DB.php';
class Paginator
{
private $queryStr;
private $pageSize;
private $currentPage=1;
private $conn;
private $totalRecords;
private $totalPages;
public function __construct($queryStr,$pageSize,$totalRecords)
{
$this->conn=DB::db_connect();
$this->queryStr=$queryStr;
if(isset($_REQUEST['showpage']))
$this->currentPage=$_REQUEST['showpage'];
$this->pageSize=$pageSize;
$this->totalRecords=$totalRecords;
$this->totalPages=ceil(($this->totalRecords)/($this->pageSize));
}
public function getPage()
{
$offset=($this->currentPage-1)*($this->pageSize);
$queryStr="$this->queryStr limit $offset,$this->pageSize";
$result=$this->conn->query($queryStr);
return $result;
}
public function showNavigationLinks()
{
echo "<div id='nav'>";
if($this->currentPage>1)
{
echo "<a href=index.php?showpage=".($this->currentPage-1).">Prev</a>";
}
echo " ";
for($i=1;$i<=$this->totalPages;$i++)
{
echo "<a href=index.php?showpage=".$i.">".$i."</a> ";
}
echo " ";
if(($this->totalRecords)>($this->currentPage)*($this->pageSize))
{
echo "<a href=index.php?showpage=".($this->currentPage+1).">Next</a>";
}
echo "</div>";
}
}
?>
require_once 'DB.php';
class Paginator
{
private $queryStr;
private $pageSize;
private $currentPage=1;
private $conn;
private $totalRecords;
private $totalPages;
public function __construct($queryStr,$pageSize,$totalRecords)
{
$this->conn=DB::db_connect();
$this->queryStr=$queryStr;
if(isset($_REQUEST['showpage']))
$this->currentPage=$_REQUEST['showpage'];
$this->pageSize=$pageSize;
$this->totalRecords=$totalRecords;
$this->totalPages=ceil(($this->totalRecords)/($this->pageSize));
}
public function getPage()
{
$offset=($this->currentPage-1)*($this->pageSize);
$queryStr="$this->queryStr limit $offset,$this->pageSize";
$result=$this->conn->query($queryStr);
return $result;
}
public function showNavigationLinks()
{
echo "<div id='nav'>";
if($this->currentPage>1)
{
echo "<a href=index.php?showpage=".($this->currentPage-1).">Prev</a>";
}
echo " ";
for($i=1;$i<=$this->totalPages;$i++)
{
echo "<a href=index.php?showpage=".$i.">".$i."</a> ";
}
echo " ";
if(($this->totalRecords)>($this->currentPage)*($this->pageSize))
{
echo "<a href=index.php?showpage=".($this->currentPage+1).">Next</a>";
}
echo "</div>";
}
}
?>
File: DB.php
<?php
class DB
{
public static function db_connect()
{
$result = new mysqli('localhost', 'root', '', 'test');
if (!$result)
return false;
return $result;
}
}
?>
class DB
{
public static function db_connect()
{
$result = new mysqli('localhost', 'root', '', 'test');
if (!$result)
return false;
return $result;
}
}
?>
File: index.php
<?php
require_once 'paginator.php';
require_once 'DB.php';
require_once 'paginator.php';
require_once 'DB.php';
//I have a table 'countries' having two fields id, name
$mainquery='Select * from countries';
$countquery='Select count(id) as rec_count from countries';
$conn=DB::db_connect();
$result=$conn->query($countquery);
$row=$result->fetch_object();
$totalRecords=$row->rec_count;
$pg=new Paginator($mainquery,6,$totalRecords);
$pageData=$pg->getPage();
while($row=$pageData->fetch_object())
{
echo '<br />'.$row->id.'-'.$row->name;
}
echo '<br />';
$pg->showNavigationLinks();
?>