分页 (用page方法)

用page方法完成分页

  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             
 90             $html[2] = "&nbsp;本页从 <b>{$this->start()}-{$this->end()}</b> 条&nbsp;";
 91             
 92             $html[3] = "&nbsp;<b>{$this->page}/{$this->pageNum}</b>页&nbsp;";
 93             
 94             $html[4] = $this->firstprev();
 95             
 96             $html[5] = $this->pageList();
 97             
 98             $html[6] = $this->nextlast();
 99             
100             $html[7] = $this->goPage();
101             
102             $fpage = '<div style="font:12px \'\5B8B\4F53\',san-serif;">';
103             
104             if(count($arr) < 1)
105                 $arr = array(0, 1,2,3,4,5,6,7);
106             
107             for($i = 0; $i < count($arr); $i++)
108                 $fpage .= $html[$arr[$i]];
109         
110             $fpage .= '</div>';
111             return $fpage;
112         }
113         
114         /* 在对象内部使用的私有方法,*/
115         private function setLimit(){
116             if($this->page > 0)
117                 return ($this->page-1)*$this->listRows.", {$this->listRows}"; 
118             else
119                 return 0;
120         }
121 
122         /* 在对象内部使用的私有方法,用于自动获取访问的当前URL */
123         private function getUri($query){    
124             $request_uri = $_SERVER["REQUEST_URI"];    
125             $url = strstr($request_uri,'?') ? $request_uri :  $request_uri.'?';
126             
127             if(is_array($query))
128                 $url .= http_build_query($query);
129             else if($query != "")
130                 $url .= "&".trim($query, "?&");
131         
132             $arr = parse_url($url);
133 
134             if(isset($arr["query"])){
135                 parse_str($arr["query"], $arrs);
136                 unset($arrs["page"]);
137                 $url = $arr["path"].'?'.http_build_query($arrs);
138             }
139             
140             if(strstr($url, '?')) {
141                 if(substr($url, -1)!='?')
142                     $url = $url.'&';
143             }else{
144                 $url = $url.'?';
145             }
146             
147             return $url;
148         }
149 
150         /* 在对象内部使用的私有方法,用于获取当前页开始的记录数 */
151         private function start(){
152             if($this->total == 0)
153                 return 0;
154             else
155                 return ($this->page-1) * $this->listRows+1;
156         }
157 
158         /* 在对象内部使用的私有方法,用于获取当前页结束的记录数 */
159         private function end(){
160             return min($this->page * $this->listRows, $this->total);
161         }
162 
163         /* 在对象内部使用的私有方法,用于获取上一页和首页的操作信息 */
164         private function firstprev(){
165             if($this->page > 1) {
166                 $str = "&nbsp;<a href='{$this->uri}page=1'>{$this->config["first"]}</a>&nbsp;";
167                 $str .= "<a href='{$this->uri}page=".($this->page-1)."'>{$this->config["prev"]}</a>&nbsp;";        
168                 return $str;
169             }
170 
171         }
172     
173         /* 在对象内部使用的私有方法,用于获取页数列表信息 */
174         private function pageList(){
175             $linkPage = "&nbsp;<b>";
176             
177             $inum = floor($this->listNum/2);
178             /*当前页前面的列表 */
179             for($i = $inum; $i >= 1; $i--){
180                 $page = $this->page-$i;
181 
182                 if($page >= 1)
183                     $linkPage .= "<a href='{$this->uri}page={$page}'>{$page}</a>&nbsp;";
184             }
185             /*当前页的信息 */
186             if($this->pageNum > 1)
187                 $linkPage .= "<span style='padding:1px 2px;background:#BBB;color:white'>{$this->page}</span>&nbsp;";
188             
189             /*当前页后面的列表 */
190             for($i=1; $i <= $inum; $i++){
191                 $page = $this->page+$i;
192                 if($page <= $this->pageNum)
193                     $linkPage .= "<a href='{$this->uri}page={$page}'>{$page}</a>&nbsp;";
194                 else
195                     break;
196             }
197             $linkPage .= '</b>';
198             return $linkPage;
199         }
200 
201         /* 在对象内部使用的私有方法,获取下一页和尾页的操作信息 */
202         private function nextlast(){
203             if($this->page != $this->pageNum) {
204                 $str = "&nbsp;<a href='{$this->uri}page=".($this->page+1)."'>{$this->config["next"]}</a>&nbsp;";
205                 $str .= "&nbsp;<a href='{$this->uri}page=".($this->pageNum)."'>{$this->config["last"]}</a>&nbsp;";
206                 return $str;
207             }
208         }
209 
210         /* 在对象内部使用的私有方法,用于显示和处理表单跳转页面 */
211         private function goPage(){
212                 if($this->pageNum > 1) {
213                 return '&nbsp;<input style="width:20px;height:17px !important;height:18px;border:1px solid #CCCCCC;" type="text" οnkeydοwn="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" οnclick="javascript:var page=(this.previousSibling.value>'.$this->pageNum.')?'.$this->pageNum.':this.previousSibling.value;location=\''.$this->uri.'page=\'+page+\'\'">&nbsp;';
214             }
215         }
216 
217         /* 在对象内部使用的私有方法,用于获取本页显示的记录条数 */
218         private function disnum(){
219             if($this->total > 0){
220                 return $this->end()-$this->start()+1;
221             }else{
222                 return 0;
223             }
224         }
225     }
226 
227     
228     
229     
page.class.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">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
</head>

<body>
<!--共10页  5/10 首页 上一页 1 2 3 4 5 6 7 8 下一页 尾页 跳转 【】 页-->
<table width="100%" border="1" cellpadding="0" cellspacing="0">
	<tr>
    	<td>地区代号</td>
        <td>地区名称</td>
        <td>父级代号</td>
    </tr>
<?php
include("./DBDA.class.php");
include("./page.class.php");
$db = new DBDA();
//求总条数
$sz = "select count(*) from chinastates";
$az = $db->Query($sz);
$zts = $az[0][0];

//造分页类对象
$page = new Page($zts,20);

$sql = "select * from chinastates ".$page->limit;
echo $sql;

$attr = $db->Query($sql);
foreach($attr as $v)
{
	echo "<tr>
    	<td>{$v[0]}</td>
        <td>{$v[1]}</td>
        <td>{$v[2]}</td>
    </tr>";
}

?>
</table>
<div><?php 
//调用分页信息并输出
echo $page->fpage(6,5,4);  
?></div>
</body>
</html>

  效果如图:

转载于:https://www.cnblogs.com/bhmmlxieliming/p/6391787.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值