Home > Article > Backend Development > Silently talk about PHP&MYSQL paging principle and implementation_PHP tutorial
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, if they are all opened on one page, it will be a torture for both the browser and the viewer.
I believe that every novice learning PHP will have a headache with pagination, but with Momo’s post, you will definitely pat your head and say, hey, it turns out that pagination 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 1000 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
The above is a very simple mysql query statement. Its function is to extract 10 pieces of data from a table named table and put all The values of the fields are obtained.
The key part is in this section "limit 0,10". The 0 in it is 0 as the starting point, and the following 10 is to display 10 pieces of data, so we need to use 10 as the starting point. How to write the 20th piece of data displayed?
Maybe many people will 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.
Now that you know how to extract 10 pieces of data, 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 . Already? 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 according to 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:
Copy code