Forum Moderators: coopster

Message Too Old, No Replies

PHP Paging Problem

paging problem

         

kristie380

6:20 pm on Feb 8, 2007 (gmt 0)

10+ Year Member



Ok so here's the problem I'm having...

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 .= " &nbsp;<a href=\"reunion_photo_large.php?id=$id&page=$page\">$page</a>&nbsp; ";
}
}
// creating previous and next link
// plus the link to go straight to
// the first and last page

if ($pageNum > 1)
{
$page = $pageNum - 1;
$prev = "&nbsp;<a href=\"reunion_photo_large.php?id=$id&page=$page\">[Prev]</a>&nbsp; ";

$first = " &nbsp;<a href=\"reunion_photo_large.php?id=$id&page=1\">[First Page]</a>&nbsp; ";
}
else
{
$prev = '&nbsp;'; // we're on page one, don't print previous link
$first = '&nbsp;'; // nor the first page link
}

if ($pageNum < $maxPage)
{
$page = $pageNum + 1;
$next = " &nbsp;<a href=\"reunion_photo_large.php?id=$id&page=$page\">[Next]</a>&nbsp; ";

$last = " &nbsp;<a href=\"reunion_photo_large.php?id=$id&page=$maxPage\">[Last Page]</a>&nbsp; ";
}
else
{
$next = '&nbsp;'; // we're on the last page, don't print next link
$last = '&nbsp;'; // 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?

cameraman

6:44 pm on Feb 8, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



One thing that I see that's happening is that you're wiping out your first query when you do the second at:
$query = "SELECT COUNT(photo_id) AS numrows FROM reunion_photos where id = $id";
$result = mysql_query($query) or die('Error, query failed');

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.

kristie380

7:14 pm on Feb 8, 2007 (gmt 0)

10+ Year Member



ok i tried changing my variable names and it is still starting from the first photo. :(

cmarshall

7:17 pm on Feb 8, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hate to say it, but you may want to look at available systems like Coppermine Photo Gallery and Gallery2.

I would not bother writing my own gallery (I've done it before, in -gasp- Perl, and I tossed it all aside for Gallery2).

cameraman

7:33 pm on Feb 8, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I think maybe I see it (don't change that query stuff back though)
At the top, you're checking for $_GET['page'], but you're not checking for $_GET['id']