To achieve a powerful page jump function (i)

Source: Internet
Author: User
Tags empty mysql in
We all know that using Php+mysql in the Web page to implement the database data display is very simple and interesting, the database data is very rare 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 you will see the implementation of two paging display functions:


-----------------------------------------------------------


first describes 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 not 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");





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


-----------------------------------------------------------


First paging function:


here describes the function is the most simple page-turning function, only to achieve the page forward and backward page, the site's very news and Download Center of the page is this function.





first introduce the way to realize the function of turning page:


first determines the number of data records that are fixed on the current page, assuming 20 records, set $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;





and the realization of the paging function depends on the $start dynamic change, when the page is backward $start $limit: $start + $limit, while the page forward $start the regular minus $limit: $start-$limit;





with the above ideas, you can begin to design the program page.php:





;?


//Set the current page display quantity (this quantity can be set arbitrarily)


$limit = 20;





//Initialize database search start record


if (!empty ($start)) $start = 0;





mysql_connect ("localhost", "", "");


mysql_select_db (database);





//Set total 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 (!empty ($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 Jump to page forward


$prve = $start-$limit;


if ($prve >=0) {


echo "<a href=page.php?start= $prve >prve</a>";


}





//Set jump for backward paging


$next = $start + $limit;


if ($next < $num _max) {


echo "<a href=page.php?start= $next >next</a>";


}





echo "</td></tr></table>";


?>





a front turn, back turn function of the program 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.:)





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.