兼容url路由的php分页类

jerry thinkphp 2015年11月18日 收藏
该分页类兼容url路由的php分页类
该类有个不足的地方,那就是post传的参数无法获取,如果有哪位大神解决了这个问题还希望给小弟分享一下!该文章同时发布在自己的博客上(http://wanql.sinaapp.com/blog_21.html),希望大家帮忙顶下 ^^
上代码!!!
  1. class Page{
  2.     private $total;                         //数据表中总记录数
  3.     private $listRows;                      //每页显示的条数
  4.     private $limit;                         //SQL语句中使用limit从句,限制获取记录数
  5.     private $uri;                           //自动获取url的请求地址
  6.     private $pageNum;                       //总页数
  7.     private $page;                          //当前页
  8.     private $config = array(
  9.                 'head' => '条记录',
  10.                 'prev' => '上一页',
  11.                 'next' => '下一页',
  12.                 'first' => '首页',
  13.                 'last' => '末页'
  14.             );              //在分页信息中显示内容,可以自己通过set()方法设置
  15.     private $listNum = 5;               //默认分页列表显示的个数
  16.      
  17.     /**
  18.      * 构造方法,可以设置分页类的属性
  19.      * @param int $total              计算分页的总记录数
  20.      * @param int $listRows      可选的,设置每页显示的记录数,默认25
  21.      * @param mixed $query    可选的,为向目标页面传递参数,可以是数组,也可以是查询字符串格式
  22.      * @param bool $ord             默认是true,页面从第一页开始显示,false则从最后一页开始显示
  23.      */
  24.     public function __construct($total, $listRows=25, $query='', $ord=true){
  25.         $this->total = $total;
  26.         $this->listRows = $listRows;
  27.         $this->uri = $this->getUri($query);
  28.         $this->pageNum = ceil($this->total/$this->listRows);
  29.          
  30.         /*以下判断用来设置当前页*/
  31.         if(!empty($_GET['page'])){
  32.             $page = $_GET['page'];
  33.         }else{
  34.             if($ord){
  35.                 $page = 1;
  36.             }else{
  37.                 $page = $this->pageNum;
  38.             }
  39.         }
  40.          
  41.         if($total > 0){
  42.             if(preg_match('/\D/', $page)){
  43.                 $this->page = 1;
  44.             }else{
  45.                 $this->page = $page;
  46.             }
  47.         }
  48.          
  49.         //$this->limit = 'LIMIT '.$this->setLimit();
  50.         $this->limit = $this->setLimit();
  51.     }
  52.      
  53.     /**
  54.      * 用于设置显示分页的信息,可以进行连贯操作
  55.      * @param string $param   成员属性数组config的下表
  56.      * @param string $value     用于设置config下标对应的元素值
  57.      * @return object                   返回本对象自己$this,用于连贯操作
  58.      */
  59.     function __set($param, $value){
  60.         if(array_key_exists($param, $this->config)){
  61.             $this->config[$param] = $value;
  62.         }
  63.         //return $this;
  64.     }
  65.      
  66.     /*可以使用在对象外部,直接获取私有成员属性limit和page的值*/
  67.     function __get($args){
  68.         if($args == 'limit' || $args == 'page'){
  69.             return $this->$args;
  70.         }else{
  71.             return null;
  72.         }
  73.     }
  74.      
  75.     /**
  76.      * 按指定的格式输出分页
  77.      * @param int 0-7的数字分别作为参数,用于自定义输出分页结构和调整结构的顺序,默认输出全部结构
  78.      * @return string 分页信息内容
  79.      */
  80.     function fpage(){
  81.         $arr = func_get_args();
  82.          
  83.         $html[0] = " 共<b> {$this->total} </b>{$this->config['head']} ";
  84.         $html[1] = " 本页<b>".$this->disnum()."</b>条 ";
  85.         $html[2] = " 本页从<b>{$this->start()} - {$this->end()}</b>条 ";
  86.         $html[3] = " <b>{$this->page}/{$this->pageNum}</b>页 ";
  87.         $html[4] = $this->firstprev();
  88.         $html[5] = $this->pageList();
  89.         $html[6] = $this->nextlast();
  90.         $html[7] = $this->goPage();
  91.          
  92.         $fpage = '<div style="font:12px \'\5B8B\4F53\',san-serif;">';
  93.          
  94.         if(count($arr)<1){
  95.             $arr = array(0,1,2,3,4,5,6,7);
  96.         }
  97.          
  98.         for($i=0; $i<count($arr); $i++){
  99.             $fpage .= $html[$arr[$i]];
  100.         }
  101.          
  102.         $fpage .= '</div>';
  103.         return $fpage;
  104.     }
  105.      
  106.      
  107.      
  108.     /*在对象内部使用,用于自动获取访问的当前url*/
  109.     private function getUri($query){
  110.         $request_uri = $_SERVER['REQUEST_URI'];
  111.         $url = strstr($request_uri, '?') ? $request_uri : $request_uri.'?';
  112.          
  113.         if(is_array($query)){
  114.             $url .= http_build_query($query);
  115.         }else if($query != ''){
  116.             $url .= '&'.trim($query, '?&');
  117.         }
  118.          
  119.         $arr = parse_url($url);
  120.          
  121.         if(isset($arr['query'])){
  122.             parse_str($arr['query'], $arrs);
  123.             unset($arrs['page']);
  124.             $url = $arr['path'].'?'.http_build_query($arrs);
  125.         }
  126.          
  127.         if(strstr($url, '?')){
  128.             if(substr($url, -1) != '?'){
  129.                 $url = $url.'&';
  130.             }
  131.         }else{
  132.             $url .= '?';
  133.         }
  134.         return $url;
  135.     }
  136.      
  137.     /*私有方法,设置limit*/
  138.     private function setLimit(){
  139.         if($this->page > 0){
  140.             return ($this->page - 1)*$this->listRows.", {$this->listRows}";
  141.         }else{
  142.             return 0;
  143.         }
  144.     }
  145.      
  146.     /*在对象内部使用的私有方法,用于获取当前页开始的记录数*/
  147.     private function start(){
  148.         if($this->total == 0){
  149.             return 0;
  150.         }else{
  151.             return ($this->page-1) * $this->listRows + 1;
  152.         }
  153.     }
  154.      
  155.     /*用于获取当前页结束的记录数*/
  156.     private function end(){
  157.         return min($this->page * $this->listRows, $this->total);
  158.     }
  159.      
  160.     /*用于获取本页显示的记录条数*/
  161.     private function disnum(){
  162.         if($this->total > 0){
  163.             return $this->end() - $this->start() + 1;
  164.         }else{
  165.             return 0;
  166.         }
  167.     }
  168.      
  169.     /*用于获取上一页和首页的操作信息*/
  170.     private function firstprev(){
  171.         if($this->page > 1){
  172.             $str = " <a href='{$this->uri}page=1'>{$this->config['first']}</a> ";
  173.             $str .= "<a href='{$this->uri}page=".($this->page-1)."'>{$this->config['prev']}</a> ";
  174.             return $str;
  175.         }
  176.     }
  177.      
  178.     private function pageList(){
  179.         $linkPage = ' <b>';
  180.         $pageSub = $this->page%$this->listNum;
  181.         if($pageSub==0 && $this->page>0){
  182.             $pageSub = $this->listNum;
  183.         }
  184.         /*当前页面前面的列表*/
  185.         for($i=$pageSub-1; $i >= 1; $i--){
  186.             $page = $this->page-$i;
  187.             if($page >= 1){
  188.                 $linkPage .= "<a href='{$this->uri}page={$page}'>{$page}</a> ";
  189.             }
  190.         }
  191.          
  192.         /*当前页的信息*/
  193.         if($this->pageNum > 1){
  194.             $linkPage .= "<span style='padding:1px 2px;background:#BBB;color:white;'>{$this->page}</span> ";
  195.         }
  196.          
  197.         /*当前页后面的列表*/
  198.         for($i=1; $i<=$this->listNum-$pageSub; $i++){
  199.             $page = $this->page + $i;
  200.             if($page<=$this->pageNum){
  201.                 $linkPage .= "<a href='{$this->uri}page={$page}'>{$page}</a> ";
  202.             }else{
  203.                 break;
  204.             }
  205.         }
  206.          
  207.         $linkPage .= '</b>';
  208.         return $linkPage;
  209.     }
  210.      
  211.     /*用于获取页数列表信息*/
  212.     private function pageListBak(){
  213.         $linkPage = ' <b>';
  214.          
  215.         $inum = floor($this->listNum/2);
  216.         /*当前页面前面的列表*/
  217.         for($i=$inum; $i >= 1; $i--){
  218.             $page = $this->page-$i;
  219.             if($page >= 1){
  220.                 $linkPage .= "<a href='{$this->uri}page={$page}'>{$page}</a> ";
  221.             }
  222.         }
  223.          
  224.         /*当前页的信息*/
  225.         if($this->pageNum > 1){
  226.             $linkPage .= "<span style='padding:1px 2px;background:#BBB;color:white;'>{$this->page}</span> ";
  227.         }
  228.          
  229.         /*当前页后面的列表*/
  230.         for($i=1; $i<=$inum; $i++){
  231.             $page = $this->page + $i;
  232.             if($page<=$this->pageNum){
  233.                 $linkPage .= "<a href='{$this->uri}page={$page}'>{$page}</a> ";
  234.             }else{
  235.                 break;
  236.             }
  237.         }
  238.          
  239.         $linkPage .= '</b>';
  240.         return $linkPage;
  241.     }
  242.      
  243.     /*获取下一页和尾页的操作信息*/
  244.     private function nextlast(){
  245.         if($this->page != $this->pageNum){
  246.             $str = " <a href='{$this->uri}page=".($this->page+1)."'>{$this->config['next']}</a> ";
  247.             $str .= " <a href='{$this->uri}page={$this->pageNum}'>{$this->config['last']}</a> ";
  248.             return $str;
  249.         }
  250.     }
  251.      
  252.     /*用于显示和处理表单跳转页面*/
  253.     private function goPage(){
  254.         if($this->pageNum > 1){
  255.             return ' <input style="width:20px;height:17px !important;height:18px;border:1px solid #cccccc" type="text" onkeydown="javascript:if(event.keyCode==13){var page=(this.value>'.$this->pageNum.')?'.$this->pageNum.':this.value;location=\''.$this->uri.'page=\'+page+\'\'}" value="'.$this->page.'" /><input style="cursor:pointer;background:#cccccc;width:25px;height:17px;border:1px solid #cccccc;" tyle="button" value="GO" onclick="javascript:var page=(this.previousSibling.value>'.$this->pageNum.')?'.$this->pageNum.':this.previousSibling.value;location=\''.$this->uri.'page=\'+page+\'\'" /> ';
  256.         }
  257.     }
  258. }