PHP 分页 (分页类)

分页类  以后可直接使用

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

执行分页类  

 1 <table width="100%" border="1" cellpadding="0" cellspacing="0">
 2     <tr>
 3          <td>AreaCode</td>
 4         <td>AreaName</td>
 5         <td>ParentAreaCode</td>
 6         <td>Root</td>
 7         <td>Zone</td>
 8     </tr>
 9 <?php
10 include("../dbda.class.php");
11 include("../page.class.php");
12 $db= new dbda();
13 $sqlall = "select count(*) from chinastates";
14 $attrall = $db->Query($sqlall);
15 $total = $attrall[0][0];
16 $page = new Page($total,20);
17 $limit = $page->limit;
18 $sql = "select * from chinastates ".$limit;
19 $attr = $db->Query($sql);
20 foreach($attr as $v)
21 {
22     echo "<tr>
23         <td>{$v[0]}</td>
24         <td>{$v[1]}</td>
25         <td>{$v[2]}</td>
26         <td>{$v[3]}</td>
27         <td>{$v[4]}</td>
28     </tr>";
29 }
30 ?>
31 </table>
32 <?php
33     echo "<div>".$page->fpage()."</div>";
34 ?>

 

posted @ 2016-05-10 16:29  冷风~云  阅读(1423)  评论(0编辑  收藏  举报