超级分页类 php 智能分页

使用实例:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
    <meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
    <title>Document</title>

</head>
<body>
    <?php
    include ‘page.class.php‘;
    $page = isset($_GET[‘page‘]) ? $_GET[‘page‘] : 1;
    echo    page::_app()
            ->setPageSize(12)
            ->setNumber(3)
            ->setDataTotal(123)
            ->setPage($page)
            ->showPaging();
    ?>
</body>
</html>

使用效果:

 

 

分页类源码:

  1 <?php
  2     /**    
  3     * author :wenzl
  4     * date:20140126
  5     * DEMO10
  6     * 分页中心方法DEMO
  7     * 功能:智能分页
  8     * $page 当前页码
  9     * $pageSize 每页数据条数
 10     * $dataTotal 数据总数
 11     * | $number 设置可视分页项数
 12     * 不依赖任何层级关系
 13     * | [1][2][3][4][5][6]
 14     * 使用实例,默认只有一个样式实例
 15     *    include ‘page.class.php‘;
 16     *    $page = isset($_GET[‘page‘]) ? $_GET[‘page‘] : 1;
 17     *    echo    page::_app()
 18     *            ->setPageSize(12)
 19     *            ->setNumber(3)
 20     *            ->setDataTotal(123)
 21     *            ->setPage($page)
 22     *            ->showPaging();
 23     */
 24 
 25     class page{
 26         private static $pageObj = null;
 27         private $page         = 1 ;
 28         private $pageSize     = 5 ;
 29         private $dataTotal     = 100 ;
 30         private $number     = 5 ;
 31         public static function _app(){
 32             is_null(self::$pageObj) && 
 33             self::$pageObj = new page();
 34             return self::$pageObj;
 35         }
 36 
 37 
 38         public function setPage($page){
 39             if(isset($page) && !is_numeric($page)) return self::$pageObj;
 40             self::$pageObj->page = $page ;
 41             return self::$pageObj;
 42         }
 43 
 44         public function setPageSize($pageSize){
 45             if(isset($pageSize) && !is_numeric($pageSize)) return self::$pageObj;
 46             self::$pageObj->pageSize = $pageSize ;
 47             return self::$pageObj;
 48         }
 49 
 50         public function setDataTotal($dataTotal){
 51             if(isset($dataTotal) && !is_numeric($dataTotal)) return self::$pageObj;
 52             self::$pageObj->dataTotal = $dataTotal ;
 53             return self::$pageObj;
 54         }
 55 
 56         public function setNumber($number){
 57             if(isset($number) && !is_numeric($number)) return self::$pageObj;
 58             self::$pageObj->number = $number ;
 59             return self::$pageObj;
 60         }
 61 
 62 
 63         public function paging(){
 64             $page = self::$pageObj->page ? 
 65             self::$pageObj->page : $page ;
 66 
 67             $pageSize = self::$pageObj->pageSize ? 
 68             self::$pageObj->pageSize : $pageSize ;
 69 
 70             $dataTotal = self::$pageObj->dataTotal ? 
 71             self::$pageObj->dataTotal : $dataTotal ;
 72 
 73             $number = self::$pageObj->number ? 
 74             self::$pageObj->number : $number ;
 75 
 76             $pageCnt = ceil($dataTotal/$pageSize);//总页数
 77             $groupCnt = ceil($pageCnt/$number);
 78             $k=1;    
 79             $pageTemp = array();
 80             for($i=1;$i<=$groupCnt;$i++){
 81                 if($i==1){
 82                     for ($j=1; $j<=$number+1; $j++) {
 83                         $k<=$pageCnt &&
 84                         $pageTemp[$i][$j] = $k++;
 85                     }
 86                     $k--;
 87                 }else{
 88                     for ($j=1; $j<=$number+2; $j++) {
 89                         $k<=$pageCnt &&
 90                         $pageTemp[$i][$j] = $k++;
 91                     }
 92                     $k--;
 93                 }
 94                 $k--;
 95             }
 96             $page = $page <= 1 ? 1 : $page;
 97             $bs = floor($page/$number);
 98             $ys = $page%$number>=1?1:0;
 99             $step = $bs + $ys;
100             $step = $ys == 0 ? $bs : $step;
101             
102             $prevPage = isset($page) && 
103             $page <= 1 ? 1 : $page - 1 ;
104             
105             $nextPage = isset($page) && 
106             $page >= $pageCnt ? $pageCnt : $page + 1;
107             
108             return array(
109                     ‘pageTemp‘=>$pageTemp[$step],‘page‘=>$page,‘number‘=>$number,
110                     ‘pageCnt‘=>$pageCnt,‘prevPage‘=>$prevPage,‘pageSize‘=>$pageSize,
111                     ‘nextPage‘=>$nextPage,‘groupCnt‘=>$groupCnt,‘dataTotal‘=>$dataTotal,
112                 );
113         }
114         
115         public function showPaging(){
116             $style="
117                 <style>
118                 .page {font-size:12px;}
119                 .page ul{list-style: none;}
120                 .page ul li a{text-decoration: none;}
121                 .page ul li{display: inline-block;}
122                 .curPage{border:1px solid #C80000;width: auto;padding-right:5px;
123                     padding-left:5px;font-size: 14px;background: #C80000;
124                     color: #FFFFFF;cursor: pointer;text-align: center;}
125                 .otherPage{border:1px solid #C80000;width: auto;
126                     padding-left:5px;font-size: 14px;padding-right: 5px;
127                     color: #C80000;cursor: pointer;text-align: center;}
128                 .othorCase{color:#C80000;}
129                 </style>
130             ";
131             $pageArray = self::$pageObj->paging();
132             
133             $pageHtml.= ‘    <div class="page">‘;
134             $pageHtml.= ‘        <ul>‘;
135             $pageHtml.= ‘            <li><a href="?page=1" class="othorCase">首页</a></li>‘;
136             $pageHtml.= ‘            <li><a href="?page=‘.$pageArray[‘prevPage‘].‘" class="othorCase">上一页</a></li>‘;
137             foreach ($pageArray[‘pageTemp‘] as $key => $p) {
138                 $pageHtml.= ‘            <li>‘;
139                 if(self::$pageObj->page != $p):
140                     $pageHtml.= ‘                <a href="?page=‘.$p.‘" class="otherPage">‘.$p.‘</a>‘;
141                 else:
142                     $pageHtml.= ‘                <a href="?page=‘.$p.‘" class="curPage">‘.$p.‘</a>‘;
143                 endif;
144                 $pageHtml.= ‘            </li>‘;
145             }
146             $pageHtml.= ‘            <li><a href="?page=‘.$pageArray[‘nextPage‘].‘" class="othorCase">下一页</a></li>‘;
147             $pageHtml.= ‘            <li><a href="?page=‘.$pageArray[‘pageCnt‘].‘" class="othorCase">尾页</a></li>‘;
148             $pageHtml.= ‘        </ul>‘;
149             $pageHtml.= ‘    </div>‘;
150             return $style.$pageHtml;
151         }
152 
153 
154         /**
155         * 功能:防止克隆        
156         */
157         public function __clone(){
158             die("can‘t clone!".E_USER_ERROR);
159         }
160 
161 
162 
163 
164     }
165 ?>
author:wenzl

 

 

 

 

何止是吸引

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。