完美分页类

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

 

posted on 2020-11-17 13:51  Ben丶大壮  阅读(112)  评论(0编辑  收藏  举报

导航