<?php
class
Page{
public
$firstRow
;
public
$listRows
;
protected
$totalPages
;
protected
$totalRows
;
protected
$nowPage
;
protected
$coolPages
;
protected
$rollPage
;
protected
$isShowLinkPage
= FALSE;
public
$isJumpPage
= FALSE;
public
$AjaxType
= FALSE;
public
$function_name
= 'ajax_page';
protected
$config
=
array
('header'=>'条记录','prev'=>'上一页','next'=>'下一页','first'=>'首页','last'=>'末页','theme'=>' %totalRow% %header% %nowPage%/%totalPage% 页 %upPage% %downPage% %first% %prePage% %linkPage% %nextPage% %
end
% %jump%');
public
function
__construct(
$totalRows
,
$listRows
,
$rollpage
){
$this
->totalRows =
$totalRows
;
$this
->listRows =
$listRows
;
$this
->rollPage =
$rollpage
;
$this
->listRows = !
empty
(
$listRows
)?
$listRows
:10;
$this
->isShowLinkPage =
empty
(
$rollpage
)?false:true;
$this
->totalPages =
ceil
(
$this
->totalRows/
$this
->listRows);
$this
->coolPages =
ceil
(
$this
->totalPages/
$this
->rollPage);
$this
->nowPage = !
empty
(
$_REQUEST
['page'])?
$_REQUEST
['page']:1;
if
(!
empty
(
$this
->totalPages) &&
$this
->nowPage>
$this
->totalPages) {
$this
->nowPage =
$this
->totalPages;
}
$this
->firstRow =
$this
->listRows*(
$this
->nowPage-1);
}
public
function
setConfig(
$name
,
$value
) {
if
(isset(
$this
->config[
$name
])) {
$this
->config[
$name
] =
$value
;
}
}
public
function
show() {
if
(0 ==
$this
->totalRows)
return
'';
$p
= 'page';
$nowCoolPage
=
ceil
(
$this
->nowPage/
$this
->rollPage);
$request_url
=
$_SERVER
['REQUEST_URI'];
if
(
strpos
(
$request_url
,'?')){
$url
=
$request_url
;
}
else
{
$url
=
$request_url
.'?';
}
$parse
=
parse_url
(
$url
);
if
(isset(
$parse
['query'])) {
parse_str
(
$parse
['query'],
$params
);
unset(
$params
[
$p
]);
$url
=
$parse
['path'].'?'.http_build_query(
$params
);
}
$upRow
=
$this
->nowPage-1;
$downRow
=
$this
->nowPage+1;
if
(
$upRow
>0){
if
(
$this
->AjaxType)
$upPage
='<a href=javascript:'.
$this
->function_name.'('.
$upRow
.');>'.
$this
->config['prev'].'</a>';
else
$upPage
='<a href=\''.
$url
.'&'.
$p
.'='.
$upRow
.'\'>'.
$this
->config['prev'].'</a>';
}
else
{
$upPage
='';
}
if
(
$downRow
<=
$this
->totalPages){
if
(
$this
->AjaxType)
$downPage
='<a href=javascript:'.
$this
->function_name.'('.
$downRow
.');>'.
$this
->config['next'].'</a>';
else
$downPage
='<a href=\''.
$url
.'&'.
$p
.'='.
$downRow
.'\'>'.
$this
->config['next'].'</a>';
}
else
{
$downPage
='';
}
if
(
$this
->nowPage == 1){
$theFirst
= '';
$prePage
= '';
}
else
{
$preRow
=
$this
->nowPage-
$this
->rollPage;
if
(
$this
->AjaxType)
$theFirst
= '<a href=javascript:'.
$this
->function_name.'(1);>'.
$this
->config['first'].'</a>';
else
$theFirst
= '<a href=\''.
$url
.'&'.
$p
.'=1\' >'.
$this
->config['first'].'</a>';
}
if
(
$this
->nowPage ==
$this
->totalPages){
$nextPage
= '';
$theEnd
='';
}
else
{
$nextRow
=
$this
->nowPage+
$this
->rollPage;
$theEndRow
=
$this
->totalPages;
if
(
$this
->AjaxType)
$theEnd
= '<a href=javascript:'.
$this
->function_name.'('.
$theEndRow
.');>'.
$this
->config['last'].'</a>';
else
$theEnd
= '<a href=\''.
$url
.'&'.
$p
.'='.
$theEndRow
.'\' >'.
$this
->config['last'].'</a>';
}
if
(
$this
->isShowLinkPage){
$linkPage
= '';
for
(
$i
=1;
$i
<=
$this
->rollPage;
$i
++){
$page
=(
$nowCoolPage
-1)*
$this
->rollPage+
$i
;
if
(
$this
->nowPage%
$this
->rollPage ==0 ){
$page
= (
$nowCoolPage
-1)*
$this
->rollPage+
$i
+
$this
->rollPage-1;
}
if
(
$page
!=
$this
->nowPage){
if
(
$page
<=
$this
->totalPages){
if
(
$this
->AjaxType)
$linkPage
.= '&nbsp;<a href=javascript:'.
$this
->function_name.'('.
$page
.');>&nbsp;'.
$page
.'&nbsp;</a>';
else
$linkPage
.= '&nbsp;<a href=\''.
$url
.'&'.
$p
.'='.
$page
.'\'>&nbsp;'.
$page
.'&nbsp;</a>';
}
else
{
break
;
}
}
else
{
if
(
$this
->totalPages != 1){
$linkPage
.= '&nbsp;<span
class
=\'currentpage\'>'.
$page
.'</span>';
}
}
}
}
if
(
$this
->isJumpPage){
$jump
= '<input type=\'text\' id=\'jumppage\' value = \''.
$this
->nowPage.'\'>';
$jump
.='<input type=\'button\' id=\'jumppagebutton\' value=\'GO\' onclick=\'go_page('.
$this
->nowPage.')\'>';
$jump
.='<script>
function
go_page(page){
var
jumptopage = document.getElementById(\'jumppage\').value;
var
topage;';
$jump
.='
if
(jumptopage==page||isNaN(jumptopage)){
return
false;}
else
{ ';
$jump
.='
if
(jumptopage>'.
$this
->totalPages.'){topage = '.
$this
->totalPages.'}';
$jump
.='
else
if
(jumptopage<1){topage = 1;}
else
{topage = jumptopage;}';
if
(
$this
->AjaxType){
$jump
.=
$this
->function_name.'(topage);';
}
else
{
$jump
.='window.location.href = \''.
$url
.'&'.
$p
.'=\'+topage;';
}
$jump
.='}}</script>';
}
$pageStr
=
str_replace
(
array
('%header%','%nowPage%','%totalRow%','%totalPage%','%upPage%','%downPage%','%first%','%prePage%','%linkPage%','%nextPage%','%
end
%','%jump%'),
array
(
$this
->config['header'],
$this
->nowPage,
$this
->totalRows,
$this
->totalPages,
$upPage
,
$downPage
,
$theFirst
,
$prePage
,
$linkPage
,
$nextPage
,
$theEnd
,
$jump
),
$this
->config['theme']);
return
$pageStr
;
}
}