Detailed explanation of PHP paging principle

高洛峰
Release: 2023-03-04 20:46:02
Original
1773 people have browsed it

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

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