Let's talk about the paging principle and implementation of PHP & amp; MYSQL. 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. for example, before reading this article, make sure that you have mastered some PHP knowledge and the basic 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, it will be a torment for both browser and viewer.
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.
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:
The code is as follows:
/*
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. the specific formula is
The total number of data divided by the number of items displayed on each 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"); // The total number of queried data
$ Pagenum = ceil ($ total/$ num); // obtain the total number of pages
// If the input page number parameter is greater than the total page number, 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 of the first parameter of limit. if the first page is (1-1) * 10 = 0, the second page is (2-1) * 10 = 10.
$ 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 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
*/
?>
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_^
The code is as follows:
/*
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 ("localhost", "root", "7529639"); // create a database connection
Mysql_select_db ("cr_download"); // 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. the specific formula is
Total databases divided by the number of entries displayed on each page, more than one.
That is to say, 10/3 = 3.3333 = 4 there is a remainder, and we need to enter.
*/
$ Result = mysql_query ("select * from cr_userinfo ");
$ Total = mysql_num_rows ($ result); // query all data
$ Url = 'Test. php'; // Obtain the URL of this page
// Page number calculation
$ Pagenum = ceil ($ total/$ num); // obtain the total number of pages, which is also the last page.
$ Page = min ($ pagenum, $ page); // Get the homepage
$ Prepg = $ page-1; // Previous page
$ Nextpg = ($ page = $ pagenum? 0: $ page + 1); // Next page
$ Offset = ($ page-1) * $ num; // obtain the value of the first parameter of limit. if the first page is (1-1) * 10 = 0, the second page is (2-1) * 10 = 10.
// Start the paging navigation bar code:
$ Pagenav = "display". ($ Total? ($ Offset + 1): 0 )."-". Min ($ offset + 10, $ total )."$ Total records ";
// If there is only one page, the function will jump out:
If ($ pagenum <= 1) return false;
$ Pagenav. = "homepage ";
If ($ prepg) $ pagenav. = "previous page"; else $ pagenav. = "previous page ";
If ($ nextpg) $ pagenav. = ""; else $ pagenav. = "";
$ Pagenav. = "Last page ";
// Pull-down Jump List, listing all page numbers cyclically:
$ Pagenav. ="\ N ";For ($ I = 1; $ I <= $ pagenum; $ I ++ ){If ($ I = $ page) $ pagenav. ="$ I\ N ";Else $ pagenav. ="$ I\ N ";}$ Pagenav. ="Page, total $ pagenum page ";
// If the input page number parameter is greater than the total page number, the error message is displayed.
If ($ page> $ pagenum ){
Echo "Error: Can Not Found The page". $ page;
Exit;
}
$ Info = mysql_query ("select * from cr_userinfo limit $ offset, $ num"); // obtain the data to be displayed on the corresponding page
While ($ it = mysql_fetch_array ($ info )){
Echo $ it ['username'];
Echo"
";
} // Display Data
Echo"
";
Echo $ pagenav; // output paging navigation
?>
By the way, in practical applications, pages are used for almost list-related items. you can try to create a paging universal function so that this function can be called wherever pages are needed, haha ~~
Bytes. As a Web program, you often have to deal with countless data, such as members...