免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1900 | 回复: 1
打印 上一主题 下一主题

[代码]与google和baidu类似的分页类 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2006-05-23 00:01 |只看该作者 |倒序浏览
  前几天在群里有人提到还不会分页,把自己写的拿出来。谁觉得有用,就收一下吧。个人觉得我写的这个类命名还是很易懂的,写的结构也很容易看懂,不用多注释了吧。跟据方法名就可以知道各个方法都有什么用处了。不过有点不友好的可能是里面逻辑语句,习惯了,如果改为一堆if代码会增加好多。目前输出默认只是类baidu,不过google形式的也很容易实现,只需要对advanDisplay()做一点扩展就可以了,也就是这一句        
  1. echo "<div class='" . $styleClass . "'align='left'>" . $leftString . $this->thisPage . '&nbsp;' . $rightString ."</div>";
复制代码

  把其中的$this->thisPage换为图片,下面怎么做,不说也知道了。

  分页类是最基础的一个应用,也接触PHP一段时间了,想为新学的同学做一点事情,发了这个学习基础知识必会遇见的一个应用。也算对得起从搜索上搜出来的信息了。好了,别的不说什么了,有什么不明白的,或者有建议请跟帖,我再修改本帖来回答。


  1. <?php
  2. /**
  3. * PuPage!
  4. * 目前有两种输出方式,其中advanDisplay()方法可以输出和百度一样的效果
  5. *
  6. * Advantage        : 调用非常简单,可选参数丰富,数据类型控制严格
  7. * Support          : hansanpu(at)gmail.com
  8. * Version          : 0.1.1
  9. * Copyright        : HanSanpu(韩三普)
  10. */
  11. class PuPage
  12. {
  13.     public $thisPage;
  14.     public $perPage;
  15.     public $perGroup;
  16.     public $totalItem;
  17.     private $totalPage;
  18.     public function __construct($thisPage, $totalItem, $perPage = 20, $pergroup = 10) {
  19.         $this->thisPage = $thisPage >= 1 ? intval(ceil($thisPage)) : 1;
  20.         $this->perPage = intval(ceil($perPage));
  21.         $this->totalItem = intval(ceil($totalItem));
  22.         $this->perGroup = intval(ceil($pergroup));
  23.         $this->totalPage = intval(ceil($totalItem/$perPage));
  24.     }
  25.     public function display($apendUrl, $styleClass = '') {
  26.         $leftString = '';
  27.         $this->_checkPre() && $leftString = "<a href='" . $apendUrl . ($this->thisPage - 1) . "'>上一页</a>";
  28.         $rightString = '';
  29.         $this->_checkNext() && $rightString = "<a href='" . $apendUrl . ($this->thisPage + 1) . "'>下一页</a>";
  30.         echo "<div class='" . $styleClass . "'>" . $leftString . $this->thisPage . $rightString . "</div>";
  31.     }
  32.     public function advanDisplay($apendUrl, $styleClass = '') {
  33.             $leftPages = '';
  34.         for ($i = 1; $i <= $this->_leftPageNum(); $i++) {
  35.             $t = $this->thisPage - $this->_leftPageNum() - 1 + $i;
  36.             $leftPages .= "<a href='" . $apendUrl . $t ."'>" . $t . "</a>&nbsp;";
  37.         }
  38.         $rightPages = '';
  39.         for ($i = 1; $i <= $this->_rightPageNum(); $i++) {
  40.             $t = $i + $this->thisPage;
  41.             $rightPages .= "<a href='" . $apendUrl . $t . "'>" . $t . "</a>&nbsp;";
  42.         }
  43.         $leftString = '' == $leftPages ? '' : "<a href='" . $apendUrl . ($this->thisPage - 1) . "'>上一页</a>&nbsp;" . $leftPages;
  44.         $rightString = '' == $rightPages ? '' : $rightPages . "<a href='" . $apendUrl . ($this->thisPage + 1) . "'>下一页</a>";
  45.         echo "<div class='" . $styleClass . "'align='left'>" . $leftString . $this->thisPage . '&nbsp;' . $rightString ."</div>";
  46.     }
  47.     private function _checkPre() {
  48.         return 1 >= $this->thisPage ? false : true;
  49.     }
  50.     private function _checkNext() {
  51.         return $this->totalPage <= $this->thisPage ? false : true;
  52.     }
  53.     private function _leftPageNum() {
  54.         $max = ($this->perGroup-1);
  55.         $total = $this->thisPage - 1;
  56.         return $total > $max ? $max : $total;
  57.     }
  58.     private function _rightPageNum() {
  59.         $total = $this->totalPage - $this->thisPage;
  60.         $max = ($this->perGroup-1);
  61.         return $total > $max ? $max : $total;
  62.     }
  63.     public function __destruct() {
  64.         unset($this);
  65.     }
  66. }
  67. ?>
  68. <?php
  69. //example
  70. $thisPage = isset($_GET['page']) ? trim($_GET['page']) : 1;
  71. $appendUrl = $_SERVER['PHP_SELF'] . "?page=";
  72. $pager = new PuPage($thisPage, '500');
  73. $pager->display($appendUrl);
  74. $pager->advanDisplay($appendUrl);
  75. ?>
复制代码

论坛徽章:
0
2 [报告]
发表于 2006-05-23 03:51 |只看该作者
好东东,顶~
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP