欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2022-06-06 22:25:03
如果左边的记录条数小于$space(页码区段)的值,页码$start从1开始向右增值。如果左则的记录条数多于$left(左右各显示页数)的值,$start将从左边记录数减去...
如果左边的记录条数小于$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;
}
};
?>