php分页类及其实现原理

php分页类及其实现原理
  1 /**
  2  *
  3  * 实现思路:分页显示拆分  :  1...上页 12 13 14 15 [16] 17 18 19 20 ...100 下页
  4  *
  5  * function htmlPart1() : 上页
  6  * function htmlPart2() : 1...
  7  * function htmlPart3() : 12 13 14 15 [16] 17 18 19 20
  8  * function htmlPart4() : ...100
  9  * function htmlPart5() : 下页
 10  *
 11  * @param int $allCount     记录总数目
 12  * @param int $eachPage     每页数目
 13  * @param int $showCount    显示数目
 14  * @param int $thenPage     当前页(页面传值)
 15  * @param string $urlPrefix Url前缀
 16  * @param string $urlSuffix Url后缀
 17  *
 18  * Author : intval@163.com
 19  * Date   : 2013.05.04
 20  *
 21  */
 22 class zPage {
 23 
 24     /** 只可内部调用 */
 25     private $allCount;  /** 总数(非总页数) */
 26     private $eachPage;  /** 每页数(分页数) */
 27     private $showCount; /** 显示数(显示多少页数) */
 28     private $urlPrefix; /** 页码前缀(例如:?page=) */
 29     private $urlSuffix; /** 页码后缀缀(例如:&type=1) */
 30     private $startHide; /** 计算前部需要出现符号(...)的最小值 */
 31     private $endHide;   /** 计算尾部需要出现符号(...)的最小值 */
 32     private $arrTxt = array(‘ ‘, ‘ ‘); // array(‘上页‘, ‘上页‘)
 33 
 34     /** 可外部调用 */
 35     public $allPage;    /** 总页数 */
 36     public $thenPage;   /** 当前页 */
 37 
 38     public function __construct($allCount, $eachPage, $showCount, $thenPage, $urlPrefix = ‘?page=‘, $urlSuffix = ‘‘) {
 39 
 40         $this->allCount = intval($allCount);
 41         $this->eachPage = intval($eachPage);
 42         $this->showCount = intval($showCount);
 43         $this->urlPrefix = trim($urlPrefix);
 44         $this->urlSuffix = trim($urlSuffix);
 45 
 46         /** 计算总页数 */
 47         $this->allPage = ceil($this->allCount / $this->eachPage);
 48 
 49         /** 使当前页的数值合法化 */
 50         $this->thenPage = max(intval($thenPage), 1);
 51         $this->thenPage >= $this->allPage AND $this->thenPage = $this->allPage;
 52 
 53         /** 计算前部和尾部需要出现符号(...)的最小值 */
 54         $this->startHide = ceil($this->showCount / 2);
 55         $this->endHide = $this->allPage - $this->startHide;
 56     }
 57 
 58     public function parseUrl($char = ‘‘) {
 59 
 60         $val = $char;
 61         ($char === ‘prev‘) AND $val = $this->thenPage - 1;
 62         ($char === ‘next‘) AND $val = $this->thenPage + 1;
 63         ($char === ‘‘) AND $val = $this->allPage;
 64         return $this->urlPrefix . $val . $this->urlSuffix;
 65     }
 66 
 67     public function htmlPart1() {
 68 
 69         $html = ‘‘;
 70         $this->thenPage > $this->startHide AND $html = ‘<a class="prev" href="‘ . $this->parseUrl(‘prev‘) . ‘" title="上一页">‘ . $this->arrTxt[0] . ‘</a>‘ . PHP_EOL;
 71         return $html;
 72     }
 73 
 74     public function htmlPart2() {
 75 
 76         $dot = ‘‘;
 77         $this->thenPage > $this->startHide AND $dot = ‘ ...‘;
 78 
 79         $html = ‘<a href="‘ . $this->parseUrl(1) . ‘">1‘. $dot .‘</a>‘ . PHP_EOL;
 80         $this->thenPage == 1 AND $html = ‘<span>1</span>‘ . PHP_EOL;
 81 
 82         return $html;
 83     }
 84 
 85     public function htmlPart3() {
 86 
 87         $html = ‘‘;
 88 
 89         if ($this->thenPage <= $this->startHide) {
 90 
 91             /**
 92              * 第一种情况:[1] 2 3 4 5 6 7 8 9 10 ...100 下页
 93              * 即:当前部在 不需要出现...的 范围之内
 94              */
 95             for ($i = 2, $n = $this->showCount; $i < $n; $i++) {
 96                 $html .= (($i == $this->thenPage) ? ‘<span>‘ . $this->thenPage . ‘</span>‘ : ‘<a href="‘ . $this->parseUrl($i) . ‘">‘ . $i . ‘</a>‘) . PHP_EOL;
 97             }
 98 
 99         } elseif ($this->thenPage >= $this->endHide) {
100 
101             /**
102              * 第二种情况:上页 1..92 93 94 95 96 97 98 [99] 100
103              * 即:当尾部在 不需要出现...的 范围之内
104              */
105             $len = $this->showCount - 2;
106             $i = $this->allPage - $len;
107             $n = $this->allPage;
108             for ($i; $i < $n; $i++) {
109                 $html .= (($i == $this->thenPage) ? ‘<span>‘ . $this->thenPage . ‘</span>‘ : ‘<a href="‘ . $this->parseUrl($i) . ‘">‘ . $i . ‘</a>‘) . PHP_EOL;
110             }
111 
112         } else {
113 
114             /**
115              * 第三种情况:上页 1..12 13 14 15 [16] 17 18 19 20 ...100 下页
116              * 即:当前后都在 需要出现...的 范围之内
117              */
118             $len = $this->showCount - 2;    // 此处减去2,是说明头尾各占去一个数字(1, x)
119             $offset = ceil($len / 2) - 1;   // 对剩下的数目平分,得出平分数
120             $i = $this->thenPage - $offset; // 循环开始:当前页向前偏移平分数
121             $n = $this->thenPage + $offset; // 循环结束:当前页向后偏移平分数
122             for ($i; $i <= $n; $i++) {
123                 $html .= (($i == $this->thenPage) ? ‘<span>‘ . $this->thenPage . ‘</span>‘ : ‘<a href="‘ . $this->parseUrl($i) . ‘">‘ . $i . ‘</a>‘) . PHP_EOL;
124             }
125         }
126 
127         return $html;
128     }
129 
130     public function htmlPart4() {
131 
132         $dot = ‘‘;
133         $this->thenPage < $this->endHide AND $dot = ‘... ‘;
134 
135         $html = ‘<a href="‘ . $this->parseUrl() . ‘">‘ . $dot . $this->allPage . ‘</a>‘ . PHP_EOL;
136         $this->thenPage == $this->allPage AND $html = ‘<span>‘ . $this->allPage . ‘</span>‘ . PHP_EOL;
137 
138         return $html;
139     }
140 
141     public function htmlPart5() {
142 
143         $html = ‘‘;
144         $this->thenPage < $this->endHide AND $html = ‘<a class="next" href="‘ . $this->parseUrl(‘next‘) . ‘" title="下一页">‘ . $this->arrTxt[1] . ‘</a>‘ . PHP_EOL;
145         return $html;
146     }
147 
148     public function html() {
149 
150         $pageHtml = ‘‘;
151 
152         /** 总页数未达到显示页码数,则全部显示 */
153         if ($this->allPage <= $this->showCount) {
154             for ($i = 1; $i <= $this->allPage; $i++) {
155                 $pageHtml .= ($i == $this->thenPage) ? ‘<span>‘ . $this->thenPage . ‘</span>‘ . PHP_EOL : ‘<a href="‘ . $this->parseUrl($i) . ‘">‘ . $i . ‘</a>‘ . PHP_EOL;
156             }
157         } else {
158             $pageHtml = $this->htmlPart2() . $this->htmlPart1() . $this->htmlPart3() . $this->htmlPart4() . $this->htmlPart5();
159         }
160 
161         return $pageHtml;
162     }
163 }
164 
165 // 调用例子
166 $getPage = isset($_GET[‘page‘]) ? intval($_GET[‘page‘]) : 0;
167 $getPage = max($getPage, 1);
168 $zPage = new zPage(1100, 10, 11, $getPage, ‘?page=‘, ‘&type=1‘);
169 echo $zPage->html();
php分页类及其实现原理

php分页类及其实现原理,布布扣,bubuko.com

php分页类及其实现原理

上一篇:String存储地址


下一篇:Extjs学习笔记--(二)