PHP Page Jump Function Realization Method _php skill

Source: Internet
Author: User
Tags mysql in

We all know that using Php+mysql in the Web page to implement the database data display is very simple and interesting, very few database data in the case of the page display is still satisfactory, but when the database data is very many cases, the page display will be very bad, Here is how to achieve the current page data display number and how to achieve dynamic rollover function.
Here is a description of the implementation of the two paging display features :
Let's introduce the database syntax used in paging:

mysql_query ("SELECT * from table ORDER BY id DESC");

This database statement is more familiar, is used to search for records and display in reverse order, but it does not work in the paging function, and the following extended syntax is to achieve the core function of paging:

mysql_query ("SELECT * from table ORDER BY id desc limit $start, $limit");

The $start here is the starting line for the database search, $limit from the start line search to the end of the $limit record, OK, with this core function, we can start the paging function.

The first kind of page-flipping function:
The function described here is the simplest one in the page-turning function, which can only be used to turn the page forward and turn the page back, the very news of this station and the page-turning function of the Download Center is this.
First, introduce the way to achieve the function of page :

    • First determine the number of data records that are fixed on the current page, assuming 20 records, set the $limit value to: $limit = 20;
    • When you display database records, you must start with the first one, so set the initial value of $start to 0: $start = 0;
    • The realization of the paging function depends on the dynamic change of the $start, and when the page is leafed back $start the $limit: $start + $limit, while the page is $start the $limit $start.

With the above thought, you can begin to design the program.

page.php:

? 
Sets the number of current page displays (this number can be set arbitrarily) $limit = 20; 
Initializes 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 "<table><tr><td> paging function </td></tr>"; 
if (!emptyempty ($num)) {for ($i =0; $i < $num; $i + +) {$val =mysql_result ($result, $i, "Val"); 
$val 1=mysql_result ($result, $i, "val1"); 
echo "<tr><td> $val </td><td> $val 1</td></tr>"; 
} echo "<tr><td>"; 
Set the jump $prve the forward page = $start-$limit; 
if ($prve >=0) {echo "<a href=page.php?start= $prve >prve</a>"; 
//Set the jump $next for backward paging = $start + $limit; 
if ($next < $num _max) {echo "<a href=page.php?start= $next >next</a>"; 
echo "</td></tr></table>"; ?>

A front turn, after the turn function of the program is completed, but this feature will be cumbersome when it comes to processing more data. The more powerful, more complex page-flipping feature--looping pages (which I've always called because I can't find a more appropriate name) will continue to be described below.:)
The introduction of a simple page-turning function to achieve, the following introduction of the page is more powerful, more complex, the site of the very forum and very article is to use this circular paging function.
The cycle page is the front flip and the number together to achieve, the specific form of expression:
page: Prve <<1 2 3 4 ..... >> Next
The numbers inside indicate the current page, and the Prve and rear turns are more than just the front and rear flip of the current page, but more complex digital controls.

As in the past, before the program design, the first to clarify the idea, I suggest that the reader after reading how to achieve the function of circular paging, can do their own hands-on practice, because some of the methods and ideas studied here may be more abstract.
First of all, we boldly assume that there are more than 1000 records in the database, we want to display 25 records currently, and the digital flip control is 20, so it is like the following display results:
page: 0 1 2 3 ... >> Next
After the turn of the display results:
page: Prve <<20 27 28 ..... >> Next
Okay, let's take a look at the rules, a fixed display number 25, a fixed digital control doubling of 20. We can use these two numbers to achieve the circular paging function;
First set the fixed display variables:
$limit = 20;

Settings for database initial variables:
$start = 0;

The total number of database records is:

$num;
Page variables: $page;
A page loop shows the following procedure:

? 
... 
$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 
} 
} 
?>

This code, in addition to displaying numbers, other functions are not implemented, because more digital control flip, so there must be a number of variables to mark and identify these controls, here with the $s to mark, this variable is used to control the digital Loop page control, Now you can look at the complete code for looping the page page.php:

? 
$limit = 25; 
if (!emptyempty ($start)) $start = 0; 
if (!emptyempty ($s)) $s = 0; 
mysql_connect ("localhost", "", ""); 
mysql_select_db (database); 
Total number of statistical 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 "<table>"; 
if (!emptyempty ($numb)) {for ($i =0; $i < $numb; $i + +) {$val =mysql_result ($result, $i, "Val"); 
$val 1=mysql_result ($result, $i, "val1"); 
echo "<tr><td> $val </td><td> $val 1</td></tr>"; 
} echo "</table>"; 
Digital loop control of the page echo "<table>"; 
echo "<tr><td> page:</td>"; 
The front flip control 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>"; 
Set the current page corresponding pages no link function $star = $start; Notice the initial value of the loop, and think carefully why not 0 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>"; 
Control the display of digital page restrictions, control displays only 20 page 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>"; 
}//Note the control break out of the loop; 
} echo "</tr></table>";
 ?>

There is also a paging function is to submit the page, that is, in the submission form to add data submission, and then the program to jump to the corresponding page, the function to achieve a relatively simple, it is left to the reader to complete their own.

The above procedures have been able to complete the powerful cycle page function, we can study carefully, really do apply.

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.