首页 > php教程 > php手册 > PHP实现数据分页显示

PHP实现数据分页显示

WBOY
发布: 2016-05-26 08:19:54
原创
1584 人浏览过

  分页在后台管理中是经常使用的功能,分页显示方便大量数据的管理。

  实例代码如下:

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>用户列表</title>
</head>
<body>
<?<span style="color: #000000;">php 
    </span><span style="color: #800080;">$con</span> = <span style="color: #008080;">mysql_connect</span>("localhost","root",""<span style="color: #000000;">);
   
    </span><span style="color: #008080;">mysql_query</span>("set names utf8"<span style="color: #000000;">);
    </span><span style="color: #008080;">mysql_select_db</span>("zhiye",<span style="color: #800080;">$con</span><span style="color: #000000;">);
    
    </span><span style="color: #800080;">$pageSize</span> = 1;      <span style="color: #008000;">//</span><span style="color: #008000;">每页显示的数量</span>
    <span style="color: #800080;">$rowCount</span> = 0;      <span style="color: #008000;">//</span><span style="color: #008000;">要从数据库中获取</span>
    <span style="color: #800080;">$pageNow</span> = 1;        <span style="color: #008000;">//</span><span style="color: #008000;">当前显示第几页
    
    //如果有pageNow就使用,没有就默认第一页。</span>
    <span style="color: #0000ff;">if</span> (!<span style="color: #0000ff;">empty</span>(<span style="color: #800080;">$_GET</span>['pageNow'<span style="color: #000000;">])){
        </span><span style="color: #800080;">$pageNow</span> = <span style="color: #800080;">$_GET</span>['pageNow'<span style="color: #000000;">];
    }
    
    </span><span style="color: #800080;">$pageCount</span> = 0;   <span style="color: #008000;">//</span><span style="color: #008000;">表示共有多少页</span>
    
    <span style="color: #800080;">$sql1</span> = "select count(id) from user"<span style="color: #000000;">;
    </span><span style="color: #800080;">$res1</span> = <span style="color: #008080;">mysql_query</span>(<span style="color: #800080;">$sql1</span><span style="color: #000000;">);
    
    </span><span style="color: #0000ff;">if</span>(<span style="color: #800080;">$row1</span>=<span style="color: #008080;">mysql_fetch_row</span>(<span style="color: #800080;">$res1</span><span style="color: #000000;">)){
        </span><span style="color: #800080;">$rowCount</span> = <span style="color: #800080;">$row1</span>[0<span style="color: #000000;">];
    }
    
    </span><span style="color: #008000;">//</span><span style="color: #008000;">计算共有多少页,ceil取进1</span>
    <span style="color: #800080;">$pageCount</span> = <span style="color: #008080;">ceil</span>((<span style="color: #800080;">$rowCount</span>/<span style="color: #800080;">$pageSize</span><span style="color: #000000;">));
    
    </span><span style="color: #008000;">//</span><span style="color: #008000;">使用sql语句时,注意有些变量应取出赋值。</span>
    <span style="color: #800080;">$pre</span> = (<span style="color: #800080;">$pageNow</span>-1)*<span style="color: #800080;">$pageSize</span><span style="color: #000000;">;
    
    </span><span style="color: #800080;">$sql2</span> = "select * from user limit <span style="color: #800080;">$pre</span>,<span style="color: #800080;">$pageSize</span>"<span style="color: #000000;">;
    </span><span style="color: #800080;">$res2</span> = <span style="color: #008080;">mysql_query</span>(<span style="color: #800080;">$sql2</span><span style="color: #000000;">);</span>
  
   <span style="color: #0000ff;">while</span>(<span style="color: #800080;">$row</span>=<span style="color: #008080;">mysql_fetch_assoc</span>(<span style="color: #800080;">$res2</span><span style="color: #000000;">)){
        </span><span style="color: #0000ff;">echo</span> <span style="color: #800080;">$row</span>['user_name']."<br>"<span style="color: #000000;">;
        </span><span style="color: #0000ff;">echo</span> <span style="color: #800080;">$row</span>['name']."<br>"<span style="color: #000000;">;
        </span><span style="color: #0000ff;">echo</span> <span style="color: #800080;">$row</span>['email']."<br>"<span style="color: #000000;">;
        </span><span style="color: #0000ff;">echo</span> <span style="color: #800080;">$row</span>['password']."<br>"<span style="color: #000000;">;
        </span><span style="color: #0000ff;">echo</span> <span style="color: #800080;">$row</span>['tel']."<br>"<span style="color: #000000;">;
   }
    </span><span style="color: #0000ff;">for</span> (<span style="color: #800080;">$i</span>=1;<span style="color: #800080;">$i</span><=<span style="color: #800080;">$pageCount</span>;<span style="color: #800080;">$i</span>++<span style="color: #000000;">){
        </span><span style="color: #0000ff;">echo</span> "<a href='userList.php?pageNow=<span style="color: #800080;">$i</span>'><span style="color: #800080;">$i</span></a> "<span style="color: #000000;">;
    }
</span>?>
</body>
</html>
登录后复制

 

相关标签:
来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
热门推荐
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板