Home  >  Article  >  Backend Development  >  PHP paging principle Paging code Detailed explanation of paging class production method examples

PHP paging principle Paging code Detailed explanation of paging class production method examples

墨辰丷
墨辰丷Original
2018-06-01 10:22:241249browse

This article mainly introduces the PHP paging principle, PHP paging code, and PHP paging class production tutorial in detail. It has a certain reference value. Interested friends can refer to it.

Paging display is A very common way to browse and display large amounts of data, and one of the most commonly handled 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 sections for display. Two steps are required here. Initial parameters:

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
    11th to 20th records:  select * from table limit 10,10
    21st to 30th 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
我们 The corresponding value is substituted with the 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>

3. Simple paging category sharing

Now announce the production 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 operation 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;); 
   } 
} 
 
?>

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>"; 
 } 
 
 ?>

Summary: The above is the entire content of this article, I hope it will be helpful to everyone's study.

Related recommendations:

PHP Upload Excel file and import data to MySQL database

phpThrow Detailed explanation of exceptions and catching specific types of exceptions

##php array_merge_recursive Array merge

The above is the detailed content of PHP paging principle Paging code Detailed explanation of paging class production method examples. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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