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

PHP实现数据分页显示

WBOY
풀어 주다: 2016-05-26 08:19:54
원래의
1585명이 탐색했습니다.

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

  实例代码如下:



    <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>?>

로그인 후 복사

 

관련 라벨:
원천:php.cn
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
최신 이슈
인기 추천
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