DB Pager maxpages algorithm and lastpagefrom variable
| From: | Richard Wall | Date: | Tue, 23 Jul 2002 10:34:40 +0000 |
| Subject: | DB Pager maxpages algorithm and lastpagefrom variable | ||
| Groups: | php.pear.dev | ||
| Request: | Send a blank email to pear-dev+get-7904@lists.php.net to get a copy of this message | ||
Hello,
I've extended the DB Pager getData() method so that the pages array stays a constant length and have added a lastpagefrom value to allow for a link to the last page in the result set.
Hope you find it useful.
-RichardW.
<?php
require_once('DB/Pager.php');
class dbPager extends DB_Pager
{
function getData($from, $limit, $numrows, $maxpages = false)
{
/*
* Don't pass the maxpages to DB_Pager
* because we need the whole pages array to calc lastpagefrom
*/
$pagerData = parent::getData($from, $limit, $numrows);
// Set the maxpages here it won't be done by parent class
$pagerData["maxpages"] = $maxpages;
// Added variable to enable a link to 'Last page'
$pagerData["lastpagefrom"] = $pagerData["pages"][count($pagerData["pages"])];
// A different implementation of the max pages algorithm
// which keeps the pages array a constant length
if ( $pagerData["numpages"] > $maxpages )
{
$lowerLimit = $pagerData["current"] - ($maxpages/2);
$upperLimit = $pagerData["current"] + ($maxpages/2);
if ( $lowerLimit < 1 )
{
$lowerLimit = 1;
$upperLimit = $maxpages+1;
}
if ( $upperLimit > $pagerData["numpages"] )
{
$upperLimit = $pagerData["numpages"]+1;
$lowerLimit = $upperLimit - $maxpages;
}
for ( $i=$lowerLimit; $i<$upperLimit; $i++ )
{
$newPageList[$i] = $pagerData["pages"][$i];
}
$pagerData["pages"] = $newPageList;
}
return $pagerData;
}
}
?>