Home  >  Article  >  Backend Development  >  PHP implements data paging to display detailed operations

PHP implements data paging to display detailed operations

王林
王林forward
2019-08-23 16:34:032899browse

Paging is a frequently used function in background management. Paging display facilitates the management of large amounts of data.

The example code is as follows:

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>用户列表</title>
</head>
<body>
    <?php 
        $con = mysqli_connect("localhost","root","");   
        mysqli_query("set names utf8");    mysqli_select_db("zhiye",$con);    
        $pageSize = 1;      //每页显示的数量
        $rowCount = 0;      //要从数据库中获取
        $pageNow = 1;        //当前显示第几页
        //如果有pageNow就使用,没有就默认第一页。
        if (!empty($_GET[&#39;pageNow&#39;])){        
            $pageNow = $_GET[&#39;pageNow&#39;];
        }
        $pageCount = 0;   //表示共有多少页
        $sql1 = "select count(id) from user";    
        $res1 = mysqli_query($sql1);
        if($row1=mysqli_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 = mysqli_query($sql2);
        while($row=mysqli_fetch_assoc($res2)){    
            echo $row[&#39;user_name&#39;]."<br>";       
            echo $row[&#39;name&#39;]."<br>";       
            echo $row[&#39;email&#39;]."<br>";      
            echo $row[&#39;password&#39;]."<br>";  
            echo $row[&#39;tel&#39;]."<br>";
             }
            for ($i=1;$i<=$pageCount;$i++){  
              echo "<a href=&#39;userList.php?pageNow=$i&#39;>$i</a> ";
            }?>
</body>
</html>

When there is a large amount of data, the above method cannot be used

<?php 
    $con = mysqli_connect("localhost","root","");
   
    mysqli_query("set names utf8");
    mysqli_select_db("zhiye",$con);
    
    $pageSize = 1;      //每页显示的数量
    $rowCount = 0;      //要从数据库中获取
    $pageNow = 1;        //当前显示第几页
    
    //如果有pageNow就使用,没有就默认第一页。
    if (!empty($_GET[&#39;pageNow&#39;])){
        $pageNow = $_GET[&#39;pageNow&#39;];
    }
    
    $pageCount = 0;   //表示共有多少页
    
    $sql1 = "select count(id) from user";
    $res1 = mysqli_query($sql1);
    
    if($row1=mysqli_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 = mysqli_query($sql2);
    
    //$sql = "select * from user";
    //$res = mysqli_query($sql,$con);
  
   while($row=mysqli_fetch_assoc($res2)){
        echo $row[&#39;user_name&#39;]."<br>";
        echo $row[&#39;name&#39;]."<br>";
        echo $row[&#39;email&#39;]."<br>";
        echo $row[&#39;password&#39;]."<br>";
        echo $row[&#39;tel&#39;]."<br>";
   }
   if($pageNow>1){
       $prePage = $pageNow-1;
       echo "<a href=&#39;userList.php?pageNow=$prePage&#39;>pre</a> ";
   }
   if($pageNow<$pageCount){
       $nextPage = $pageNow+1;
       echo "<a href=&#39;userList.php?pageNow=$nextPage&#39;>next</a> ";
       echo "当前页{$pageNow}/共{$pageCount}页";
   }
   echo "<br/><br/>";
   ?>
 
   <form action="userList.php">
        <input type="text" name="pageNow">
        <input type="submit" value="GO">
   </form>

For more related questions, please visit the PHP Chinese website: PHP Video Tutorial

The above is the detailed content of PHP implements data paging to display detailed operations. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:cnblogs.com. If there is any infringement, please contact admin@php.cn delete