Forum Moderators: coopster
I have a photo gallery with a paging script and what I am trying to do is link to an enlarged version of the photo after it is clicked on. Then on the page with the enlarged photo I want the user to be able to page through all of the enlarged photos one at a time. It works except for one problem - no matter what picture in the gallery the user clicks on to enlarge, it always starts at the beginning of the records, rather than starting from the picture the user clicked on. For example, if the user clicks on the 5th picture in the gallery to see it enlarged, the next screen will always start from picture #1 instead of picture #5. Anyway, here is the code I have...
///////////////////////////BEGIN PAGING SCRIPT/////////////////////////////////////////////
$rowsPerPage = 1;
// by default we show first page
$pageNum = 1;
// if $_GET['page'] defined, use it as page number
if(isset($_GET['page']))
{
$pageNum = $_GET['page'];
}
// counting the offset
$offset = ($pageNum - 1) * $rowsPerPage;
$query = " SELECT * FROM reunion_photos where id = $id" .
" LIMIT $offset, $rowsPerPage";
$result = mysql_query($query) or die('Error, query failed');
while ($newArray = mysql_fetch_array($result))
{
$id = $newArray['id'];
$file_comment = $newArray['file_comment'];
$photo = $newArray['photo'];
$photo_id = $newArray['photo_id'];
$firstname = $newArray['firstname'];
// how many rows we have in database
$query = "SELECT COUNT(photo_id) AS numrows FROM reunion_photos where id = $id";
$result = mysql_query($query) or die('Error, query failed');
$row = mysql_fetch_array($result, MYSQL_ASSOC);
$numrows = $row['numrows'];
// how many pages we have when using paging?
$maxPage = ceil($numrows/$rowsPerPage);
// print the link to access each page
$self = $_SERVER['PHP_SELF'];
$nav = '';
for($page = 1; $page <= $maxPage; $page++)
{
if ($page == $pageNum)
{
$nav .= " $page "; // no need to create a link to current page
}
else
{
$nav .= " <a href=\"reunion_photo_large.php?id=$id&page=$page\">$page</a> ";
}
}
// creating previous and next link
// plus the link to go straight to
// the first and last page
if ($pageNum > 1)
{
$page = $pageNum - 1;
$prev = " <a href=\"reunion_photo_large.php?id=$id&page=$page\">[Prev]</a> ";
$first = " <a href=\"reunion_photo_large.php?id=$id&page=1\">[First Page]</a> ";
}
else
{
$prev = ' '; // we're on page one, don't print previous link
$first = ' '; // nor the first page link
}
if ($pageNum < $maxPage)
{
$page = $pageNum + 1;
$next = " <a href=\"reunion_photo_large.php?id=$id&page=$page\">[Next]</a> ";
$last = " <a href=\"reunion_photo_large.php?id=$id&page=$maxPage\">[Last Page]</a> ";
}
else
{
$next = ' '; // we're on the last page, don't print next link
$last = ' '; // nor the last page link
}
// print the navigation link
echo "<div align=\"center\"><font size=\"2\">";
echo $first . $prev . $nav . $next . $last;
echo "</font><br><br></div>";
///////////////////////////END PAGING SCRIPT/////////////////////////////////////////////
How can I fix it so that the paging script will start from the picture the user clicks on rather than picture #1 all the time?
since you're reusing the $query and $result variables. Instead of doing that second query you can just do
$numrows = mysql_num_rows($query);
to get the number of rows in the first one. If I'm missing something and you do need to do that second query, use different variable names - $query2 and $result2 or something.
That may clear it all up, so try that and post results.