Home > php教程 > php手册 > PHP实现数据分页显示的简单实例,php分页显示实例

PHP实现数据分页显示的简单实例,php分页显示实例

WBOY
Release: 2016-06-13 08:38:09
Original
1086 people have browsed it

PHP实现数据分页显示的简单实例,php分页显示实例

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

实例代码如下:

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

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

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

以上这篇PHP实现数据分页显示的简单实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持帮客之家。

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template