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

PHP实现数据分页显示

WBOY
Freigeben: 2016-05-26 08:19:54
Original
1585 Leute haben es durchsucht

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

  实例代码如下:



    <meta charset="utf-8">
    <title>用户列表</title>


<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>$pageCount;<span style="color: #800080;">$i</span>++<span style="color: #000000;">){
        </span><span style="color: #0000ff;">echo</span> "<a href="userList.php?pageNow=<span%20style=%22color:%20#800080;%22>%24i</span>"><span style="color: #800080;">$i</span></a> "<span style="color: #000000;">;
    }
</span>?>

Nach dem Login kopieren

 

Verwandte Etiketten:
Quelle:php.cn
Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Beliebte Empfehlungen
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage