Home Backend Development PHP Tutorial 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

Jun 01, 2018 am 10:22 AM
php code principle

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>
Copy after login

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;); 
   } 
} 
 
?>
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: 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 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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1665
14
PHP Tutorial
1269
29
C# Tutorial
1249
24
PHP and Python: Comparing Two Popular Programming Languages PHP and Python: Comparing Two Popular Programming Languages Apr 14, 2025 am 12:13 AM

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP in Action: Real-World Examples and Applications PHP in Action: Real-World Examples and Applications Apr 14, 2025 am 12:19 AM

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

PHP: A Key Language for Web Development PHP: A Key Language for Web Development Apr 13, 2025 am 12:08 AM

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

The Enduring Relevance of PHP: Is It Still Alive? The Enduring Relevance of PHP: Is It Still Alive? Apr 14, 2025 am 12:12 AM

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

PHP vs. Python: Understanding the Differences PHP vs. Python: Understanding the Differences Apr 11, 2025 am 12:15 AM

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

PHP and Python: Code Examples and Comparison PHP and Python: Code Examples and Comparison Apr 15, 2025 am 12:07 AM

PHP and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.

PHP vs. Other Languages: A Comparison PHP vs. Other Languages: A Comparison Apr 13, 2025 am 12:19 AM

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

PHP and Python: Different Paradigms Explained PHP and Python: Different Paradigms Explained Apr 18, 2025 am 12:26 AM

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

See all articles