代码如下:
//分页函数 第一页: url/1 第二页: url/2
function pages($url,$totalnum,$page,$pagesize = 20) {
$urladd = '';
$url .= '/';$totalpage = ceil($totalnum / $pagesize);
if($totalpage < 2) return '';
$page = min($totalpage,$page);
$shownum = 5; // 显示多少个页 * 2$start = max(1,$page - $shownum);
$end = min($totalpage,$page + $shownum);// 不足 $shownum,补全左右两侧
$right = $page + $shownum - $totalpage;
$right > 0 && $start = max(1,$start -= $right);
$left = $page - $shownum;
$left < 0 && $end = min($totalpage,$end -= $left);$s = '';
$page != 1 && $s .= '';
if($start > 1) $s .= ' 2 ? '... ' : '').'';
for($i=$start; $i<=$end; $i++) {
if($i == $page) {
$s .= '';// checked
} else {
$s .= '';
}
}
if($end != $totalpage) $s .= ' 1 ? '... ' : '').$totalpage.'';
$page != $totalpage && $s .= '';
return $s;
}
function mid($n,$min,$max) {
if($n < $min) return $min;
if($n > $max) return $max;
return $n;
}function page($page,$n,$pagesize) {
$total = ceil($n / $pagesize);
$total < 1 AND $total = 1;
return mid($page,1,$total);
}
function pages($url,$totalnum,$page,$pagesize = 20) {
$urladd = '';
$url .= '/';$totalpage = ceil($totalnum / $pagesize);
if($totalpage < 2) return '';
$page = min($totalpage,$page);
$shownum = 5; // 显示多少个页 * 2$start = max(1,$page - $shownum);
$end = min($totalpage,$page + $shownum);// 不足 $shownum,补全左右两侧
$right = $page + $shownum - $totalpage;
$right > 0 && $start = max(1,$start -= $right);
$left = $page - $shownum;
$left < 0 && $end = min($totalpage,$end -= $left);$s = '';
$page != 1 && $s .= '';
if($start > 1) $s .= ' 2 ? '... ' : '').'';
for($i=$start; $i<=$end; $i++) {
if($i == $page) {
$s .= '';// checked
} else {
$s .= '';
}
}
if($end != $totalpage) $s .= ' 1 ? '... ' : '').$totalpage.'';
$page != $totalpage && $s .= '';
return $s;
}
function mid($n,$min,$max) {
if($n < $min) return $min;
if($n > $max) return $max;
return $n;
}function page($page,$n,$pagesize) {
$total = ceil($n / $pagesize);
$total < 1 AND $total = 1;
return mid($page,1,$total);
}
后端部分,我们几句就可以搞定。
代码如下: