We all know that using php + MySQL to display all database information on the web page is very simple and interesting. when there are few database information, the page is still satisfactory, however, when there are a lot of database information, the page display will become very bad, the following describes how to display the data volume on the current page and how to implement the dynamic flip function. here we will introduce the implementation of two paging display functions: ------------------- we all know that using php + MySQL to display all database information on the web page is very simple and interesting, when there are few database materials, the page display is satisfactory. However, when there are many database materials, the page display will be very bad, the following describes how to display the data volume on the current page and how to implement the dynamic flip function.
Here we will introduce the implementation of two page flip display functions:
-----------------------------------------------------------
First, we will introduce the database syntax used in Paging:
Mysql_query ("select * from table order by id desc ");
This database statement is no longer familiar. it is used to search records and display them in reverse order, but does not play a role in the paging function, the extended syntax below is the core function of page flip:
Mysql_query ("select * from table order by id desc limit $ start, $ limit ");
Here, $ start is the starting row for database search, and $ limit is the starting row to search for $ limit Records. well, with this core function, we can start the page flip function;
-----------------------------------------------------------
The first page flip function:
The features described here are the simplest of the page flip function. you can only flip the pages forward and backward. this is the paging function of the news and download center on this site.
The following describes how to implement the paging function:
First, determine the number of data records displayed on the current page. for example, 20 records are displayed. set the value of $ limit to 20: $ limit = 20;
When displaying database records, it must start from the first entry. Therefore, the initial value of $ start is set to 0: $ start = 0;
The implementation of the page flip function depends on the dynamic changes of $ start. when the page is turned back, $ limit: $ start + $ limit is added to the $ start rule; when you flip the page forward, $ start is regularly subtracted from $ limit: $ start-$ limit;
With the above ideas, you can start designing the page. php program:
// Set the quantity displayed on the current page (this quantity can be set as needed)
$ Limit = 20;
// Initialize the Start Record of the database search
If (! Empty ($ start) $ start = 0;
Mysql_connect ("localhost ","","");
Mysql_select_db (database );
// Set the total number of database records
$ Result = mysql_query ("select * from table ");
$ Num_max = mysql_numrows ($ result );
$ Result = mysql_query ("select * from table order by id desc limit $ start, $ limit );
$ Num = mysql_numrows ($ result );
Echo"
If (! Empty ($ num )){
For ($ I = 0; $ I <$ num; $ I ++ ){
$ Val = mysql_result ($ result, $ I, "val ");
$ Val1 = mysql_result ($ result, $ I, "val1 ");
Echo"
Echo"
Page Flip function |
";
$ Val |
$ Val1 |
";}}
"; // Set the jump to the forward page $ PRve = $ start-$ limit; If ($ prve> = 0 ){ Echo "prve "; } // Set the jump to the back page $ Next = $ start + $ limit; If ($ next <$ num_max ){ Echo "next "; } Echo" |
";
?>
A front flip and a back flip are completed. however, this function will be cumbersome to process more data display. next we will continue to introduce the more powerful and more complex page turning feature-loop page turning (I have always called this because I cannot find a more appropriate name ). :)
The previous section introduced the simple page flip function implementation. the page flip function described below is more powerful and complex. This function is used in many forums and articles on this site.
Loop paging is implemented by adding numbers to the front and back pages. the specific manifestation is as follows:
Page: prve <1 2 3 4 ...... 20> next
The numbers in it indicate the current pages. the prve and next operations are not only the front and back pages of the current page, but more complex numeric control.
As in the past, before program design, I should clarify my ideas. I suggest that you do it yourself after reading how to implement the function of turning pages cyclically, some methods and ideas studied here may be abstract.
First, we boldly assume that there are more than 1000 records in the database. we want to display 25 records, and the number flip control is 20. Therefore, the following result is displayed:
Page: 0 1 2 3...> next
The following result is displayed:
Page: prve <20 27 28 ...... 49> next
Well, let's take a look at the rule. a fixed number 25 is displayed, and a fixed number is doubled by 20. we can use these two numbers to implement the cyclic page flip function;
First, set the fixed display variable:
$ Limit = 20;
Database initial variable settings:
$ Start = 0;
Total database records: $ num;
Page number variable: $ page;
The procedure for loop display of a page number is as follows:
...
$ Result = mysql_query ("select * from table ");
$ Num = mysql_numrows ($ result );
For ($ page = 0; $ page <($ num/$ limit); $ page ++ ){
Echo $ page;
If ($ page> 0 & ($ page % 20) = 0 ){
Break; // exit the loop
}
}
?>
In addition to displaying numbers, this code does not implement other functions. because the number control is used to control the flip, there must be several variables to Mark and identify these controls, $ s is used here to mark; this variable is used to control the digital loop page turning control. now you can see the complete code page for implementing loop page turning. php:
$ Limit = 25;
If (! Empty ($ start) $ start = 0;
If (! Empty ($ s) $ s = 0;
Mysql_connect ("localhost ","","");
Mysql_select_db (database );
// Count the total number of database records
$ Result = mysql_query ("select * from table ");
$ Num = mysql_numrows ($ result );
$ Result = mysql_query ("select * from table order by id limit $ start, $ limit ");
$ Numb = mysql_numrows ($ result );
Echo"
";If (! Empty ($ numb )){For ($ I = 0; $ I <$ numb; $ I ++ ){$ Val = mysql_result ($ result, $ I, "val ");$ Val1 = mysql_result ($ result, $ I, "val1 ");Echo"
$ Val |
$ Val1 |
";}}Echo"
";
// Control of digital loop page turning
Echo"
// Forward flip control
If ($ s> 20 ){
If ($ s = 21 ){
$ St = $ S-21;
} Else {
$ St = $ S-20;
}
$ Pstart = $ st * $ limit;
Echo"
// Set no link to the page number of the current page
$ Star = $ start;
// Pay attention to the initial value of the loop. think carefully about why it is not 0.
For ($ page = $ s; $ page <($ num/$ limit); $ page ++ ){
$ Start = $ page * $ limit;
Echo"
// Control the restricted display function of the number page. only 20 pages are displayed.
If ($ page> 0 & ($ page % 20) = 0 ){
If ($ s = 0 ){
$ S = $ s + 21;
} Else {
$ S = $ s + 20;
}
$ Start = $ start + $ limit;
If ($ num/$ limit)-1)> $ page ){
Echo"
";Echo"
Page: | ";
Echo "start = $ pstart & s = $ st> prve | ";}Echo"
> | ";
"; If ($ page! = $ Star/$ limit ){ Echo "echo" start = $ start & s = $ s> "; } Echo $ page; If ($ page! = $ Star/$ limit ){ Echo ""; } Echo" | ";
< |
Echo "start = $ start & s = $ s> next | ";}// Note the loop jump controlBreak;}}Echo"
";
?>
The preceding steps are sorted to provide powerful cyclic page turning functions.
Another page flip function is to submit pages, that is, to add data for submission in the submission form, and then the program jumps to the corresponding page. This function is easy to implement and is left for the reader to complete, I will not introduce it here...