php实现google样式的分页

 废话不多说,直接看代码吧

 

Pager.class.php 代码如下

  1. class Pager {
  2.     /**
  3.      *int 总页数
  4.      * */
  5.     protected $pageTotal;
  6.     /**
  7.      *int 上一页
  8.      * */
  9.     protected $previous;
  10.     /**
  11.      *int 下一页
  12.      * */
  13.     protected $next;
  14.     /**
  15.      *int 中间页起始序号
  16.      * */
  17.     protected $startPage;
  18.     /**
  19.      *int 中间页终止序号
  20.      * */
  21.     protected $endPage;
  22.     /**
  23.      *int 记录总数
  24.      * */
  25.     protected $recorbTotal;
  26.     /**
  27.      *int 每页显示记录数
  28.      * */
  29.     protected $pageSize;
  30.     /**
  31.      *int 当前显示页
  32.      * */
  33.     protected $currentPage;
  34.     /**
  35.      *string 基url地址
  36.      * */
  37.     protected $baseUri;
  38.     
  39.     /**
  40.      * @return string 获取基url地址
  41.      */
  42.     public function getBaseUri() {
  43.         return $this->baseUri;
  44.     }
  45.     
  46.     /**
  47.      * @return int 获取当前显示页
  48.      */
  49.     public function getCurrentPage() {
  50.         return $this->currentPage;
  51.     }
  52.     
  53.     /**
  54.      * @return int 获取每页显示记录数
  55.      */
  56.     public function getPageSize() {
  57.         return $this->pageSize;
  58.     }
  59.     
  60.     /**
  61.      * @return int 获取记录总数
  62.      */
  63.     public function getRecorbTotal() {
  64.         return $this->recorbTotal;
  65.     }
  66.     
  67.     /**
  68.      * @param string $baseUri 设置基url地址
  69.      */
  70.     public function setBaseUri($baseUri) {
  71.         $this->baseUri = $baseUri;
  72.     }
  73.     
  74.     /**
  75.      * @param int $currentPage 设置当前显示页
  76.      */
  77.     public function setCurrentPage($currentPage) {
  78.         $this->currentPage=$currentPage;
  79.     }
  80.     
  81.     /**
  82.      * @param int $pageSize 设置每页显示记录数
  83.      */
  84.     public function setPageSize($pageSize) {
  85.         $this->pageSize = $pageSize;
  86.     }
  87.     
  88.     /**
  89.      * @param int $recorbTotal 设置获取记录总数
  90.      */
  91.     public function setRecorbTotal($recorbTotal) {
  92.         $this->recorbTotal = $recorbTotal;
  93.     }
  94.     
  95.     /**
  96.      *构造函数
  97.      * */
  98.     public function __construct()
  99.     {
  100.         $this->pageTotal = 0;
  101.         $this->previous = 0;
  102.         $this->next = 0;
  103.         $this->startPage = 0;
  104.         $this->endPage = 0;
  105.         
  106.         $this->pageSize = 20;
  107.         $this->currentPage = 0;
  108.     }
  109.     
  110.     /**
  111.      *分页算法
  112.      * */
  113.     private function arithmetic() {
  114.         if ($this->currentPage < 1)
  115.             $this->currentPage = 1;
  116.         
  117.         $this->pageTotal = floor ( $this->recorbTotal / $this->pageSize ) + ($this->recorbTotal % $this->pageSize > 0 ? 1 : 0);
  118.         
  119.         if ($this->currentPage > 1 && $this->currentPage > $this->pageTotal)
  120.             header ( 'location:' . $this->baseUri . 'page=' . $this->pageTotal );
  121.         
  122.         $this->next = $this->currentPage + 1;
  123.         $this->previous = $this->currentPage - 1;
  124.         
  125.         $this->startPage = ($this->currentPage + 5) > $this->pageTotal ? $this->pageTotal - 10 : $this->currentPage - 5;
  126.         $this->endPage = $this->currentPage < 5 ? 11 : $this->currentPage + 5;
  127.         
  128.         if ($this->startPage < 1)
  129.             $this->startPage = 1;
  130.         
  131.         if ($this->pageTotal < $this->endPage)
  132.             $this->endPage = $this->pageTotal;
  133.     }
  134.     
  135.     /**
  136.      *分页样式
  137.      * */
  138.     protected function pageStyle() {
  139.         $result = "共" . $this->pageTotal . "页      ";
  140.         
  141.         if ($this->currentPage > 1)
  142.             $result .= "<a href=/"" . $this->baseUri . "page=1/"><font style=/"font-family:webdings/">9</font></a>  <a href=/"" . $this->baseUri . "page=$this->previous/"><font style=/"font-family:webdings/">3</font></a>";
  143.         else
  144.             $result .= "<font style=/"font-family:webdings/">9</font> <font style=/"font-family:webdings/">3</font>";
  145.         
  146.         for($i = $this->startPage; $i <= $this->endPage; $i ++) {
  147.             if ($this->currentPage == $i)
  148.                 $result .= "  <font color=/"#ff0000/">$i</font>";
  149.             else
  150.                 $result .= "  <a href=/"" . $this->baseUri . "page=$i/">$i</a>";
  151.         }
  152.         
  153.         if ($this->currentPage != $this->pageTotal) {
  154.             $result .= "  <a href=/"" . $this->baseUri . "page=$this->next/"><font style=/"font-family:webdings/">4</font></a>";
  155.             $result .= "  <a href=/"" . $this->baseUri . "page=$this->pageTotal/"><font style=/"font-family:webdings/">:</font></a>";
  156.         } else {
  157.             $result .= " <font style=/"font-family:webdings/">4</font> <font style=/"font-family:webdings/">:</font>";
  158.         }
  159.         return $result;
  160.     }
  161.     
  162.     /**
  163.      *执行分页
  164.      * */
  165.     public function execute() {
  166.         if ($this->baseUri != "" && $this->recorbTotal == 0)
  167.             return "";
  168.         $this->arithmetic();
  169.         return $this->pageStyle ();
  170.     }
  171. }

调用代码(test.php 代码如下)

  1. include_once 'Pager.class.php';
  2. $pager=new Pager();
  3. if (isset ( $_GET ['page'] ) && ! emptyempty ( $_GET ['page'] ))
  4.     $pager->setCurrentPage($_GET ['page']);
  5. else
  6.     $pager->setCurrentPage(1);
  7.     
  8. $pager->setRecorbTotal(1000);
  9. $pager->setBaseUri("test.php?");
  10. echo $pager->execute();

 

数据库结合 mysql 通用存储过程分页 海量数据分页  就是一个完美的分页了

 

我们还可继承 Pager 类重写pageStyle方法就可以有不同的样式了. yes ok

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值