Home >Backend Development >PHP Tutorial >Implementing 'Previous Page' and 'Next Page Buttons_PHP Tutorial
//This example is taken from phpbuilder.com n";
//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
}
?>