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, 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 statement is "limit 10, 10". The parameter after it is not the end point,Number of entries 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, weTry to change the value of the first parameter based on the number of pagesYou 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:
[Php]
<? Php
/*
Author: silent
Date: 2006-12-03
*/
$ Page = isset ($ _ GET ['page'])? Intval ($ _ GET ['page']): 1; // obtain the value of the page in page = 18. If there is no page, the page number is 1.
$ Num = 10; // 10 data entries per page
$ Db = mysql_connect ("host", "name", "pass"); // create a database connection
$ Select = mysql_select_db ("db", $ db); // select the database to operate
/*
First, we need to obtain the actual amount of data in the database to determine the specific number of pages to be divided,Total number of pagesThe specific formula is
Total number of data divided by the number of lines displayed per page, more than one .
That is to say, 10/3 = 3.3333 = 4 there is a remainder, and we need to enter.
*/
$ Total = mysql_num_rows (mysql_query ("select * from table"); // total number of queried data
$ Pagenum = ceil ($ total/$ num); // obtain the total number of pages pagenum
// If the input page number parameter apge is greater than the total page number pagenum, the error message is displayed.
If ($ page> $ pagenum | $ page = 0 ){
Echo "Error: Can Not Found The page .";
Exit;
}
$ Offset = ($ page-1) * $ num;// Obtain 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. (Number of incoming pages-1) * data per pageObtain the value of the first parameter of limit.
$ 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']. "<br/> ";
} // Display data
For ($ I = 1; $ I <= $ pagenum; $ I ++ ){
$ Show = ($ I! = $ Page )? "<A href = 'index. php? Page = ". $ I." '> $ I </a> ":" <B> $ I </B> ";
Echo $ show ."";
}
/* Display the page information. If the page is displayed, the numbers in bold are displayed, and the remaining pages are hyperconnections. If the page is currently on the third page, the following information is displayed:
1 2 3 4 5 6
*/
?>
[/Php]
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?
OK, fill in the water post, and close the job. Pai_^ quiet talk about the paging Principle and Implementation of PHP and MYSQL
==========================================
Summary:
Prototype:Select * from table limit 0, 10
Program:Select * from table limit $ offset, $ num ($ offset value:Incoming page count-1$ Num is the data displayed on each page, mostly fixed constant values)
Total number of pages:Total data%Number of entries per page,Yujin
Int totalPage = (totalCount % NUM) = 0 )? TotalCount/NUM: totalCount/NUM + 1;
Limit usage:Limit start point, number to be extracted
However, you must addOrder, Determine the ascending or descending order to query. Otherwise, you will not know where to start the query. But pay attention to the order: the correct one isSelect * from user order by id desc limit 0, 10;