Version: 2.0.1
Type: Class
Category: Databases
License: GNU General Public License
Description: A Class for paging MySQl results (included an example on how to use it)
<?
/***************************************
Title : Page MySQL results
File : paging.cls.php
Version : 2.0.0
Last Edit : 04.12.2002 - 15:51:02
Developer : George Kosmidis
****************************************/
///////////////e.g.///////////////////
/***************************************
require "../common/paging.cls.php";
//connect
mysql_connect("localhost", "anaptixiaki", "anaptixiaki"));
mysql_select_db("mysql"));
//instanciate
$paging=new clPaging();
//set vars
if(isset($HTTP_GET_VARS["start"])) $paging->clPaging_setStart($HTTP_GET_VARS["start"]);
if(isset($HTTP_GET_VARS["fix"])) $paging->clPaging_setFix($HTTP_GET_VARS["fix"]);
if(isset($HTTP_GET_VARS["pp"])) $paging->clPaging_setPerPage($HTTP_GET_VARS["pp"]);
else $paging->clPaging_setPerPage("1");
//create query
$selectResults="SELECT * ";
$selectCount="SELECT COUNT(*) AS count ";
$mainQuery="FROM user ";
$setLimits=" LIMIT ".$paging->clPaging_getStart().",".$paging->clPaging_getPerPage();
$Results=mysql_query($selectResults.$mainQuery.$setLimits);
//show results
while($Row=mysql_fetch_object($Results)) {
echo$Row->Host."-".$Row->User."<br>";
}
//set query for paging
$paging->clPaging_setQuery($selectCount.$mainQuery);
$paging->clPaging_Pagination();
//output results
echo "<br>".$paging->clPaging_getPreviousLink()."-".$paging->clPaging_getNextLink()."<br>";
echo $paging->clPaging_getPageNo()."<br>";
echo $paging->clPaging_getPages()."<br>";
echo "Results".$paging->clPaging_getRecords();
*/
class clPaging {
var $PerPage=10;
var $Start=0;
var $Query;
var $PreviousLink;
var $NextLink;
var $Records;
var $PageNo;
var $Pages;
function clPaging_setPerPage($newPerPage){$this->PerPage=$newPerPage;}
function clPaging_setStart($newStart){$this->Start=$newStart;}
function clPaging_setQuery($newQuery){$this->Query=$newQuery;}
function clPaging_setPreviousLink($newPreviousLink){$this->PreviousLink=$newPreviousLink;}
function clPaging_setNextLink($newNextLink){$this->NextLink=$newNextLink;}
function clPaging_setRecords($newRecords){$this->Records=$newRecords;}
function clPaging_setPageNo($newPageNo){$this->PageNo=$newPageNo;}
function clPaging_setPages($newPages){$this->Pages=$newPages;}
function clPaging_getPerPage(){Return $this->PerPage;}
function clPaging_getStart(){Return $this->Start;}
function clPaging_getQuery(){Return $this->Query;}
function clPaging_getPreviousLink(){Return $this->PreviousLink;}
function clPaging_getNextLink(){Return $this->NextLink;}
function clPaging_getRecords(){Return $this->Records;}
function clPaging_getPageNo(){Return $this->PageNo;}
function clPaging_getPages(){Return $this->Pages;}
function clPaging_Pagination() {
$Query=$this->clPaging_getQuery();
$Start=$this->clPaging_getStart();
$PerPage=$this->clPaging_getPerPage();
$Result = mysql_query ($Query);
$row = mysql_fetch_array($Result);
$numrows = $row['count'];
if(($Start)>=$PerPage)
$this->clPaging_setPreviousLink("<a href=\"".$PHP_SELF."
?pp=$PerPage
&start=".($Start-$PerPage).
"\">Previous</a>\n");
else
$this->clPaging_setPreviousLink("Previous");
if($numrows>(($this->$Start)+$PerPage))
$this->clPaging_setNextLink("<a href=\"".$PHP_SELF."
?pp=$PerPage
&start=".($Start+$PerPage).
"\">Next</a>\n");
else
$this->clPaging_setNextLink("Next");
for($i=1; $i<=ceil(($numrows/$PerPage)); $i++) {
if($i==floor(($Start/$PerPage)+1))
$Pages.=" $i ";
else
$Pages.=" <a href=\"".$PHP_SELF."
?pp=$PerPage
&start=".(($i-1)*$PerPage).
"\">$i</a> ";
}
$this->clPaging_setPages($Pages);
$this->clPaging_setPageNo("Page ".floor(($Start/$PerPage)+1)." of ".ceil(($numrows/$PerPage)));
$this->clPaging_setRecords($numrows);
}
}
?>