当前页左边的页码为最新的产品,按更新时间呈升序排列;右边的页码为早期的产品, 按更新时间呈降序排列。如果左边的记录条数小于$space(页码区段)的值,页码$start从1开始向右增值。如果左则的记录条数多于$left(左右各显示页数)的值,$start将从左边记录数减去$left值开始记数。
1 <?php
2 /** 实现同类产品翻页 **/
3
4
5 class pager
6 {
7 protected $space;
8 protected $left;
9 protected $DB;
10 protected $pageName;
11
12 public function setSpace($num) {
13 $this->space = $num;
14 $this->left = ceil(($num-1)/2);
15 }
16
17 public function setDB(&$db) {
18 $this->DB = $db;
19 }
20
21 public function setPageName($pageName) {
22 $this->pageName = $pageName;
23 }
24
25 public function getPages($catid, $exptime) {
26 $fields = array("`id`,`title`");
27 $left = array(">" => array("exptime"=>$exptime), "memberid" => gs(_MEM_PREFIX_ . "memberid"), "catid"=>$catid);
28 $right = array("<" => array("exptime"=>$exptime), "memberid" => gs(_MEM_PREFIX_ . "memberid"), "catid"=>$catid);
29
30 $leftCount = $this->DB->getCount($left);
31
32 if($leftCount <= $this->left) {
33 $star = 1;
34 $leftLimit = "LIMIT" . $leftCount;
35 $rightLimit = "LIMIT " . ($this->space-$leftCount);
36 }
37 else {
38 $start = $leftCount - $this->left;
39 $leftLimit = "LIMIT " . $this->left;
40 $rightLimit = $leftLimit;
41 }
42
43 $list1 = $this->DB->findAll($left, array("exptime"=>"ASC"), $leftLimit, $fields);
44 $list2 = $this->DB->findAll($right, array("exptime"=>"DESC"), $rightLimit, $fields);
45
46 /** 上一页链接 **/
47 $c = count($list1);
48 if($c > 1) {
49 $url = $this->pageName."-".$list1[$c]['id'].".html";
50 $pages = "<a href="{$url}">上一页</a><ol>";
51 }elseif($c == 1) {
52 $url = $this->pageName."-".$list1[0]['id'].".html";
53 $pages = "<a href="{$url}">上一页</a><ol>";
54 }else {
55 $pages = "";
56 }
57
58
59 /** 当前页的左边内容 **/
60 foreach($list1 as $item) {
61 $url = $this->pageName."-".$item['id'].".html";
62 $pages .= "<li><a href="{$url}">{$start}</a></li>";
63 $start++;
64 }
65
66 $pages .= "<li><b>{$leftCount}</b></li>";
67 $start++;
68
69 /** 当前页面右边的内容 **/
70 foreach($list1 as $item) {
71 $url = $this->pageName."-".$item['id'].".html";
72 $pages .= "<li><a href="{$url}">{$start}</a></li>";
73 $start++;
74 }
75
76 /** 下一页的链接 **/
77 $c = count($list2);
78 if($c > 0) {
79 $url = $this->pageName."-".$list2[0]['id'].".html";
80 $pages .= "<a href="{$url}">下一页</a><ol>";
81 }else {
82 $pages .= "";
83 }
84
85 return $pages;
86 }
87 };
88 ?>