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 );
//在分页信息中显示内容,可以自己通过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] = " 共<b> {
$this->total} </b>{
$this->config["head"]} "
;
87 $html[1] = " 本页 <b>".
$this->disnum()."</b> 条 "
;
88 $html[2] = " 本页从 <b>{
$this->start()}-{
$this->end()}</b> 条 "
;
89 $html[3] = " <b>{
$this->page}/{
$this->pageNum}</b>页 "
;
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 = " <a href='{
$this->uri}page=1'>{
$this->config["first"]}</a> "
;
159 $str .= "<a href='{
$this->uri}page=".(
$this->page-1)."'>{
$this->config["
prev"]}</a> "
;
160 return $str;
161 }
162
163 }
164
165 /* 在对象内部使用的私有方法,用于获取页数列表信息 */
166 private function pageList(){
167 $linkPage = " <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> "
;
176 }
177 /*当前页的信息 */
178 if(
$this->pageNum > 1
)
179 $linkPage .= "<span style='padding:1px 2px;background:#BBB;color:white'>{
$this->page}</span> "
;
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> "
;
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 = " <a href='{
$this->uri}page=".(
$this->page+1)."'>{
$this->config["
next"]}</a> "
;
197 $str .= " <a href='{
$this->uri}page=".(
$this->pageNum)."'>{
$this->config["last"]}</a> "
;
198 return $str;
199 }
200 }
201
202 /* 在对象内部使用的私有方法,用于显示和处理表单跳转页面 */
203 private function goPage(){
204 if(
$this->pageNum > 1
) {
205 return ' <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+\'\'"> '
;
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 $count=200
;
220
221 $page =
new page(
$count,3
);
222 echo $page->fpage();
分页类效果
转载于:https://www.cnblogs.com/lizhaoyao/p/4312294.html