GoFuckYourself.com - Adult Webmaster Forum

GoFuckYourself.com - Adult Webmaster Forum (https://gfy.com/index.php)
-   Fucking Around & Business Discussion (https://gfy.com/forumdisplay.php?f=26)
-   -   Tech Why is my pagination showing pages when there is no extra data to show? (https://gfy.com/showthread.php?t=1349360)

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

?>

Drachen 10-23-2021 09:37 AM

This loop looks ok. The section one above looks strange.

Shouldn't it be something like:

Code:

echo "Pages: ";
echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=1'>&lt;&lt; </a> ";
// previous page num
$prevpage = $currentpage - 1;
// show < go back to 1 page
echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$prevpage'>&lt; </a> ";
} // end if


sarettah 10-23-2021 10:45 AM

Quote:

for ($x = ($currentpage - $range); $x < (($currentpage + $range) + 1); $x++)
You have range set to 2. So lets say current page is 3.

Your loop will go

currentpage - range = 1

to

currentpage + range + 1=6

So you will be putting up links to pages 4, 5 and 6 that do not exist.

Not going to tell you how to fix it. This one is an easy one.

.

Publisher Bucks 10-23-2021 11:10 AM

Got it!

Thank you 👍

Drachen 10-23-2021 11:36 AM

Quote:

Originally Posted by sarettah (Post 22927275)
You have range set to 2. So lets say current page is 3.

Your loop will go

currentpage - range = 1

to

currentpage + range + 1=6

So you will be putting up links to pages 4, 5 and 6 that do not exist.

Not going to tell you how to fix it. This one is an easy one.

.

This section works as intended. It skips all pages larger than $totalpages.
If after fixing what I posted previously the problem still persists you need to check if $numrows fetches you the number you are expecting. Maybe try printing it just to be sure:

Code:

echo "Number of rows: $numrows"


All times are GMT -7. The time now is 05:03 PM.

Powered by vBulletin® Version 3.8.8
Copyright ©2000 - 2025, vBulletin Solutions, Inc.
©2000-, AI Media Network Inc123