In fact, the paging principle is similar in different languages, mainly because the expressions of programs and databases are different. Before reading this article, make sure that you have mastered some PHP knowledge and the basics of MYSQL Query operations.
As a Web program, you often have to deal with countless pieces of data, such as member data and article data. if there are only a few dozen members, you can easily display them on one page, however, if your website is thousands or even hundreds of thousands of members, if you open it on one page, both browser and viewer will be suffering, and if the data is hundreds of millions, if you query the database once, the pressure on the server is very high. this is not the correct method.
I believe that every beginner who learns PHP will feel a headache for paging, but with this silent post, you will surely pat your head and say, hey, is paging so simple? Indeed, please take a deep breath of fresh air and listen carefully to give you a 1.1-point breakdown.
Suppose we want to process 1000 pieces of data and display 10 pieces of data on each page. this will be displayed on 100 pages, let's take a look at how 10 pieces of information are extracted from mysql.
Select * from table limit 0,10
The above is a simple mysql Query statement, which extracts 10 pieces of data from a table and obtains the values of all fields. Here, the limit usage is: limit start point, the number to be extracted
The key point is in this section "limit", where 0 is the starting point of 0, and 10 is the display of 10 data records, so we should start with 10, how can I write 20th data records?
There may be a lot of people who say "limit 10, 20" right away! Ah, this is wrong. the correct method is "limit 10, 10". The parameter next to it is not the end point but the number to be extracted. remember.
If you know how to extract 10 pieces of data, extract 1000 pieces of data, that is, perform the following query:
Limit 0, 10 // page 1
Limit 10, 10 // page 2
Page 20, 10 // third
Limit 30, 10 // page 4
......
Are there any rules? Yes, the first parameter is increased by 10 every time you flip the page, but the second parameter remains unchanged.
That is to say, if we try to change the value of the first parameter based on the number of pages, we can display the data by page. how is the principle simple?
But how can we change the value of the first parameter based on the number of pages? First, we need to have a page number value, which is obtained using the url GET method.
For example, index. php? Page = 18
I believe most of them are familiar with this topic. this url can be seen everywhere. the page parameter is used to input the page number to be displayed.
Let's use a piece of code to see how it is implemented:
$ Pagenum | $ page = 0) {Echo "Error: Can Not Found The page. "; Exit;} $ offset = ($ page-1) * $ num; // get the value offset of the first parameter of limit. if the first page is (1-1) * 10 = 0. The second page is (2-1) * 10 = 10. (Incoming page number-1) * The value of the first parameter of limit is obtained for each page of Data $ info = mysql_query ("select * from table limit $ offset, $ num "); // obtain the data to be displayed for the corresponding page number While ($ it = mysql_fetch_array ($ info) {Echo $ it ['name']."
";}// Display Data For ($ I = 1; $ I <=$ pagenum; $ I ++) {$ show = ($ I! = $ Page )? "$ I ":"$ I"; Echo $ show. "";}/* Display page information. if the page displays a bold number, the remaining pages are hyperconnections, if the current page is the third page, the following figure is displayed: 1 2 3 4 5 6 */?>
If you carefully read the above code and replace the database connection and query table with yours, you can see the execution result.
Isn't it easy? just use your brains to make it more personalized and give you a question. how can we achieve pagination in the format of "The last page of the previous page on the homepage?
Summary:
Prototype: Select * from table limit 0, 10
Program: select * from table limit $ offset, $ num ($ offset value: Number of incoming pages-1 $ num is the data displayed on each page, mostly fixed constant values)
Total number of pages: total data % Number of items displayed per page, more than one
Int totalPage = (totalCount % NUM) = 0 )? TotalCount/NUM: totalCount/NUM + 1;
Limit Usage: limit start point, number to be extracted
However, you must add order by to determine the ascending or descending order. Otherwise, you will not know where to start the query. But pay attention to the order: the correct is select * from user order by id desc limit;
For more details about the principles of php paging, refer to the Chinese PHP website!
Related articles:
Use PHP to implement simple paging classes and detailed usage
Php paging PHP paging display production details
Php paging code