//Page function First page: url/1 Second page: 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; // display How many pages* 2
$start = max(1, $page - $shownum);
$end = min($totalpage, $page + $shownum);
/ / If $shownum is insufficient, complete the left and right sides
$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 .= '
1 '.($start > 2 ? '... ' : ' ').'';
for($i=$start; $i<=$end; $i++) {
if($i == $page) {
$ s .= '
'.$i.'';// checked
} else {
$s .= '
'.$i.'';
}
}
if($end != $totalpage) $s .= '
'.($totalpage - $end > 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);
}
We can handle the back-end part in just a few sentences.