Publisher Bucks |
10-22-2021 11:32 PM |
Why is my pagination showing pages when there is no extra data to show?
I have my pagination stuff working however, its showing me additional pages, for example, lets say I have enough data to fill 3 pages, on page 3 it continues to show 4,5,6,7 etc. even though there is no data to put on those pages.
I *THINK* it has something to do with my calculation in red below?
What do I need to change to stop that from happening? :helpme
Quote:
<?php
// db info
$conn = mysqli_connect('localhost','dbuser',dbpass') or trigger_error("SQL", E_USER_ERROR);
$db = mysqli_select_db($conn, 'dbname') or trigger_error("SQL", E_USER_ERROR);
// find rows in table
$sql = "SELECT COUNT(*) FROM Recipe";
$result = mysqli_query($conn, $sql) or trigger_error("SQL", E_USER_ERROR);
$r = mysqli_fetch_row($result);
$numrows = $r[0];
// number of rows per page
$rowsperpage = 25;
// get total pages
$totalpages = ceil($numrows / $rowsperpage);
// grab current page default
if (isset($_GET['currentpage']) && is_numeric($_GET['currentpage'])) {
// cast var as int
$currentpage = (int) $_GET['currentpage'];
} else {
// default page numbers
$currentpage = 1;
} // end if
// current page is bigger than total
if ($currentpage > $totalpages) {
// current to last page
$currentpage = $totalpages;
} // end if
// current page less than first
if ($currentpage < 1) {
// set current to first
$currentpage = 1;
} // end if
// list offset, based on current
$offset = ($currentpage - 1) * $rowsperpage;
// get the info from the db
$sql = "SELECT * FROM Recipe WHERE Title REGEXP 'pork' LIMIT $offset, $rowsperpage";
$result = mysqli_query($conn, $sql) or trigger_error("SQL", E_USER_ERROR);
// fetch data rows...
while ($list = mysqli_fetch_assoc($result)) {
$link = "/test/recipes.php?id=".$list['RecipeID'];
// echo data
echo "<a href = ". $link . ">" . $list['Title'] . "</a><br>";
} // end while
// show links
$range = 2;
// not page 1 no links
if ($currentpage > 1) {
// show << go back to page 1
echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=1'></a> ";
// previous page num
$prevpage = $currentpage - 1;
// show < go back to 1 page
echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$prevpage'>Pages:</a> ";
} // end if
// loop current page
for ($x = ($currentpage - $range); $x < (($currentpage + $range) + 1); $x++) {
// valid page number...
if (($x > 0) && ($x <= $totalpages)) {
// current page...
if ($x == $currentpage) {
// 'highlight' no link
echo " [<b>$x</b>] ";
// not current page...
} else {
// linked
echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$x'>$x</a> ";
} // end else
} // end if
} // end for
?>
|
|