zoukankan      html  css  js  c++  java
  • 好用的PHP分页类

    1. <?php  
    2.     class Page {  
    3.           private $total;      //总记录  
    4.           private $pagesize;    //每页显示多少条  
    5.           private $limit;          //limit  
    6.           private $page;           //当前页码  
    7.           private $pagenum;      //总页码  
    8.           private $url;           //地址  
    9.           private $bothnum;      //两边保持数字分页的量  
    10.     
    11.       //构造方法初始化  
    12.       public function __construct($_total$_pagesize) {  
    13.          $this->total = $_total ? $_total : 1;  
    14.          $this->pagesize = $_pagesize;  
    15.           $this->pagenum = ceil($this->total / $this->pagesize);  
    16.          $this->page = $this->setPage();  
    17.          $this->limit = "LIMIT ".($this->page-1)*$this->pagesize.",$this->pagesize";  
    18.          $this->url = $this->setUrl();  
    19.          $this->bothnum = 2;  
    20.       }  
    21.     
    22.       //拦截器  
    23.       private function __get($_key) {  
    24.          return $this->$_key;  
    25.       }  
    26.     
    27.       //获取当前页码  
    28.       private function setPage() {  
    29.          if (!empty($_GET['page'])) {  
    30.                 if ($_GET['page'] > 0) {  
    31.                    if ($_GET['page'] > $this->pagenum) {  
    32.                           return $this->pagenum;  
    33.                    } else {  
    34.                           return $_GET['page'];  
    35.                    }  
    36.                 } else {  
    37.                    return 1;  
    38.                 }  
    39.          } else {  
    40.                 return 1;  
    41.          }  
    42.       }   
    43.     
    44.       //获取地址  
    45.       private function setUrl() {  
    46.          $_url = $_SERVER["REQUEST_URI"];  
    47.          $_par = parse_url($_url);  
    48.          if (isset($_par['query'])) {  
    49.                 parse_str($_par['query'],$_query);  
    50.                 unset($_query['page']);  
    51.                 $_url = $_par['path'].'?'.http_build_query($_query);  
    52.          }  
    53.          return $_url;  
    54.       }     //数字目录  
    55.       private function pageList() {  
    56.          for ($i=$this->bothnum;$i>=1;$i--) {  
    57.             $_page = $this->page-$i;  
    58.             if ($_page < 1) continue;  
    59.                 $_pagelist .= ' <a href="'.$this->url.'&page='.$_page.'">'.$_page.'</a> ';  
    60.          }  
    61.          $_pagelist .= ' <span class="me">'.$this->page.'</span> ';  
    62.          for ($i=1;$i<=$this->bothnum;$i++) {  
    63.             $_page = $this->page+$i;  
    64.                 if ($_page > $this->pagenum) break;  
    65.                 $_pagelist .= ' <a href="'.$this->url.'&page='.$_page.'">'.$_page.'</a> ';  
    66.          }  
    67.          return $_pagelist;  
    68.       }  
    69.     
    70.       //首页  
    71.       private function first() {  
    72.          if ($this->page > $this->bothnum+1) {  
    73.                 return ' <a href="'.$this->url.'">1</a> ...';  
    74.          }  
    75.       }  
    76.     
    77.       //上一页  
    78.       private function prev() {  
    79.          if ($this->page == 1) {  
    80.                 return '<span class="disabled">上一页</span>';  
    81.          }  
    82.          return ' <a href="'.$this->url.'&page='.($this->page-1).'">上一页</a> ';  
    83.       }  
    84.     
    85.       //下一页  
    86.       private function next() {  
    87.          if ($this->page == $this->pagenum) {  
    88.                 return '<span class="disabled">下一页</span>';  
    89.          }  
    90.          return ' <a href="'.$this->url.'&page='.($this->page+1).'">下一页</a> ';  
    91.       }  
    92.     
    93.       //尾页  
    94.       private function last() {  
    95.          if ($this->pagenum - $this->page > $this->bothnum) {  
    96.                 return ' ...<a href="'.$this->url.'&page='.$this->pagenum.'">'.$this->pagenum.'</a> ';  
    97.          }  
    98.       }  
    99.     
    100.       //分页信息  
    101.       public function showpage() {  
    102.          $_page .= $this->first();  
    103.          $_page .= $this->pageList();  
    104.          $_page .= $this->last();  
    105.          $_page .= $this->prev();  
    106.          $_page .= $this->next();  
    107.          return $_page;  
    108.       }  
    109.  }  
    110. ?>  
      使用分页
    1. <?php  
    2.     $_page = new Page($_total,$_pagesize); //其中 $_total 是数据集的总条数,$_pagesize 是每页显示的数量.  
    3. ?>  

  • 相关阅读:
    Windows Azure Storage (17) Azure Storage读取访问地域冗余(Read Access – Geo Redundant Storage, RA-GRS)
    SQL Azure (15) SQL Azure 新的规格
    Azure China (5) 管理Azure China Powershell
    Azure China (4) 管理Azure China Storage Account
    Azure China (3) 使用Visual Studio 2013证书发布Cloud Service至Azure China
    Azure China (2) Azure China管理界面初探
    Azure China (1) Azure公有云落地中国
    SQL Azure (14) 将云端SQL Azure中的数据库备份到本地SQL Server
    [New Portal]Windows Azure Virtual Machine (23) 使用Storage Space,提高Virtual Machine磁盘的IOPS
    Android数据库升级、降级、创建(onCreate() onUpgrade() onDowngrade())的注意点
  • 原文地址:https://www.cnblogs.com/qhorse/p/4594759.html
Copyright © 2011-2022 走看看