Home >Backend Development >PHP Tutorial >Implementing 'Previous Page' and 'Next Page Buttons_PHP Tutorial

Implementing 'Previous Page' and 'Next Page Buttons_PHP Tutorial

WBOY
WBOYOriginal
2016-07-21 16:05:451048browse

//This example is taken from phpbuilder.com
//Slightly translated
//

$limit=20; // Every The number of rows displayed on the page
$numresults=mysql_query("select * from TABLE where YOUR CONDITIONAL HERE order by WHATEVER");//Replace with the sql statement you need
$numrows=mysql_num_rows($numresults);

// next determine if offset has been passed to script, if not use 0
if (empty($offset)) {
$offset=1;
}

// Get query results
$result=mysql_query("select id,name,phone ".
"from TABLE where YOUR CONDITIONAL HERE ".
"order by WHATEVER limit $offset,$limit" );

// Now display the query results
while ($data=mysql_fetch_array($result)) {
// Insert the results and style you want to display here
}

//Show button

if ($offset!=1) { // bypass PREV link if offset is 1
$prevoffset=$offset-20;
print "< a href="$PHP_SELF?offset=$prevoffset">Previous page n";
}

// Calculate the number of pages
$pages=intval($numrows /$limit);

// $pages now contains int of pages needed unless there is a remainder from division
if ($numrows%$limit) {
// has remainder so add one page
$pages++;
}

for ($i=1;$i<=$pages;$i++) { // Display the number of pages
$newoffset=$limit*( $i-1);
print "$i n";
}

// check to see if last page
if (!(($offset/$limit)==$pages) && $pages!=1) {
// not last page so give NEXT link
$newoffset=$ offset+$limit;
print "next page

n";
}

?>


www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/315557.htmlTechArticle?php //This example is taken from phpbuilder.com //Slightly translated//sprming@netease.com $limit =20; // Number of rows displayed per page $numresults=mysql_query(select * from TABLE where YOUR CONDITIONAL HERE...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn