If the number of records on the left is less than the value of $space (page number section), the page number $start increases from 1 to the right. If the number of records on the left is more than the value of $left (the number of pages displayed on the left and right), $start will start counting by subtracting the $left value from the number of records on the left.
/**Realize page turning of similar products **/
class pager
{
protected $space;
protected $left;
protected $DB;
protected $pageName;
public function setSpace($num) {
$this->space = $num;
$this->left = ceil(($num-1)/2);
}
public function setDB( &$db) {
$this->DB = $db;
}
public function setPageName($pageName) {
$this->pageName = $pageName;
}
public function getPages($catid, $exptime) {
$fields = array("`id`,`title`");
$left = array("> " => array("exptime"=>$exptime), "memberid" => gs(_MEM_PREFIX_ . "memberid"), "catid"=>$catid);
$right = array(" <" => array("exptime"=>$exptime), "memberid" => gs(_MEM_PREFIX_ . "memberid"), "catid"=>$catid);
$ leftCount = $this->DB->getCount($left);
if($leftCount <= $this->left) {
$star = 1;
$ leftLimit = "LIMIT" . $leftCount;
$rightLimit = "LIMIT " . ($this->space-$leftCount);
}
else {
$start = $leftCount - $ this->left;
$leftLimit = "LIMIT " . $this->left;
$rightLimit = $leftLimit;
}
$list1 = $this-> DB->findAll($left, array("exptime"=>"ASC"), $leftLimit, $fields);
$list2 = $this->DB->findAll($right, array ("exptime"=>"DESC"), $rightLimit, $fields);
/**Previous page link **/
$c = count($list1);
if( $c > 1) {
$url = $this->pageName."-".$list1[$c]['id'].".html";
$pages = "< a href="{$url}">Previous page
";
}elseif($c == 1) {
$url = $this->pageName ."-".$list1[0]['id'].".html";
$pages = "Previous page< ;ol>";
}else {
$pages = "";
}
/**Content on the left of the current page **/
foreach($list1 as $ item) {
$url = $this->pageName."-".$item['id'].".html";
$pages .= "- {$start}
";
$start++;
}
$pages .= "- < ;b>{$leftCount}
";
$start++;
/**Content on the right side of the current page **/
foreach($list1 as $item) {
$url = $this->pageName."-".$item['id'].".html";
$pages .= "- {$start}
";
$start++;
}
/**Link to next page **/
$ c = count($list2);
if($c > 0) {
$url = $this->pageName."-".$list2[0]['id'].". html";
$pages .= "next page";
}else {
$pages . = "";
}
return $pages;
}
};
?>