Home Backend Development PHP Tutorial Detailed explanation of PHP paging principle

Detailed explanation of PHP paging principle

Mar 16, 2017 pm 03:48 PM
php Pagination principle

Before reading this article, please make sure you have mastered some knowledge of PHP and the basics of MYSQL query operations.

As a Web program, it often has to deal with countless data, such as member data and article data. If there are only a few dozen members, it is easy to handle. It can be displayed on one page, but what if If your website has thousands or even hundreds of thousands of members, opening them all on one page will be a torture for both the browser and the viewer. Moreover, if there are hundreds of millions of data, querying them once from the database will put pressure on the server. It's a big deal, it's not the right way to do it.

I believe that every novice learning PHP will have a headache about paging, but with this silent post, you will definitely pat your head and say, hey, it turns out that paging is so simple? Indeed, please take a deep breath of fresh air now and listen carefully as Silence explains it to you bit by bit.

Suppose we want to process 1,000 pieces of data and display 10 pieces on each page. In this case, it will be displayed in 100 pages. Let's first take a look at how to extract 10 pieces of information in mysql.

Select * from table limit 0,10
Copy after login

The above is a very simple mysql query statement. Its function is to extract 10 pieces of data from a table named table and obtain the values ​​of all fields. The usage of limit 0,10 is: limit starting point, the amount to be extracted

The key part is in this paragraph "limit 0,10", where 0 is the starting point, and then 10 is to display 10 pieces of data, so we need to use 10 as the starting point, and how to write the 20th piece of data?

Perhaps a lot of people say "limit 10,20" outright! Oh, this is wrong. The correct way to write it is "limit 10,10". The parameter after it is not the end point but the number to be extracted. Remember.

Understand how to extract 10 pieces of data, then extracting 1,000 pieces means doing this kind of query 100 times, which means doing the following query:

Limit 0,10                                                             . Page
Limit 10,10                                                                                                                                                                                                                                                             Yet? Yes, the first parameter increases by 10 every time the page is turned, but the second parameter remains unchanged.
That is to say, if we try to change the value of the first parameter according to the number of pages, we can display the data in pages. How about it? Is the principle very simple?

But how to change the value of the first parameter based on the number of pages? First, we need to have a page number value, which can be obtained using the GET method of the URL.
For example, index.php?page=18
I believe most of you are familiar with this thing. This kind of URL address can be found everywhere. The function of the page parameter is to pass in the number of pages to be displayed.

Let’s take a look at how it is implemented through a piece of code:

<?php

/*

Author:默默
Date :2006-12-03

*/

$page=isset($_GET[&#39;page&#39;])?intval($_GET[&#39;page&#39;]):1;        //这句就是获取page=18中的page的值,假如不存在page,那么页数就是1。
$num=10;         //每页显示10条数据

$db=mysql_connect("host","name","pass");           //创建数据库连接
$select=mysql_select_db("db",$db);                 //选择要操作的数据库

/*
首先咱们要获取数据库中到底有多少数据,才能判断具体要分多少页,总页数 具体的公式就是
总数据数 除以 每页显示的条数,有余进一 。
也就是说10/3=3.3333=4 有余数就要进一。
*/

$total=mysql_num_rows(mysql_query("select * from table")); //查询数据的总数total
$pagenum=ceil($total/$num);      //获得总页数 pagenum

//假如传入的页数参数apge 大于总页数 pagenum,则显示错误信息
If($page>$pagenum || $page == 0){
       Echo "Error : Can Not Found The page .";
       Exit;
}

$offset=($page-1)*$num;         //获取limit的第一个参数的值 offset ,假如第一页则为(1-1)*10=0,第二页为(2-1)*10=10。             (传入的页数-1) * 每页的数据 得到limit第一个参数的值

$info=mysql_query("select * from table limit $offset,$num ");   //获取相应页数所需要显示的数据
While($it=mysql_fetch_array($info)){
       Echo $it[&#39;name&#39;]."<br />";
}                                                              //显示数据

For($i=1;$i<=$pagenum;$i++){

       $show=($i!=$page)?"<a href=&#39;index.php?page=".$i."&#39;>$i</a>":"<b>$i</b>";
       Echo $show." ";
}

/*显示分页信息,假如是当页则显示粗体的数字,其余的页数则为超连接,假如当前为第三页则显示如下
1 2 3 4 5 6
*/
?>
Copy after login

If you read the above code carefully, replace the database connection and query tables with yours, Then you can see its execution effect.

Isn’t it very simple? Just use your brain to make it display more personalized. Let me give you a small question: how to implement the format of "Homepage, Previous Page, Next Page, Last Page" What about paging?

Summary:

Prototype: Select * from table limit 0,10

Program: select * from table limit $offset,$num ($offset value is: pass The number of pages entered - 1 $num is the data displayed on each page, mostly a fixed constant value) Total number of pages: % of total data The number of items displayed on each page, if there is any excess int totalPage=( (totalCount%NUM)==0)?totalCount/NUM:totalCount/NUM+1;
limit usage: limit starting point, the number to be extracted

But it should be noted that: must be added order by , make sure to query in ascending or descending order, otherwise you will not know which direction to start the query when querying. But be sure to pay attention to the order: the correct one is select * from user order by id desc limit 0,10;

For more detailed explanations of PHP paging principles and related articles, please pay attention to the PHP Chinese website!

Related articles:

Use PHP to implement simple paging class and its detailed usage

php paging PHP paging display production detailed explanation

php paging class code

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 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
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
1668
14
PHP Tutorial
1273
29
C# Tutorial
1256
24
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

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 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 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.

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 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.

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

See all articles