To achieve a powerful page jump function (ii)

Source: Internet
Author: User
Tags empty variables variable
Page in front of 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 the use of this circular paging function.





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 the
represent the current pages, 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 before, 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 we boldly assume that there are more than 1000 records in the database, we want to display 25 records at the moment, and the digital flip control is 20, so we have the following results:





page: 0 1 2 3 ... >> Next





after the results of the display:





page: Prve << 20 27 28 ..... >> Next





Well, 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 variable:


$limit = 20;


database initial variable settings:


$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 few 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 see the complete code to loop the page page.php:





;?


$limit = 25;


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


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





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


mysql_select_db (database);





//Statistics database records total


$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 (!empty ($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 cycle paging control


echo "<table>";


echo "<tr><td> page:</td>";





//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;





//Note 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 Digital page limit display function, control only displays 20 pages


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>";


}


//Pay attention to the control of jumping out of circulation


break;


}


}


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


?>





above the Cheng can complete the powerful circular paging function.





also has 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, this function to achieve a relatively simple, leaving the reader to complete their own, here no longer introduced ...





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.