Implementation principle of php paging

黄舟
Release: 2023-03-17 10:38:01
Original
2096 people have browsed it

In our last article, we introduced the effect of paging in PHP, so what is the principle of paging? Today I will take you to analyze the principle of PHP paging.

Paging display is a very common method of browsing and displaying large amounts of data, and is one of the most commonly processed events in web programming. For veterans of web programming, writing this kind of code is as natural as breathing, but for beginners, they are often confused about this issue, so I specially wrote this article to explain this issue in detail.

1. Paging principle:

The so-called paging display means that the result set in the database is artificially divided into segments for display. Two initial parameters are required here:

How many records per page ($PageSize)?
What page is the current page ($CurrentPageID)?

Now as long as you give me another result set, I can display a specific result.

As for other parameters, such as: previous page ($PReviousPageID), next page ($NextPageID), total number of pages ($numPages), etc., they can all be obtained based on the previous things.

Taking the MySQL database as an example, if you want to intercept a certain piece of content from the table, the sql statement can be used: select * from table limit offset, rows. Take a look at the following set of SQL statements and try to find the rules.

          The first 10 records: select * from table limit 0,10
    The 11th to 20th records:  select * from table limit 10,10
      The 21st to 30th records Records: select * from table limit 20,10
……
This set of sql statements is actually the sql statement that fetches each page of data in the table when $PageSize=10. We can summarize such a template:
SELECT * From Table Limit ($ CurrenTPageid -1) * $ PageSize, $ PageSize
to the corresponding value with the corresponding SQL statement above. That's not the case. After solving the most important problem of how to obtain the data, all that is left is to pass the parameters, construct the appropriate SQL statement and then use PHP to obtain the data from the database and display it.

2. Paging code description: five steps

The code is fully explained and can be copied to your own notepad for direct use


