程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 網頁編程 >> PHP編程 >> PHP綜合 >> php實現的仿阿裡巴巴實現同類產品翻頁

php實現的仿阿裡巴巴實現同類產品翻頁

編輯:PHP綜合
如果左邊的記錄條數小於$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;
}
};
?>
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved