完美分页类设计--细说PHP

 完美分页类page.class.php,敲完了代码,测试的时候发现一片空白完全没东西输出来,可是代码又没有显示报错,最后只能照着源码一行行的对,发现原来少了个“.”,但是这个 . 是被单引号
括起来,被单纯的当做了字符串来看待,所以没有报错。整整用了2个多小时去核对整理自己思路,看代码。因此,细心对于刚开始学PHP参考着源码来敲代码很重要。
当我把 . 补齐了,可是效果并不是如下。
技术分享
而是:技术分享完全没有分页结构的信息。
我显示反复对了好多次分页结构的方法fpage()的代码,都没有感觉都没有问题。当时我代码第113行是没有的,而是放到了第105行的位置。这个地方的源码是我改过的,因为之前看过一篇文章
对于for($i=0; $i<count($arr); $++),每循环一次都会对count()函数进行一次计算,这样当处理的数据较多时会效率低下。count()函数返回的是一个整型的数,所以我就先处理了函数
再直接用得到的值进行操作。但是在这里我忽略了一个问题,我尝试输出$conNum=count($arr),得到结果为0,详见:代码105行。

测试代码:

 1 <?php
 2     header("Content-type: text/html; charset=utf-8");
 3     /*第一步:包含分页类所在文件page.class.php*/
 4     include "page.class.php";
 5     /*第二步:实例化分页对象*/
 6     $page = new Page(100);
 7 
 8     /*第三部:通过对象中的limit属性,获取LIMIT从句并组合SQL语句,从数据表artical中获取当页的数据*/
 9     $sql =  "select * from artical {$page->limit}";
10     echo ‘SQL = " ‘.$sql.‘"‘;
11 
12     /*第四步:通过扉页对象中的fpage()方法,输出所有分页的结构信息 */
13     echo $page -> fpage();

 

page.class.php

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

 

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