连络 thinkPHP 分页写成本身分页类
添加时间:2013-5-26 点击量:
1 <?php
2 // +----------------------------------------------------------------------
3 // | 参考 ThinkPHP 分页类改变而来
4 // +----------------------------------------------------------------------
5 // | 有两种样式:1、当面页码始终在中心。 2、当点到分页条两边的页码时,主动翻页(默认)。
6 // +----------------------------------------------------------------------
7 // | Author: zb搜刮引擎优化ag <zbsoeagy@qq.com>
8 // +----------------------------------------------------------------------
9 // ¥Id: page.class.php 2013-05-26 00:08:49
10
11 session_start(); //因为要记录用户是向前,还是向后翻页,故启用了session
12
13 class Page{
14
15 public ¥page; //当前页
16 public ¥nextPage; //上一页
17 public ¥prevPage; //下一页
18 public ¥pageMax; //最大页
19 public ¥startPage; //分页条起码
20 public ¥endPage; // 分页条止码
21 public ¥style; //样式:¥style = mid
22 public ¥parameter;//页数跳转时要带的参数
23 protected ¥varPage;//默认分页变量名
24 protected ¥config = array(
25 header=>笔记录,prev=>上一页,next=>下一页,first=>首页,last=>尾页,
26 theme => %records% %header% %page%/%pageMax% 页 %firstPage%%prevPage%%linkPage%%nextPage%%lastPage%
27 );
28
29 /
30 +----------------------------------------------------------
31 架造函数
32 +----------------------------------------------------------
33 @param ¥records 总的记录数
34 @param ¥pageSize 每页显示记录数
35 @param ¥parameter 分页跳转的参数
36 +----------------------------------------------------------
37 /
38
39 function __construct(¥records, ¥pageSize, ¥pageVal = p, ¥parameter = null){
40
41 ¥this->records = ¥records;
42 ¥this->pageMax = ceil(¥records/¥pageSize);
43 ¥this->page= max(intval(¥_GET[¥pageVal]), 1); //最小为1
44 ¥this->page = min(¥this->page, ¥this->pageMax); //最大为末页
45 ¥this->nextPage = ¥this->page + 1;
46 ¥this->prevPage = ¥this->page - 1;
47 ¥this->varPage = ¥pageVal; //默认分页变量名
48 ¥this->parameter = ¥parameter;
49 }
50
51 /
52 +----------------------------------------------------------
53 设置样式
54 +----------------------------------------------------------
55 @param ¥name 内置参数名
56 @param ¥value 给定参数值
57 +----------------------------------------------------------
58 /
59 function setConfig(¥name, ¥value){
60 if(isset(¥this->config[¥name])){
61 ¥this->config[¥name] = ¥value;
62 }
63 }
64
65 /
66 +----------------------------------------------------------
67 分页显示输出
68 +----------------------------------------------------------
69 @param ¥length 分页条长度
70 ----------------------------------------------------------
71 @param ¥hide 是否当令隐蔽上、下一页,比如:当前是第一页,则隐蔽上一页
72 +----------------------------------------------------------
73 /
74 function show(¥length, ¥hide = false){
75
76 if(0 == ¥this->records) return ;
77
78 ¥url = ¥_SERVER[REQUEST_URI].(strpos(¥_SERVER[REQUEST_URI],?)?:?).¥this->parameter;
79 ¥parse = parse_url(¥url);
80 if(isset(¥parse[query])){
81 parse_str(¥parse[query],¥params);
82 unset(¥params[¥this->varPage]);
83 ¥url = ¥parse[path].?.http_build_query(¥params);
84 }
85
86 if(mid == ¥this->style){ //当前页居中
87 if(¥length % 2 != 0 ){
88 ¥midder = ceil(¥length / 2);
89 ¥big_repair = ¥midder - 1 ;//当上方以进一法取整,则这里为减1,反之为加1
90 }else{
91 ¥big_repair = ¥midder = ¥length / 2;
92 }
93 ¥sml_repair = ¥midder- 1; //这是小补丁
94 ¥this->startPage = (¥this->page + ¥midder) > ¥this->pageMax ? ¥this->pageMax - ¥length : ¥this->page - ¥sml_repair;
95 ¥this->endPage = (¥this->page < ¥midder)? ¥length : ¥this->page + ¥big_repair;
96 }else{ //最后一页翻页
97 ¥this->startPage = (¥this->page >= ¥length)? ¥_SESSION[startPage] : 1;
98 ¥this->endPage = ¥this->startPage + ¥length - 1;
99
100 ¥this->lastPage = ¥_SESSION[lastPage]; //前次的页码
101 ¥_SESSION[lastPage] = ¥this->page; //保存当前页
102
103 if(¥this->lastPage < ¥this->page){ //后翻页
104 if(¥this->page >= ¥this->endPage){
105 ¥this->startPage = ¥_SESSION[startPage] = ¥this->page;
106 ¥this->endPage = ¥this->startPage + ¥length -1;
107 }
108 }elseif(¥this->lastPage == ¥this->page){//切换
109 if(¥this->page == ¥this->endPage){
110 ¥this->startPage = ¥_SESSION[startPage] = ¥this->page;
111 ¥this->endPage = ¥this->startPage + ¥length -1;
112 }else{
113 ¥this->startPage = ¥_SESSION[startPage] = ¥this->page - ¥length + 1;
114 ¥this->endPage = ¥this->startPage + ¥length -1;
115 }
116 }else{//前翻页
117 if(¥this->page == ¥this->startPage ){
118 ¥this->startPage = ¥_SESSION[startPage] = ¥this->page - ¥length + 1;
119 ¥this->endPage = ¥this->startPage + ¥length -1;
120 }
121 }
122 //echo 开端页码:. ¥this->startPage.<br/>;
123 //echo 前次页码:. ¥this->lastPage.<br/>;
124 }
125 //批改一下
126 ¥this->startPage = min(¥this->pageMax - ¥length + 1, ¥this->startPage); //最大
127 ¥this->startPage = max(¥this->startPage, 1);//最小为第一页(先求最大,再求最小)
128
129 ¥this->endPage = ¥this->startPage + ¥length -1;
130 ¥this->endPage = min(¥this->pageMax, ¥this->endPage); //最大为末页
131
132 ¥_SESSION[startPage] = ¥this->startPage; //批改之后,从头保存
133
134 //echo 开端页码:. ¥this->startPage.<br/>;
135 //echo 前次页码:. ¥this->lastPage.<br/>;
136
137 ¥firstPage = <a href=¥url&¥this->varPage=1>.¥this->config[first].</a>;
138 ¥prevPage = <a href=¥url&¥this->varPage=¥this->prevPage>.¥this->config[prev].</a>;
139 ¥nextPage = <a href=¥url&¥this->varPage=¥this->nextPage>.¥this->config[next].</a>;
140 ¥lastPage = <a href=¥url&¥this->varPage=¥this->pageMax>.¥this->config[last].</a>;
141 if(¥hide){ //若是是隐蔽模式
142 (¥this->nextPage >= ¥this->pageMax)? ¥nextPage = : ;
143 (¥this->prevPage < 1)? ¥prevPage = : ;
144 }
145
146 for(¥i = ¥this->startPage; ¥i <= ¥this->endPage; ¥i++){
147 if (¥this->page == ¥i)
148 ¥linkPage .= <span class=current href=¥url&¥this->varPage=¥i>¥i</span>;
149 else
150 ¥linkPage .= <a href=¥url&¥this->varPage=¥i>¥i</a>;
151 }
152 ¥search = array(%records%,%header%,%page%,%pageMax%,%firstPage%,%prevPage%,%linkPage%,%nextPage%,%lastPage%);
153 ¥replace = array(¥this->records,¥this->config[header],¥this->page,¥this->pageMax,¥firstPage,¥prevPage,¥linkPage,¥nextPage,¥lastPage);
154 ¥pageStr = str_replace(¥search, ¥replace, ¥this->config[theme]);
155 ¥pageStr = str_replace(¥search,,¥pageStr); //再调换一次
156 return ¥pageStr;
157 }
158
159 }
160
161 ¥page = new Page(340, 10);
162 ¥theme = %records% %header% %page%/%pageMax% 页 %firstPage%%prevPage%%linkPage%%nextPage%%lastPage%;
163 ¥page->setConfig(theme,¥theme); //可以批改样式,比如:当不要末页是,可以删除 %lastPage%,或者改变地位次序
164 ¥page->setConfig(last,最后一页); //可自定义文字 : header=笔记录,prev=上一页,next=下一页,first=首页,last=尾页
165
166 echo ¥page->show(10, true);
167
168 ?>
169 <style>
170 a,span{ margin:2px 5px;}
171 .current{ color:red;}
172 </style>
原来,再大的房子,再大的床,没有相爱的人陪伴,都只是冰冷的物质。而如果身边有爱人陪伴,即使房子小,床小,也觉得无关紧要,因为这些物质上面有了爱的温度,成了家的元素。—— 何珞《婚房》#书摘#
1 <?php
2 // +----------------------------------------------------------------------
3 // | 参考 ThinkPHP 分页类改变而来
4 // +----------------------------------------------------------------------
5 // | 有两种样式:1、当面页码始终在中心。 2、当点到分页条两边的页码时,主动翻页(默认)。
6 // +----------------------------------------------------------------------
7 // | Author: zb搜刮引擎优化ag <zbsoeagy@qq.com>
8 // +----------------------------------------------------------------------
9 // ¥Id: page.class.php 2013-05-26 00:08:49
10
11 session_start(); //因为要记录用户是向前,还是向后翻页,故启用了session
12
13 class Page{
14
15 public ¥page; //当前页
16 public ¥nextPage; //上一页
17 public ¥prevPage; //下一页
18 public ¥pageMax; //最大页
19 public ¥startPage; //分页条起码
20 public ¥endPage; // 分页条止码
21 public ¥style; //样式:¥style = mid
22 public ¥parameter;//页数跳转时要带的参数
23 protected ¥varPage;//默认分页变量名
24 protected ¥config = array(
25 header=>笔记录,prev=>上一页,next=>下一页,first=>首页,last=>尾页,
26 theme => %records% %header% %page%/%pageMax% 页 %firstPage%%prevPage%%linkPage%%nextPage%%lastPage%
27 );
28
29 /
30 +----------------------------------------------------------
31 架造函数
32 +----------------------------------------------------------
33 @param ¥records 总的记录数
34 @param ¥pageSize 每页显示记录数
35 @param ¥parameter 分页跳转的参数
36 +----------------------------------------------------------
37 /
38
39 function __construct(¥records, ¥pageSize, ¥pageVal = p, ¥parameter = null){
40
41 ¥this->records = ¥records;
42 ¥this->pageMax = ceil(¥records/¥pageSize);
43 ¥this->page= max(intval(¥_GET[¥pageVal]), 1); //最小为1
44 ¥this->page = min(¥this->page, ¥this->pageMax); //最大为末页
45 ¥this->nextPage = ¥this->page + 1;
46 ¥this->prevPage = ¥this->page - 1;
47 ¥this->varPage = ¥pageVal; //默认分页变量名
48 ¥this->parameter = ¥parameter;
49 }
50
51 /
52 +----------------------------------------------------------
53 设置样式
54 +----------------------------------------------------------
55 @param ¥name 内置参数名
56 @param ¥value 给定参数值
57 +----------------------------------------------------------
58 /
59 function setConfig(¥name, ¥value){
60 if(isset(¥this->config[¥name])){
61 ¥this->config[¥name] = ¥value;
62 }
63 }
64
65 /
66 +----------------------------------------------------------
67 分页显示输出
68 +----------------------------------------------------------
69 @param ¥length 分页条长度
70 ----------------------------------------------------------
71 @param ¥hide 是否当令隐蔽上、下一页,比如:当前是第一页,则隐蔽上一页
72 +----------------------------------------------------------
73 /
74 function show(¥length, ¥hide = false){
75
76 if(0 == ¥this->records) return ;
77
78 ¥url = ¥_SERVER[REQUEST_URI].(strpos(¥_SERVER[REQUEST_URI],?)?:?).¥this->parameter;
79 ¥parse = parse_url(¥url);
80 if(isset(¥parse[query])){
81 parse_str(¥parse[query],¥params);
82 unset(¥params[¥this->varPage]);
83 ¥url = ¥parse[path].?.http_build_query(¥params);
84 }
85
86 if(mid == ¥this->style){ //当前页居中
87 if(¥length % 2 != 0 ){
88 ¥midder = ceil(¥length / 2);
89 ¥big_repair = ¥midder - 1 ;//当上方以进一法取整,则这里为减1,反之为加1
90 }else{
91 ¥big_repair = ¥midder = ¥length / 2;
92 }
93 ¥sml_repair = ¥midder- 1; //这是小补丁
94 ¥this->startPage = (¥this->page + ¥midder) > ¥this->pageMax ? ¥this->pageMax - ¥length : ¥this->page - ¥sml_repair;
95 ¥this->endPage = (¥this->page < ¥midder)? ¥length : ¥this->page + ¥big_repair;
96 }else{ //最后一页翻页
97 ¥this->startPage = (¥this->page >= ¥length)? ¥_SESSION[startPage] : 1;
98 ¥this->endPage = ¥this->startPage + ¥length - 1;
99
100 ¥this->lastPage = ¥_SESSION[lastPage]; //前次的页码
101 ¥_SESSION[lastPage] = ¥this->page; //保存当前页
102
103 if(¥this->lastPage < ¥this->page){ //后翻页
104 if(¥this->page >= ¥this->endPage){
105 ¥this->startPage = ¥_SESSION[startPage] = ¥this->page;
106 ¥this->endPage = ¥this->startPage + ¥length -1;
107 }
108 }elseif(¥this->lastPage == ¥this->page){//切换
109 if(¥this->page == ¥this->endPage){
110 ¥this->startPage = ¥_SESSION[startPage] = ¥this->page;
111 ¥this->endPage = ¥this->startPage + ¥length -1;
112 }else{
113 ¥this->startPage = ¥_SESSION[startPage] = ¥this->page - ¥length + 1;
114 ¥this->endPage = ¥this->startPage + ¥length -1;
115 }
116 }else{//前翻页
117 if(¥this->page == ¥this->startPage ){
118 ¥this->startPage = ¥_SESSION[startPage] = ¥this->page - ¥length + 1;
119 ¥this->endPage = ¥this->startPage + ¥length -1;
120 }
121 }
122 //echo 开端页码:. ¥this->startPage.<br/>;
123 //echo 前次页码:. ¥this->lastPage.<br/>;
124 }
125 //批改一下
126 ¥this->startPage = min(¥this->pageMax - ¥length + 1, ¥this->startPage); //最大
127 ¥this->startPage = max(¥this->startPage, 1);//最小为第一页(先求最大,再求最小)
128
129 ¥this->endPage = ¥this->startPage + ¥length -1;
130 ¥this->endPage = min(¥this->pageMax, ¥this->endPage); //最大为末页
131
132 ¥_SESSION[startPage] = ¥this->startPage; //批改之后,从头保存
133
134 //echo 开端页码:. ¥this->startPage.<br/>;
135 //echo 前次页码:. ¥this->lastPage.<br/>;
136
137 ¥firstPage = <a href=¥url&¥this->varPage=1>.¥this->config[first].</a>;
138 ¥prevPage = <a href=¥url&¥this->varPage=¥this->prevPage>.¥this->config[prev].</a>;
139 ¥nextPage = <a href=¥url&¥this->varPage=¥this->nextPage>.¥this->config[next].</a>;
140 ¥lastPage = <a href=¥url&¥this->varPage=¥this->pageMax>.¥this->config[last].</a>;
141 if(¥hide){ //若是是隐蔽模式
142 (¥this->nextPage >= ¥this->pageMax)? ¥nextPage = : ;
143 (¥this->prevPage < 1)? ¥prevPage = : ;
144 }
145
146 for(¥i = ¥this->startPage; ¥i <= ¥this->endPage; ¥i++){
147 if (¥this->page == ¥i)
148 ¥linkPage .= <span class=current href=¥url&¥this->varPage=¥i>¥i</span>;
149 else
150 ¥linkPage .= <a href=¥url&¥this->varPage=¥i>¥i</a>;
151 }
152 ¥search = array(%records%,%header%,%page%,%pageMax%,%firstPage%,%prevPage%,%linkPage%,%nextPage%,%lastPage%);
153 ¥replace = array(¥this->records,¥this->config[header],¥this->page,¥this->pageMax,¥firstPage,¥prevPage,¥linkPage,¥nextPage,¥lastPage);
154 ¥pageStr = str_replace(¥search, ¥replace, ¥this->config[theme]);
155 ¥pageStr = str_replace(¥search,,¥pageStr); //再调换一次
156 return ¥pageStr;
157 }
158
159 }
160
161 ¥page = new Page(340, 10);
162 ¥theme = %records% %header% %page%/%pageMax% 页 %firstPage%%prevPage%%linkPage%%nextPage%%lastPage%;
163 ¥page->setConfig(theme,¥theme); //可以批改样式,比如:当不要末页是,可以删除 %lastPage%,或者改变地位次序
164 ¥page->setConfig(last,最后一页); //可自定义文字 : header=笔记录,prev=上一页,next=下一页,first=首页,last=尾页
165
166 echo ¥page->show(10, true);
167
168 ?>
169 <style>
170 a,span{ margin:2px 5px;}
171 .current{ color:red;}
172 </style>
原来,再大的房子,再大的床,没有相爱的人陪伴,都只是冰冷的物质。而如果身边有爱人陪伴,即使房子小,床小,也觉得无关紧要,因为这些物质上面有了爱的温度,成了家的元素。—— 何珞《婚房》#书摘#