Prev/Next Link problem.
| From: | Michael O'Neal | Date: | Tue, 16 Oct 2001 23:13:52 +0000 |
| Subject: | Prev/Next Link problem. | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-71272@lists.php.net to get a copy of this message | ||
Hi. I've got a photo gallery that I've built, that displays 6 thumbnails at
a time, and then gives previous, next, and page links. The problem is, I
always seem to end up with 1 extra page result, and the "next" link never
goes away, even when the records end. Clicking on the "next" link just
takes you to a blank page. I've been messing with the numbers in the code,
but I can't seem to figure it out, so I thought I'd instill the knowledge of
the expert masses. Can anyone help?
The gallery itself can be found at http://128.242.234.179/gallery/
Here's the code:
// execute SQL query and get results
$limit=6; // Rows to return
$numresults=mysql_query($sql,$connection) or die ("Couldn't execute
query.");
$numrows=mysql_num_rows($numresults);
// next determine if offset has been passed to script, if not use 0
if (empty($offset)) {
$offset=0;
}
// Get Results
$result=mysql_query("SELECT ID,title,thumbnail FROM $table_name limit
$offset,$limit");
// now you can display the results returned
$i=1;
while ($row = mysql_fetch_array($result)) {
$ID=$row['ID'];
$thumbnail=$row['thumbnail'];
$title=$row['title'];
if (is_int($i/3)) {
$display_block .= "<display code snipped...>
}
else {
$display_block .= "<display code snipped>
}
$i++;
}
// next we need to do the links to other results
if ($offset>0) { // bypass PREV link if offset is 0
$prevoffset=$offset-6;
$prev_link .= "<a href=\"$PHP_SELF?offset=$prevoffset\">PREV</a>\n";
}
// calculate number of pages needing links
$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++) { // loop thru
$newoffset=$limit*($i-1);
$pages_link .= "<a href=\"$PHP_SELF?offset=$newoffset\">$i</a>\n";
}
// check to see if last page
if (!(($offset/$limit)==$pages) && $pages!=0) {
// not last page so give NEXT link
$newoffset=$offset+$limit;
$next_link .= "<a
href=\"$PHP_SELF?offset=$newoffset\">NEXT</a><p>\n";
}
<END PHP CODE>
If possible, please reply to mike@thinkmango.com, as I am on the digest.
Thanks,
mto
--
Michael O'Neal
Web Producer/ Autocrosser
ST 28 '89 Civic Si
---------------------
M A N G O
B O U L D E R
---------------------
http://www.thinkmango.com
e- mike@thinkmango.com
p- 303.442.1821
f- 303.938.8507