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

PHP实现数据分页显示

May 26, 2016 am 08:19 AM

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

  实例代码如下:

<!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>
登录后复制

 

本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

AI Hentai Generator

AI Hentai Generator

免费生成ai无尽的。

热门文章

R.E.P.O.能量晶体解释及其做什么(黄色晶体)
2 周前 By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳图形设置
2 周前 By 尊渡假赌尊渡假赌尊渡假赌

热工具

记事本++7.3.1

记事本++7.3.1

好用且免费的代码编辑器

SublimeText3汉化版

SublimeText3汉化版

中文版,非常好用

禅工作室 13.0.1

禅工作室 13.0.1

功能强大的PHP集成开发环境

Dreamweaver CS6

Dreamweaver CS6

视觉化网页开发工具

SublimeText3 Mac版

SublimeText3 Mac版

神级代码编辑软件(SublimeText3)