- 论坛徽章:
- 0
|
前几天在群里有人提到还不会分页,把自己写的拿出来。谁觉得有用,就收一下吧。个人觉得我写的这个类命名还是很易懂的,写的结构也很容易看懂,不用多注释了吧。跟据方法名就可以知道各个方法都有什么用处了。不过有点不友好的可能是里面逻辑语句,习惯了,如果改为一堆if代码会增加好多。目前输出默认只是类baidu,不过google形式的也很容易实现,只需要对advanDisplay()做一点扩展就可以了,也就是这一句
- echo "<div class='" . $styleClass . "'align='left'>" . $leftString . $this->thisPage . ' ' . $rightString ."</div>";
复制代码
把其中的$this->thisPage换为图片,下面怎么做,不说也知道了。
分页类是最基础的一个应用,也接触PHP一段时间了,想为新学的同学做一点事情,发了这个学习基础知识必会遇见的一个应用。也算对得起从搜索上搜出来的信息了 。好了,别的不说什么了,有什么不明白的,或者有建议请跟帖,我再修改本帖来回答。
- <?php
- /**
- * PuPage!
- * 目前有两种输出方式,其中advanDisplay()方法可以输出和百度一样的效果
- *
- * Advantage : 调用非常简单,可选参数丰富,数据类型控制严格
- * Support : hansanpu(at)gmail.com
- * Version : 0.1.1
- * Copyright : HanSanpu(韩三普)
- */
- class PuPage
- {
- public $thisPage;
- public $perPage;
- public $perGroup;
- public $totalItem;
- private $totalPage;
- public function __construct($thisPage, $totalItem, $perPage = 20, $pergroup = 10) {
- $this->thisPage = $thisPage >= 1 ? intval(ceil($thisPage)) : 1;
- $this->perPage = intval(ceil($perPage));
- $this->totalItem = intval(ceil($totalItem));
- $this->perGroup = intval(ceil($pergroup));
- $this->totalPage = intval(ceil($totalItem/$perPage));
- }
- public function display($apendUrl, $styleClass = '') {
- $leftString = '';
- $this->_checkPre() && $leftString = "<a href='" . $apendUrl . ($this->thisPage - 1) . "'>上一页</a>";
- $rightString = '';
- $this->_checkNext() && $rightString = "<a href='" . $apendUrl . ($this->thisPage + 1) . "'>下一页</a>";
- echo "<div class='" . $styleClass . "'>" . $leftString . $this->thisPage . $rightString . "</div>";
- }
- public function advanDisplay($apendUrl, $styleClass = '') {
- $leftPages = '';
- for ($i = 1; $i <= $this->_leftPageNum(); $i++) {
- $t = $this->thisPage - $this->_leftPageNum() - 1 + $i;
- $leftPages .= "<a href='" . $apendUrl . $t ."'>" . $t . "</a> ";
- }
- $rightPages = '';
- for ($i = 1; $i <= $this->_rightPageNum(); $i++) {
- $t = $i + $this->thisPage;
- $rightPages .= "<a href='" . $apendUrl . $t . "'>" . $t . "</a> ";
- }
- $leftString = '' == $leftPages ? '' : "<a href='" . $apendUrl . ($this->thisPage - 1) . "'>上一页</a> " . $leftPages;
- $rightString = '' == $rightPages ? '' : $rightPages . "<a href='" . $apendUrl . ($this->thisPage + 1) . "'>下一页</a>";
- echo "<div class='" . $styleClass . "'align='left'>" . $leftString . $this->thisPage . ' ' . $rightString ."</div>";
- }
- private function _checkPre() {
- return 1 >= $this->thisPage ? false : true;
- }
- private function _checkNext() {
- return $this->totalPage <= $this->thisPage ? false : true;
- }
- private function _leftPageNum() {
- $max = ($this->perGroup-1);
- $total = $this->thisPage - 1;
- return $total > $max ? $max : $total;
- }
- private function _rightPageNum() {
- $total = $this->totalPage - $this->thisPage;
- $max = ($this->perGroup-1);
- return $total > $max ? $max : $total;
- }
- public function __destruct() {
- unset($this);
- }
- }
- ?>
- <?php
- //example
- $thisPage = isset($_GET['page']) ? trim($_GET['page']) : 1;
- $appendUrl = $_SERVER['PHP_SELF'] . "?page=";
- $pager = new PuPage($thisPage, '500');
- $pager->display($appendUrl);
- $pager->advanDisplay($appendUrl);
- ?>
复制代码 |
|