This article mainly introduces the implementation of two page turning display functions in PHP. Interested friends can refer to it. I hope it will be helpful to everyone.
We all know that it is very simple and interesting to use php mysql to display all database information on a web page. When there is very little database information, the page display is still satisfactory, but when there is a lot of database information Next, the display of the page will become very bad. Let's introduce how to display the amount of data on the current page and how to implement the dynamic flip function.
Here we will introduce the implementation of two page turning display functions:
First introduce the database syntax used in page turning:
1 | mysql_query( "select * from table order by id desc" );
|
Copy after login
You will be familiar with this database statement However, it is used to search for records and display them in reverse order, but it does not work in the page turning function. The following extended syntax is the core function of page turning:
1 | mysql_query( "select * from table order by id desc limit $start,$limit" );
|
Copy after login
Here $start is the starting row of the database search, $limit is the search starting from the starting row and ending with $limit records. Well, with this core function, we can start the page turning function.
The first page turning function: The function introduced here is the simplest one among the page turning functions. It can only realize page turning forward and page turning backward. The function of this site This is the page turning function of the Very News and Download Center.
Let’s first introduce the idea of implementing the page turning function:
- First determine the number of data records fixedly displayed on the current page, assuming it is 20 records, set $ The value of limit is 20: $limit=20;
- When displaying database records, they must be displayed from the first one, so the initial value of $start is set here to 0:$start =0;
- The realization of the page turning function relies on the dynamic change of $start. When turning the page backward, $start regularly adds $limit:$start $limit; while going forward When turning pages, $start subtracts $limit regularly: $start-$limit;
After having the above ideas, you can start designing the program
page.php:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | <?
$limit =20;
if (!emptyempty( $start )) $start =0;
mysql_connect( "localhost" , "" , "" );
mysql_select_db(database);
$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 "<table><tr><td>翻页功能</td></tr>" ;
if (!emptyempty( $num )) {
for ( $i =0; $i < $num ; $i ++) {
$val =mysql_result( $result , $i , "val" );
$val1 =mysql_result( $result , $i , "val1" );
echo "<tr><td>$val</td><td>$val1</td></tr>" ;
}
}
echo "<tr><td>" ;
$prve = $start - $limit ;
if ( $prve >=0) {
echo "<a href=page.php?start=$prve>prve</a>" ;
}
$next = $start + $limit ;
if ( $next < $num_max ) {
echo "<a href=page.php?start=$next>next</a>" ;
}
echo "</td></tr></table>" ;
?>
|
Copy after login
A program with forward and backward functions has been completed, but this function will be very cumbersome when processing more data and displaying it. We will continue to introduce it below. A more powerful and complex page turning function - circular page turning (I have always called it this, because I can't find a more appropriate name).:)
The implementation of the simple page turning function was introduced earlier, and the following is introduced The page turning function is more powerful and complex. The special forums and articles on this site use this circular page turning function.
The circular page turning is achieved by turning forward and backward together with numbers. The specific expression is: :
Page: prve <<1 2 3 4 ....... 20 >> nextThe numbers inside represent each current Page, prve and next are not just forward and backward flips of the current page, but more complex digitally controlled front and back flips.
As always, before proceeding with program design, let’s clarify our ideas first. I suggest readers to practice it themselves after reading how to implement the page turning function, because some of the methods studied here are The ideas may be relatively abstract.
First of all, we boldly assume that there are more than 1,000 records in the database. We want to currently display 25 records, and the number flip control is 20, so the following display results will be shown:
Page : 0 1 2 3 ......... 19 >> nextThe displayed result after flipping:
page: prve < <20 27 28 ....... 49 >> nextOkay, let’s take a look at the rules. A fixed number displays the number 25, and a fixed number controls the doubling of 20. We can use these two numbers to implement the page turning function;First set the fixed display variable:
$limit=20;
Setting of the initial variable of the database:
$start= 0;
The total number of database records is:
$num;
Page variable:$page;
The program for a page number loop display is as follows:
1 2 3 4 5 6 7 8 9 10 11 12 | <?
...
$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 ;
}
}
?>
|
Copy after login
Except for displaying numbers, this code does not implement any other functions. Because there are more numbers to control flipping, several variables must be used to mark and identify these control quantities. $s is used to mark them here. This variable is used to To control digital cycle page turning, you can now look at the complete code page.php that implements cycle page turning:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 | <?
$limit =25;
if (!emptyempty( $start )) $start =0;
if (!emptyempty( $s )) $s =0;
mysql_connect( "localhost" , "" , "" );
mysql_select_db(database);
$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 "<table>" ;
if (!emptyempty( $numb )) {
for ( $i =0; $i < $numb ; $i ++) {
$val =mysql_result( $result , $i , "val" );
$val1 =mysql_result( $result , $i , "val1" );
echo "<tr><td>$val</td><td>$val1</td></tr>" ;
}
}
echo "</table>" ;
echo "<table>" ;
echo "<tr><td>页:</td>" ;
if ( $s >20) {
if ( $s ==21) {
$st = $s -21;
} else {
$st = $s -20;
}
$pstart = $st * $limit ;
echo "<td><a href=page.php?" ;
echo "start=$pstart&s=$st>prve</a></td>" ;
}
echo "<td> >></td>" ;
$star = $start ;
for ( $page = $s ; $page <( $num / $limit ); $page ++) {
$start = $page * $limit ;
echo "<td>" ;
if ( $page != $star / $limit ) {
echo "<a href=page.php?" ;
echo "start=$start&s=$s>" ;
}
echo $page ;
if ( $page != $star / $limit ) {
echo "</a>" ;
}
echo "</td>" ;
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 "<td> <<</td><td><a href'page.php?" ;
echo "start=$start&s=$s>next</a></td>" ;
}
break ;
}
}
echo "</tr></table>" ;
?>
|
Copy after login
There is also a page turning function that is to submit page turning, that is, add data submission in the submission form. The program then jumps to the corresponding page. This function is relatively simple to implement and is left to the reader to complete.
Summary: The above is the entire content of this article, I hope it will be helpful to everyone's study.
Related recommendations:
PHP recursive implementation of hierarchical tree expansion method
Usage and example analysis of PHP array function array_multisort()
PHP function import_request_variables() usage and example analysis
The above is the detailed content of Implementation of two page turning display functions in PHP. For more information, please follow other related articles on the PHP Chinese website!