Creating Paging using PHP and MySQL with classes
Paging means showing your query result in multiple pages instead of just put them all in one long page.
MySQL helps to generate paging by using LIMIT clause which will take two arguments. First argument as OFFSET and second argument how many records should be returned from the database.
Below is a simple example to fetch records using LIMIT clause to generate paging.
crate file example.php
<?php
include("page.class.php");
mysql_connect('localhost', 'root', '');
mysql_select_db('pager');
$pager = new paging();
$pager->PageNumber=$_GET[PageNumber];
$pager->sql_query('SELECT * FROM books'); //first time query
echo "<br>Total Pages: ".$pager->TotalPage();
echo "<br>Total Records: ".$pager->TotalRecord();
echo "<br>Current Page : ".$pager->PageNumber();
echo "<br>Next Page : ".$pager->NextPage();
echo "<br>Previous Page : ".$pager->PreviousPage();
echo "<br>Remaining Page : ".$pager->RemainingPage();
echo "<div style=\"background:gray;\" id=\"records\">";
echo"<hr>";
foreach($pager->mysql_fetch() as $row){
echo "<br>".$row['ID']."-".$row['BookName'];
}
echo "</div>";
echo "<div style=\"background:gray; display:none;\" id=\"pending\"><img src=\"ajax_small.gif\" ></div>";
echo"<hr>";
////////////PreviousPage//////////////////////
if($pager->PreviousPage())
{
echo " <a href='?PageNumber=".$pager->PreviousPage()."'>Previous</a>";
}
///////////////////////////////////////////////////
foreach($pager->PageLink() as $key=>$value){
if($pager->PageNumber()==$key)
{
echo "<b>".$key."</b>";
}
else
{
echo " <a href='?PageNumber=".$key."' >".$key."</a>";
}
}
////////////PreviousPage//////////////////////
if($pager->NextPage())
{
echo " <a href='?PageNumber=".$pager->NextPage()."'>Next</a>";
}
?>
create paging classes
paging.php
<?Php
class paging
{
public $PageNumber=1;
var $PerPages = 10;
var $PageNumberLimit=10;
var $PerPageStart=0;
function sql_query($sql)
{
$this->sql_num = mysql_query($sql); //execute query
$this->PageCalc();
$this->sql=mysql_query("$sql LIMIT $this->PerPageStart , $this->PerPages");
}
function TotalPage()
{
$this->TotalPage = $this->TotalRecord() / $this->PerPages;
return ceil($this->TotalPage);
}
function TotalRecord()
{
$this->TotalRecord=@mysql_num_rows($this->sql_num);
return $this->TotalRecord;
}
function RemainingPage()
{
$this->RemainingPage=$this->TotalPage()-$this->PageNumber;
return $this->RemainingPage;
}
function PageCalc()
{
if($this->PageNumber>$this->TotalPage() or empty($this->PageNumber))
{
$this->PageNumber=1;
}
$this->PerPageStart=(($this->PageNumber-1) * $this->PerPages+1)-1;
}
function PageLink()
{
$this->calc = ceil(($this->PerPageStart) / $this->PerPages / $this->PageNumberLimit);
for($b=0;$b <= $this->calc ;$b++){
$this->StartCalc = $b * $this->PageNumberLimit;
$this->EndCalc = $this->StartCalc + $this->PageNumberLimit ;
if ($this->EndCalc <= $this->TotalPage)
{
if($this->PageNumber >= $this->StartCalc)
{
$this->End = $this->EndCalc;
$this->Start =$this->StartCalc;
}
}
if ($this->EndCalc >= $this->TotalPage)
{
if($this->PageNumber >= $this->StartCalc)
{
$this->End= $this->TotalPage;
$this->Start=$this->StartCalc;
}
}
if(empty($this->Start))
{
$this->Start=1;
}
}
for($i=ceil($this->Start); $i <= ceil($this->End) ;$i++){
$list[$i]=$this->i;
}
return $list;
}
function mysql_fetch()
{
while($row=mysql_fetch_array($this->sql))
{
$rows[] = $row;
}
return $rows;
}
function PreviousPage()
{
if($this->PageNumber)
{
$PreviousNumber = $this->PageNumber-1;
}
return $PreviousNumber;
}
function NextPage()
{
if ($this->PageNumber < $this->TotalPage)
{
$NextNumber = $this->PageNumber+1;
}
return $NextNumber;
}
function PageNumber()
{
return $this->PageNumber;
}
}
?>
Paging means showing your query result in multiple pages instead of just put them all in one long page.
MySQL helps to generate paging by using LIMIT clause which will take two arguments. First argument as OFFSET and second argument how many records should be returned from the database.
Below is a simple example to fetch records using LIMIT clause to generate paging.
crate file example.php
<?php
include("page.class.php");
mysql_connect('localhost', 'root', '');
mysql_select_db('pager');
$pager = new paging();
$pager->PageNumber=$_GET[PageNumber];
$pager->sql_query('SELECT * FROM books'); //first time query
echo "<br>Total Pages: ".$pager->TotalPage();
echo "<br>Total Records: ".$pager->TotalRecord();
echo "<br>Current Page : ".$pager->PageNumber();
echo "<br>Next Page : ".$pager->NextPage();
echo "<br>Previous Page : ".$pager->PreviousPage();
echo "<br>Remaining Page : ".$pager->RemainingPage();
echo "<div style=\"background:gray;\" id=\"records\">";
echo"<hr>";
foreach($pager->mysql_fetch() as $row){
echo "<br>".$row['ID']."-".$row['BookName'];
}
echo "</div>";
echo "<div style=\"background:gray; display:none;\" id=\"pending\"><img src=\"ajax_small.gif\" ></div>";
echo"<hr>";
////////////PreviousPage//////////////////////
if($pager->PreviousPage())
{
echo " <a href='?PageNumber=".$pager->PreviousPage()."'>Previous</a>";
}
///////////////////////////////////////////////////
foreach($pager->PageLink() as $key=>$value){
if($pager->PageNumber()==$key)
{
echo "<b>".$key."</b>";
}
else
{
echo " <a href='?PageNumber=".$key."' >".$key."</a>";
}
}
////////////PreviousPage//////////////////////
if($pager->NextPage())
{
echo " <a href='?PageNumber=".$pager->NextPage()."'>Next</a>";
}
?>
create paging classes
paging.php
<?Php
class paging
{
public $PageNumber=1;
var $PerPages = 10;
var $PageNumberLimit=10;
var $PerPageStart=0;
function sql_query($sql)
{
$this->sql_num = mysql_query($sql); //execute query
$this->PageCalc();
$this->sql=mysql_query("$sql LIMIT $this->PerPageStart , $this->PerPages");
}
function TotalPage()
{
$this->TotalPage = $this->TotalRecord() / $this->PerPages;
return ceil($this->TotalPage);
}
function TotalRecord()
{
$this->TotalRecord=@mysql_num_rows($this->sql_num);
return $this->TotalRecord;
}
function RemainingPage()
{
$this->RemainingPage=$this->TotalPage()-$this->PageNumber;
return $this->RemainingPage;
}
function PageCalc()
{
if($this->PageNumber>$this->TotalPage() or empty($this->PageNumber))
{
$this->PageNumber=1;
}
$this->PerPageStart=(($this->PageNumber-1) * $this->PerPages+1)-1;
}
function PageLink()
{
$this->calc = ceil(($this->PerPageStart) / $this->PerPages / $this->PageNumberLimit);
for($b=0;$b <= $this->calc ;$b++){
$this->StartCalc = $b * $this->PageNumberLimit;
$this->EndCalc = $this->StartCalc + $this->PageNumberLimit ;
if ($this->EndCalc <= $this->TotalPage)
{
if($this->PageNumber >= $this->StartCalc)
{
$this->End = $this->EndCalc;
$this->Start =$this->StartCalc;
}
}
if ($this->EndCalc >= $this->TotalPage)
{
if($this->PageNumber >= $this->StartCalc)
{
$this->End= $this->TotalPage;
$this->Start=$this->StartCalc;
}
}
if(empty($this->Start))
{
$this->Start=1;
}
}
for($i=ceil($this->Start); $i <= ceil($this->End) ;$i++){
$list[$i]=$this->i;
}
return $list;
}
function mysql_fetch()
{
while($row=mysql_fetch_array($this->sql))
{
$rows[] = $row;
}
return $rows;
}
function PreviousPage()
{
if($this->PageNumber)
{
$PreviousNumber = $this->PageNumber-1;
}
return $PreviousNumber;
}
function NextPage()
{
if ($this->PageNumber < $this->TotalPage)
{
$NextNumber = $this->PageNumber+1;
}
return $NextNumber;
}
function PageNumber()
{
return $this->PageNumber;
}
}
?>
No comments:
Post a Comment