PHP page flip function implementation

Source: Internet
Author: User
This article describes how to implement the PHP page flip function. The following describes how to implement the current page data display quantity and how to implement the dynamic page flip function. For more information, see

This article describes how to implement the PHP page flip function. The following describes how to implement the current page data display quantity and how to implement the dynamic page flip function. For more information, see

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 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:

With the above ideas, you can start designing the program.

Page. php:

<? // Set the quantity displayed on the current page (this quantity can be set as needed) $ limit = 20; // initialize the database search start record if (! Emptyempty ($ 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 (! Emptyempty ($ 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 previous page flip $ prve = $ start-$ limit; if ($ prve> = 0) {echo" prve ";} // set the jump to the back page to $ next = $ start + $ limit; if ($ next <$ num_max) {echo "next" ;}echo"
";?>

A front flip and back flip function is completed, but this function will be very cumbersome for processing more information 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;

The total number of database records is:

$ 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 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 to mark the variable. This variable is used to control the number loop page turning control. Now you can see the complete code page for implementing loop page turning. php:

<? $ Limit = 25; if (! Emptyempty ($ start) $ start = 0; if (! Emptyempty ($ 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 (! Emptyempty ($ numb) {for ($ I = 0; $ I <$ numb; $ I ++) {$ val = mysql_result ($ result, $ I, "val"); $ val1 = mysql_result ($ result, $ I, "val1"); echo" ";}} Echo"
$ Val $ Val1
"; // Digital loop paging control echo" "; Echo" "; // Forward flip control if ($ s> 20) {if ($ s = 21) {$ st = $ S-21;} else {$ st = $ S-20 ;} $ pstart = $ st * $ limit; echo" ";} Echo" "; // Set the page number of the current page without link function $ 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" "; // Controls the limited display function of the digital 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" ";}// Note break for loop exit control;} echo"
Page:Prve>"; If ($ page! = $ Star/$ limit) {echo "";} echo $ page; if ($ page! = $ Star/$ limit) {echo "";} echo"< Next
";?>

Another page flip function is to submit pages, that is, to add data to the submission form, and then the program jumps to the corresponding page. This function is easy to implement and left for the reader to complete.

The above procedures have already been able to complete the powerful loop paging function, you can carefully study, truly learn to use.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.