As any database grows, showing all the results of a query on a single page is no longer practical. This is where pagination comes in very useful. One can display his results over a number of pages, each linked to the next, and to allow your users to browse your content in bite sized pieces.
Here I have a class to do the entire pagination job. One has to just use it properly.
Here is the procedure how to use this class.
- First one has to know how many database entry he got. He can do it easily by using a COUNT function of database.
- Then he has to create an object of the PageNavigation class. For this will have to pass three arguments
- first one is for Total number of element
- second one is for which page you want to show now
- And the last is for size of the page.
Such as
$objPageNev=new PageNavigation(120, 2, 25);
- Use $objPageNev->initialMsg and $objPageNev->maxElement to limit the result form start element to max element.
Such as
LIMIT $objPageNev->initialMsg , $objPageNev->maxElement;
- To display the nevagain bar at bottom or top just use the method PagingShowHTML with supplying the root link.
$objPageNev->PagingShowHTML(“http://example.com?index.php?action=cat”)
Negation class code:
<?php
class PageNavigation
{
var $totalPages =null;
var $page =null;
var $totalElement =null;
var $initialMsg =null;
var $threadStart =null;
var $threadEnd =null;
var $maxElement =null;
function PageNavigation($totalElement,$page,$maxElement)
{
if( $totalElement <= $maxElement )
$this->totalPages = 1;
else if( $totalElement % $maxElement == 0 )
$this->totalPages = $totalElement / $maxElement;
else
$this->totalPages = ceil( $totalElement / $maxElement );
if( $page > $this->totalPages || $page<=0 )
$page = 1;
if( $totalElement == 0 )
$this->threadStart = 0;
else
$this->threadStart = $maxElement * $page – $maxElement + 1;
if( $page == $this->totalPages )
$this->threadEnd = $totalElement;
else
$this->threadEnd = $maxElement * $page;
$this->initialMsg = $maxElement * $page – $maxElement;
$this->page=$page;
$this->totalElement=$totalElement;
$this->maxElement=$maxElement;
}
function PagingShowHTML($link)
{
if( $this->page == 1 )
echo “<< Start “;
else
echo “<a href=\”$link&page=1\”><< Start</a> “;
if( $this->page == 1 )
echo “< Prev “;
else
{
$prv=$this->page-1;
echo “<a href=\”$link&page=$prv\”>< Prev</a> “;
}
for( $i = 1; $i <= $this->totalPages; $i++ )
{
if( $this->page == $i )
echo “[$i] “;
else
echo “<a href=\”$link&page=$i\”>$i</a> “;
}
if( $this->page == $this->totalPages)
echo “Next > “;
else
{
$next=$this->page+1;
echo “<a href=\”$link&page=$next\”>Next ></a> “;
}
if( $this->page == $this->totalPages )
echo “End >> “;
else
echo “<a href=\”$link&page=”.$this->totalPages.”\”>End >></a> “;
}
}
?>
Thanks
(Faisal Sikder)