<html>
<head>
<meta http-equiv="content-type" content="text/html;charset=utf-8" />
<title>雇员信息列表</title>
</head>
<?php 
    //显示所有emp表的信息
    //1.连接数据库
    $conn=mysql_connect(&#39;localhost&#39;,&#39;root&#39;,&#39;1234abcd&#39;) or die(&#39;连接数据库错误&#39;.mysql_error());
    //2.选择数据库
    mysql_select_db(&#39;empManage&#39;);
   //3.选择字符集
    mysql_query(&#39;set names utf8&#39;);
   //4.发送sql语句并得到结果进行处理
    //4.1分页[分页要发出两个sql语句,一个是获得$rowCount,一个是通过sql的limit获得分页结果。所以我们会获得两个结果集,在命名的时候要记得区分。
分页 (四个值 两个sql语句)。]
  $pageSize=3;//每页显示多少条记录
   $rowCount=0;//共有多少条记录
    $pageNow=1;//希望显示第几页
    $pageCount=0;//一共有多少页 [分页共有这个四个指标,缺一不可。由于$rowCount可以从服务器获得的,所以可以给予初始值为0;
$pageNow希望显示第几页,这里最好是设置为0;$pageSize是每页显示多少条记录,这里根据网站需求提前制定。
.$pageCount=ceil($rowCount/$pageSize),既然$rowCount可以初始值为0,那么$pageCount当然也就可以设置为0.四个指标,两个0 ,一个1,另一个为网站需求。]
         //4.15根据分页链接来修改$pageNow的值
         if(!empty($_GET[&#39;pageNow&#39;])){
            $pageNow=$_GET[&#39;pageNow&#39;];
        }[根据分页链接来修改$pageNow的值。]
     $sql=&#39;select count(id) from emp&#39;;
     $res1=mysql_query($sql);
    //4.11取出行数
     if($row=mysql_fetch_row($res1)){
        $rowCount=$row[0];        
    }//[取得$rowCount,,进了我们就知道了$pageCount这两个指标了。]
    //4.12计算共有多少页
     $pageCount=ceil($rowCount/$pageSize);
    $pageStart=($pageNow-1)*$pageSize;
    
     //4.13发送带有分页的sql结果
     $sql="select * from emp limit $pageStart,$pageSize";//[根据$sql语句的limit 后面的两个值(起始值,每页条数),来实现分页。以及求得这两个值。]
    $res2=mysql_query($sql,$conn) or die(&#39;无法获取结果集&#39;.mysql_error());
     echo &#39;<table border=1>&#39;;[    echo "<table border=&#39;1px&#39; cellspacing=&#39;0px&#39; bordercolor=&#39;red&#39; width=&#39;600px&#39;>";]
 "<tr><th>id</th><th>name</th><th>grade</th><th>email</th><th>salary</th><th><a href=&#39;#&#39;>删除用户</a></th><th><a href=&#39;#&#39;>修改用户</a></th></tr>";    
 while($row=mysql_fetch_assoc($res2)){
        echo "<tr><td>{$row[&#39;id&#39;]}</td><td>{$row[&#39;name&#39;]}</td><td>{$row[&#39;grade&#39;]}</td><td>{$row[&#39;email&#39;]}</td>
        <td>{$row[&#39;salary&#39;]}</td><td><a href=&#39;#&#39;>删除用户</a></td><td><a href=&#39;#&#39;>修改用户</a></td></tr>";    }
     echo &#39;</table>&#39;;
     //4.14打印出页码的超链接
     for($i=1;$i<=$pageCount;$i++){
         echo "<a href=&#39;?pageNow=$i&#39;>$i</a> ";//[打印出页码的超链接]
     
     }
     //5.释放资源,关闭连接
     mysql_free_result($res2);
    mysql_close($conn);
?>
</html>
Copy after login

3. Simple paging category sharing

Now we announce the creation of a simple category. As long as you understand the principles and steps of this class, you will be able to understand other complex classes by analogy. No nonsense, just upload the source code and you can use it directly in your project.

Database operationClass code: mysqli.func.php

<?php 
// 数据库连接常量 
 define(&#39;DB_HOST&#39;, &#39;localhost&#39;); 
 define(&#39;DB_USER&#39;, &#39;root&#39;); 
 define(&#39;DB_PWD&#39;, &#39;&#39;); 
 define(&#39;DB_NAME&#39;, &#39;guest&#39;); 
  
 // 连接数据库 
 function conn() 
 { 
   $conn = mysqli_connect(DB_HOST, DB_USER, DB_PWD, DB_NAME); 
   mysqli_query($conn, "set names utf8"); 
  return $conn; 
} 
 
//获得结果集 
function doresult($sql){ 
 $result=mysqli_query(conn(), $sql); 
  return $result; 
 } 
 
 //结果集转为对象集合 
 function dolists($result){ 
  return mysqli_fetch_array($result, MYSQL_ASSOC); 
 } 

 function totalnums($sql) { 
  $result=mysqli_query(conn(), $sql); 
 return $result->num_rows; 
 } 
 // 关闭数据库 
 function closedb() 
 { 
   if (! mysqli_close()) { 
    exit(&#39;关闭异常&#39;); 
   } 
} 
?>
Copy after login

Paging implementation code:

<?php 
 include &#39;mysqli.func.php&#39;; 
 // 总记录数 
 $sql = "SELECT dg_id FROM tb_user "; 
 $totalnums = totalnums($sql); 
  
 // 每页显示条数 
 $fnum = 8; 
 
 // 翻页数 
 $pagenum = ceil($totalnums / $fnum); 
 
 //页数常量 
 @$tmp = $_GET[&#39;page&#39;]; 
  
 //防止恶意翻页 
 if ($tmp > $pagenum) 
   echo "<script>window.location.href=&#39;index.php&#39;</script>"; 
  
 //计算分页起始值 
 if ($tmp == "") { 
  $num = 0; 
} else { 
  $num = ($tmp - 1) * $fnum; 
 } 
// 查询语句 
 $sql = "SELECT dg_id,dg_username FROM tb_user ORDER BY dg_id DESC LIMIT " . $num . ",$fnum"; 
 $result = doresult($sql); 
 
 // 遍历输出 
 while (! ! $rows = dolists($result)) { 
   echo $rows[&#39;dg_id&#39;] . " " . $rows[&#39;dg_username&#39;] . "<br>"; 
 } 

 // 翻页链接 
 for ($i = 0; $i < $pagenum; $i ++) { 
   echo "<a href=index.php?page=" . ($i + 1) . ">" . ($i + 1) . "</a>"; 
 }
 ?>
Copy after login

Summary:

After reading the details of this chapter, everyone has a certain understanding of the principle of PHP paging. When we clarify the principle, then the implementation of paging is not a problem. At work It can also be used like a duck in water!

related suggestion:

#Example of php to achieve paging effect


How Write PHP paging code


php perfect paging method

The above is the detailed content of Implementation principle of php paging. For more information, please follow other related articles on the PHP Chinese website!

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 Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!