Home >Backend Development >PHP Tutorial >php分页示例分享_PHP

php分页示例分享_PHP

WBOY
WBOYOriginal
2016-06-01 11:55:00830browse

复制代码 代码如下:
//分页函数 第一页: url/1 第二页: url/2
function pages($url, $totalnum, $page, $pagesize = 20) {
    $urladd = '';
    $url .= '/';

    $totalpage = ceil($totalnum / $pagesize);
    if($totalpage     $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
    $s = '';
    $page != 1 && $s .= '';
    if($start > 1) $s .= '1 '.($start > 2 ? '... ' : '').'';
    for($i=$start; $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     if($n > $max) return $max;
    return $n;
}

function page($page, $n, $pagesize) {
    $total = ceil($n / $pagesize);
    $total     return mid($page, 1, $total);
}

后端部分,我们几句就可以搞定。
复制代码 代码如下:
$pagesize = 20;    //每页显示多少用户
$n = user_count();      //总用户数
$page = page($page, $n, $pagesize);  //当前页数
$userlist = user_find($page, $pagesize);  //查找出当前页的用户
//html部分,分页效果,直接渲染到页面即可
$pagehtml = pages(url前缀, $n, $page, $pagesize);

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn