Home > php教程 > php手册 > body text

php分页示例分享

WBOY
Release: 2016-06-06 20:23:02
Original
1190 people have browsed it

这篇文章主要介绍了php分页示例,需要的朋友可以参考下

复制代码 代码如下:


//分页函数 第一页: 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);

Related labels:
source:php.cn
Statement of this Website
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
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template