php实现的仿阿里巴巴实现同类产品翻页

时间:2021-05-26

如果左边的记录条数小于$space(页码区段)的值,页码$start从1开始向右增值。如果左则的记录条数多于$left(左右各显示页数)的值,$start将从左边记录数减去$left值开始记数。
复制代码 代码如下:
<?php
/** 实现同类产品翻页 **/

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);

/** 上一页链接 **/
$c = count($list1);
if($c > 1) {
$url = $this->pageName."-".$list1[$c]['id'].".html";
$pages = "<a href=\"{$url}\">上一页</a><ol>";
}elseif($c == 1) {
$url = $this->pageName."-".$list1[0]['id'].".html";
$pages = "<a href=\"{$url}\">上一页</a><ol>";
}else {
$pages = "";
}


/** 当前页的左边内容 **/
foreach($list1 as $item) {
$url = $this->pageName."-".$item['id'].".html";
$pages .= "<li><a href=\"{$url}\">{$start}</a></li>";
$start++;
}

$pages .= "<li><b>{$leftCount}</b></li>";
$start++;

/** 当前页面右边的内容 **/
foreach($list1 as $item) {
$url = $this->pageName."-".$item['id'].".html";
$pages .= "<li><a href=\"{$url}\">{$start}</a></li>";
$start++;
}

/** 下一页的链接 **/
$c = count($list2);
if($c > 0) {
$url = $this->pageName."-".$list2[0]['id'].".html";
$pages .= "<a href=\"{$url}\">下一页</a><ol>";
}else {
$pages .= "";
}

return $pages;
}
};
?>

声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。

相关文章