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

PHP实现数据分页显示,php实现分页显示

WBOY
풀어 주다: 2016-06-13 08:39:08
원래의
1120명이 탐색했습니다.

PHP实现数据分页显示,php实现分页显示

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

  实例代码如下:

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

  当有大量数据时,就不能使用上述方法。

<?<span>php 
    </span><span>$con</span> = <span>mysql_connect</span>("localhost","root",""<span>);
   
    </span><span>mysql_query</span>("set names utf8"<span>);
    </span><span>mysql_select_db</span>("zhiye",<span>$con</span><span>);
    
    </span><span>$pageSize</span> = 1;      <span>//</span><span>每页显示的数量</span>
    <span>$rowCount</span> = 0;      <span>//</span><span>要从数据库中获取</span>
    <span>$pageNow</span> = 1;        <span>//</span><span>当前显示第几页
    
    //如果有pageNow就使用,没有就默认第一页。</span>
    <span>if</span> (!<span>empty</span>(<span>$_GET</span>['pageNow'<span>])){
        </span><span>$pageNow</span> = <span>$_GET</span>['pageNow'<span>];
    }
    
    </span><span>$pageCount</span> = 0;   <span>//</span><span>表示共有多少页</span>
    
    <span>$sql1</span> = "select count(id) from user"<span>;
    </span><span>$res1</span> = <span>mysql_query</span>(<span>$sql1</span><span>);
    
    </span><span>if</span>(<span>$row1</span>=<span>mysql_fetch_row</span>(<span>$res1</span><span>)){
        </span><span>$rowCount</span> = <span>$row1</span>[0<span>];
    }
    
    </span><span>//</span><span>计算共有多少页,ceil取进1</span>
    <span>$pageCount</span> = <span>ceil</span>((<span>$rowCount</span>/<span>$pageSize</span><span>));
    
    </span><span>//</span><span>使用sql语句时,注意有些变量应取出赋值。</span>
    <span>$pre</span> = (<span>$pageNow</span>-1)*<span>$pageSize</span><span>;
    
    </span><span>$sql2</span> = "select * from user limit <span>$pre</span>,<span>$pageSize</span>"<span>;
    </span><span>$res2</span> = <span>mysql_query</span>(<span>$sql2</span><span>);
    
    </span><span>//</span><span>$sql = "select * from user";
    //$res = mysql_query($sql,$con);</span>
  
   <span>while</span>(<span>$row</span>=<span>mysql_fetch_assoc</span>(<span>$res2</span><span>)){
        </span><span>echo</span> <span>$row</span>['user_name']."<br>"<span>;
        </span><span>echo</span> <span>$row</span>['name']."<br>"<span>;
        </span><span>echo</span> <span>$row</span>['email']."<br>"<span>;
        </span><span>echo</span> <span>$row</span>['password']."<br>"<span>;
        </span><span>echo</span> <span>$row</span>['tel']."<br>"<span>;
   }
   </span><strong><span>if</span>(<span>$pageNow</span>>1<span>){
       </span><span>$prePage</span> = <span>$pageNow</span>-1<span>;
       </span><span>echo</span> "<a href='userList.php?pageNow=<span>$prePage</span>'>pre</a> "<span>;
   }
   </span><span>if</span>(<span>$pageNow</span><<span>$pageCount</span><span>){
       </span><span>$nextPage</span> = <span>$pageNow</span>+1<span>;
       </span><span>echo</span> "<a href='userList.php?pageNow=<span>$nextPage</span>'>next</a> "<span>;
       </span><span>echo</span> "当前页{<span>$pageNow</span>}/共{<span>$pageCount</span>}页"<span>;
   }
   </span><span>echo</span> "<br/><br/>"<span>;
   </span>?>
 
   <form action="userList.php">
        <input type="text" name="pageNow">
        <input type="submit" value="GO">
   </form></strong>
로그인 후 복사

 

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