Welcome to the GoFuckYourself.com - Adult Webmaster Forum forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community you will have access to post topics, communicate privately with other members (PM), respond to polls, upload content and access many other special features. Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Post New Thread Reply

Register GFY Rules Calendar Mark Forums Read
Go Back   GoFuckYourself.com - Adult Webmaster Forum > >
Discuss what's fucking going on, and which programs are best and worst. One-time "program" announcements from "established" webmasters are allowed.

 
Thread Tools
Old 10-22-2021, 11:32 PM   #1
Publisher Bucks
Confirmed User
 
Industry Role:
Join Date: Oct 2018
Location: New Orleans, Louisiana. / Newcastle, England.
Posts: 1,125
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?

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

?>
__________________
SOMETHING EXTREME IS COMING SOON!
Publisher Bucks is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Old 10-23-2021, 09:37 AM   #2
Drachen
Registered User
 
Drachen's Avatar
 
Industry Role:
Join Date: Nov 2011
Location: Poland
Posts: 9
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
__________________
Your contact in Poland.
Drachen is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Old 10-23-2021, 10:45 AM   #3
sarettah
see you later, I'm gone
 
Industry Role:
Join Date: Oct 2002
Posts: 14,063
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.

.
__________________
All cookies cleared!
sarettah is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Old 10-23-2021, 11:10 AM   #4
Publisher Bucks
Confirmed User
 
Industry Role:
Join Date: Oct 2018
Location: New Orleans, Louisiana. / Newcastle, England.
Posts: 1,125
Got it!

Thank you 👍
__________________
SOMETHING EXTREME IS COMING SOON!
Publisher Bucks is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Old 10-23-2021, 11:36 AM   #5
Drachen
Registered User
 
Drachen's Avatar
 
Industry Role:
Join Date: Nov 2011
Location: Poland
Posts: 9
Quote:
Originally Posted by sarettah View Post
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"
__________________
Your contact in Poland.
Drachen is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Post New Thread Reply
Go Back   GoFuckYourself.com - Adult Webmaster Forum > >

Bookmarks

Tags
pages, data, pagination, calculation, happening, helpme, stop, change, red, fill, additional, extra, stuff, continues, 4, 5, 6, 7, page
Thread Tools



Advertising inquiries - marketing at gfy dot com

Contact Admin - Advertise - GFY Rules - Top

©2000-, AI Media Network Inc



Powered by vBulletin
Copyright © 2000- Jelsoft Enterprises Limited.