在框架中引入,只需更改命名空间,调用$pageArr = PageStr::pageStr($sum,$set='a.id',$showNums);其中$sum是数据总量,$set是分页依据字段,$showNums是每页显示的数据量;结果返回数组array('pageStr'=$
在框架中引入,只需更改命名空间,调用$pageArr = PageStr::pageStr($sum,$set='a.id',$showNums);其中$sum是数据总量,$set是分页依据字段,$showNums是每页显示的数据量;结果返回数组array('pageStr'=>$pageStr,'sum'=>$sum,'where'=>$where);分别是分页字符串,要分页的数据总量,分页条件:如where id>n order by id limit m;(优化替换limit offset,len)
= 2 显示首页、上一页
$str = '';
if( $page > 1 ){
$str .= "
首页 ";
$str .= "
<<上一页 ";
}
// 5.页码显示
// 计算循环起始
$half = ceil(self::$pageShowNum/2);
// 先设置起始页码,再跟进起始页码设置结束页码 $pageShowNum 显示的页码个数
// 1.假设$page在正中间,
// 2.那么$start = $page-ceil($pageShowNum/2);
// 3.如果$start < 1 ,起始页码 $start = 1
// 4.此时结束页码 $end = $start + $pageShowNum -1;
// 5.r如果$end > $pages ;$end = $pages, $start = $end - $pageShowNum +1
$start = $page-$half+1;
if($start<1)$start = 1;
$end = $start+self::$pageShowNum-1;
if($end > $pages){
$end = $pages;
$start = $end - self::$pageShowNum+1;
}
for( $i=$start; $i <=$end ; $i++){
if($i == $page){
$str .= "
$i ";
}else{
$str .= "
$i ";
}
}
// 6.下一页和尾页
if( $page != $pages && $pages!=0){
$str .= "
下一页>> ";
$str .= "
尾页 ";
}
return $str;
}
/**
* [输出分页字符串,要分页的数据总量,分页条件where id>n order by id limit 替换 limit offset len]
* @param [int] $sum [要分页的数据总量]
* @param string $set [分页依据字段,一般为主键id]
* @param integer $showNums [每页显示的数据量]
* @return [array] [输出分页字符串,要分页的数据总量,分页条件where id>n order by id limit 替换 limit offset len]
*/
public static function pageStr($sum,$set='id',$showNums=5){
//获取当前页码
if(isset($_GET['page'])){
$page = intval($_GET['page']);
//分页起始位置
$start = $page?($page-1)*$showNums:0;
}else $start = 0;
//优化查询分页用where id>n order by id limit 替换 limit offset len
$where = " WHERE ( {$set} >{$start} ) ORDER BY {$set} LIMIT {$showNums}";
// 获取当前工作路径
$uri = $_SERVER['REQUEST_URI'];
$uri = substr_replace($uri,'?',stripos($uri,'?'));
// 输出分页字符串
$pageStr = self::pages($sum,$uri,$showNums);
return array('pageStr'=>$pageStr,'sum'=>$sum,'where'=>$where);
}
}
?>
下面是css样式代码,请自行分离
/************************************ 翻页 ********************************/
.pagination {
height: 36px;
margin: 18px 0;
float: right;
}
.pagination ul {
display: inline-block;
*display: inline;
/* IE7 inline-block hack */
*zoom: 1;
margin-left: 0;
margin-bottom: 0;
-webkit-border-radius: 3px;
-moz-border-radius: 3px;
border-radius: 3px;
-webkit-box-shadow: 0 1px 2px rgba(0, 0, 0, 0.05);
-moz-box-shadow: 0 1px 2px rgba(0, 0, 0, 0.05);
box-shadow: 0 1px 2px rgba(0, 0, 0, 0.05);
}
.pagination li {
display: inline;
}
.pagination a,.pagination span {
float: left;
padding: 0 14px;
line-height: 34px;
text-decoration: none;
border: 1px solid #ddd;
border-left-width: 0;
}
.pagination a:hover,
.pagination .active a,.pagination .active span {
background-color: #f5f5f5;
}
.pagination .active a,.pagination .active span {
color: #999999;
cursor: default;
}
.pagination .disabled span,
.pagination .disabled a,
.pagination .disabled a:hover {
color: #999999;
background-color: transparent;
cursor: default;
}
.pagination li:first-child a,.pagination li:first-child span {
border-left-width: 1px;
-webkit-border-radius: 3px 0 0 3px;
-moz-border-radius: 3px 0 0 3px;
border-radius: 3px 0 0 3px;
}
.pagination li:last-child a,.pagination li:last-child span {
-webkit-border-radius: 0 3px 3px 0;
-moz-border-radius: 0 3px 3px 0;
border-radius: 0 3px 3px 0;
}
.pagination-centered {
text-align: center;
}
.pagination-right {
text-align: right;
}
.pager {
margin-left: 0;
margin-bottom: 18px;
list-style: none;
text-align: center;
*zoom: 1;
}
.pager:before,
.pager:after {
display: table;
content: "";
}
.pager:after {
clear: both;
}
.pager li {
display: inline;
}
.pager a {
display: inline-block;
padding: 5px 14px;
background-color: #fff;
border: 1px solid #ddd;
-webkit-border-radius: 15px;
-moz-border-radius: 15px;
border-radius: 15px;
}
.pager a:hover {
text-decoration: none;
background-color: #f5f5f5;
}
.pager .next a {
float: right;
}
.pager .previous a {
float: left;
}
.pager .disabled a,
.pager .disabled a:hover {
color: #999999;
background-color: #fff;
cursor: default;
}