中间加省略号的PHP分页方法
实在受不了搜索到的教程,根本无法使用,于是自己写了一个。
/**
* auther 阿修罗 qq 610176732
* [page description] 分页
* @param [type] $sum [总页数]
* @param [type] $pagenum [页数]
* @return [type] [description]
*/
function page($sum,$pagenum){
$span = "";
if($sum > 0){
if($pagenum <=0){$pagenum = 1;}
if($pagenum >= $sum){$pagenum = $sum;}
$k = $pagenum-1 <= 0 ? 1:$pagenum-1;
$m = $sum - 6 <= 0 ?1:$sum-6;
$pageM = $pagenum == 1?$pagenum+2:$pagenum + 1;
if($sum - $pagenum >= 6){
for($i = $k; $i <= $pageM; $i++){
$class = $i == $pagenum?'Pcurrent':'Pnum';
$span .= '<a class="'.$class.'" onclick="get_logs('.$i.')">'.$i.'</a>';
}
$span .= '<a class="Pnum" >....</a>';
for($i = $sum - 3; $i <= $sum; $i++){
$span .= '<a class="Pnum" onclick="get_logs('.$i.')" >'.$i.'</a>';
}
}else{
for($i = $m; $i <= $sum; $i++){
$class = $i == $pagenum?'Pcurrent':'Pnum';
$span .= '<a class="'.$class.'" onclick="get_logs('.$i.')">'.$i.'</a>';
}
}
}
return $span;
}
/**
* auther 阿修罗 qq 610176732
* [page description] 分页
* @param [type] $sum [总页数]
* @param [type] $pagenum [页数]
* @return [type] [description]
*/
function get_logs(){
$param = $this->post;
$p= $param['p'];
//这里你需要改造成自己的查询总数
$count = db()->count('access_log','*',$where);
$start = ($p-1)*$pageSize;
$totalPage = ceil($count/$pageSize);
//这里你需要改造成自己的SQL查询,适配自己的框架
$data = db()->select('access_log','*',[
'AND'=>$where,
'LIMIT' => [$start, $pageSize],
"ORDER" => [
'time_local'=>'DESC'
]
]);
$span = '<a class="Pnum" onclick="get_logs(1)" >首页</a>';
$span .= $this->page($totalPage,$p);
$span .= '<a class="Pnum" onclick="get_logs('.$totalPage.')" >尾页</a>';
$span .= '<span class="Pcount">共'.$count.'条</span>';
$this->success('success',[
'data'=>$data,
'page'=>$span
]);
}
|