首页 > Web开发 > 详细

php分页原理

时间:2014-07-01 14:12:36      阅读:360      评论:0      收藏:0      [点我收藏+]

一、php分页流程

新手最好在设计先画流程图

 

二、代码实现

<meta http-equiv="Content-Type" content="text/html;charset=utf8"/>
<?php
if (isset($_GET[‘page‘]))      //获取url里的page
{
    $page = intval($_GET[‘page‘]);
}
else
{
    $page = 1;        //其他情况,都指向第一页
}
function _PAGEFT($totle, $displaypg = 20, $url = ‘‘) {
  //$totle表示总条数,$displaypg表示每页显示数目,$url表示url里显示的php文件
    global  $page, $firstcount, $pagenav, $_SERVER;

    $GLOBALS["displaypg"] = $displaypg;

    if (!$page)
        $page = 1;           //没有接收到页数,默认为1
    if (!$url) {
        $url = $_SERVER["REQUEST_URI"];   //没有传输url,默认当前文件
    }

    //URL分析:
    $parse_url = parse_url($url);
    $url_query = $parse_url["query"]; //单独取出URL的查询字串
    if ($url_query) {
        $url_query = preg_replace("(^|&)page=$page", "", $url_query);
        $url = str_replace($parse_url["query"], $url_query, $url);
        if ($url_query)
            $url .= "&page";
        else
            $url .= "page";
    } else {
        $url .= "?page";
    }
    $lastpg = ceil($totle / $displaypg); //最后页,也是总页数
    $page = min($lastpg, $page);
    $prepg = $page-1; //上一页
    $nextpg = ($page == $lastpg ? 0 : $page+1); //下一页
    $firstcount = ($page -1) * $displaypg;

    //开始分页导航条代码:
    $pagenav = "显示第 <B>" . ($totle ? ($firstcount +1) : 0) . "</B>-<B>" . min($firstcount + $displaypg, $totle) . "</B> 条记录,共 $totle 条记录";

    //如果只有一页则跳出函数:
    if ($lastpg <= 1)
        return false;

    $pagenav .= " <a href=‘$url=1‘>首页</a> ";
    if ($prepg)
        $pagenav .= " <a href=‘$url=$prepg‘>前页</a> ";
    else
        $pagenav .= " 前页 ";
    if ($nextpg)
        $pagenav .= " <a href=‘$url=$nextpg‘>后页</a> ";
    else
        $pagenav .= " 后页 ";
    $pagenav .= " <a href=‘$url=$lastpg‘>尾页</a> ";

    //下拉跳转列表,循环列出所有页码:
    $pagenav .= " 到第 <select name=‘topage‘ size=‘1‘ onchange=‘window.location=\"$url=\"+this.value‘>\n";
    for ($i = 1; $i <= $lastpg; $i++) {
        if ($i == $page)
        {
            $pagenav .= "<option value=‘$i‘ selected>$i</option>\n";
        }

        else
            $pagenav .= "<option value=‘$i‘>$i</option>\n";
    }
    $pagenav .= "</select> 页,共 $lastpg 页";
}$conn = new PDO("mysql:localhost=host;dbname=regi","root","900914");
$conn->exec("set names utf8");
$result = $conn->query("select * from member");
$num = $result->fetchAll();
$total = count($num);

_PAGEFT($total, 5);

$result = $conn->query("select * from member limit $firstcount,$displaypg");
while ($row=$result->fetch())
{
    echo "<hr><b>".$row[username]."|".md5($row[password])."</b>";
}

echo ‘</br>‘.$pagenav;

 

 

php分页原理,布布扣,bubuko.com

php分页原理

原文:http://www.cnblogs.com/eyeSpace/p/3818071.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!