Forum Moderators: coopster

Message Too Old, No Replies

Frustrated - and perplexed.

Dynamic thumbnails reverting to page one on click

         

mcjohnson

4:11 pm on May 22, 2007 (gmt 0)

10+ Year Member



Friends,

I could really use your help. I am contructing a small photo gallery that the client can manage. (if you want to see the real thing in action, visit my profile)

1. He uploads his photos into a basic form and chooses the category.

2. When the user chooses a particular category page, the script pulls the thumbnails out of the MySQL database and displays them, five to a page.

3. After five are pulled from the db, the script adds a "next" link to view the next five. (And a "prev" link):

<?PHP
// rows to return
$limit=5;
$thumbnail = $row['thumbnail'];
$title = $row['title'];

$query = "select * from photo_gallery where category_id = 3
order by photo_id desc";

$numresults=mysql_query($query);
$numrows=mysql_num_rows($numresults);

// next determine if s has been passed to script, if not use 0
if (empty($s)) {
$s=0;
}

$query .= " limit $s,$limit";
$count = 1 + $s ;
$results=mysql_query($query) or die (mysql_error());
while ($rows=mysql_fetch_array($results)) {
extract ($rows);
echo "&nbsp<a href='http://www.example.com$PHP_SELF?photo_id=$rows[photo_id]'>";
echo "<img src='http://www.example.com/galleryscript/_files/photogallery/$thumbnail' width=75 height=50 border=1>";
echo "</a>";
echo "&nbsp;";

$count++ ;
}

$currPage = (($s/$limit) + 1);

//break before paging
echo "<br />";

// next we need to do the links to other results
if ($s>=1) { // bypass PREV link if s is 0
$prevs=($s-$limit);
print "&nbsp;<a href=\"$PHP_SELF?s=$prevs&q=$var\">&lt;&lt;
Prev</a>&nbsp&nbsp;";
}

// 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++;
}

// check to see if last page
if (!((($s+$limit)/$limit)==$pages) && $pages!=1) {

// not last page so give NEXT link
$news=$s+$limit;

echo "&nbsp;<a href=\"$PHP_SELF?s=$news&q=$var\">Next &gt;&gt;</a>";
}

$a = $s + ($limit) ;
if ($a > $numrows) { $a = $numrows ; }
$b = $s + 1 ;
?>

4. Now, when a user select the page of thumbnails, the script checks to see if a thumbnail has already been clicked. If not, it displays a basic starter photo. Otherwise, the script displays the enlarged version of the thumbnail, when the thumbnail is clicked:

<?php
$yes = $_GET['photo_id'];
$photo = $row['photo'];
if (isset($yes)) {
$query = "SELECT * FROM photo_gallery WHERE photo_id = ".$_GET["photo_id"];
$results=mysql_query($query) or die (mysql_error());
while ($rows=mysql_fetch_array($results)) {
extract ($rows);
echo "<img src='http://www.example.com/galleryscript/_files/photogallery/$photo' width=400 height=300 border=1>";
echo "<br>";
echo "$title";

}} else {
echo "<img src='images/2.jpg' width=400 height=300 border=1>";
}

?>

The problem is, that if a user clicks the "next" link in the thumbnail display, and, say, pages back to the 3rd page of 5 thumbs, THEN clicks a thumbnail, the display of thumbs reverts back to page 1. I would like for the thumbs to remain on the 3rd, 2nd page, whichever the user is on.

I've tried a number of fixes, but so far, nothing is working. This form is posting to itself. Hopefully, that's not a problem that is insurmountable.

Any suggestions, fixes or help would be GREATly appreciated.

Best Regards,

Pat

StupidScript

5:13 pm on May 22, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



This line may be causing trouble with your link behavior:

 echo "&nbsp<a href='http://www.example.com$PHP_SELF?photo_id=$rows[photo_id]'>";

You're missing a semi-colon in the entity reference:

&nbsp;
and a slash in the URI:
...example.com/$PHP_SELF...

[edited by: StupidScript at 5:14 pm (utc) on May 22, 2007]

cameraman

5:18 pm on May 22, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Add your current page to your image link:
echo "&nbsp;<a href='http://www.example.com$PHP_SELF?photo_id={$rows['photo_id']}&s=$s'>";
(or something similar) - I'm not seeing where $s is first calculated; you need to ensure that when you get to this line, the value of $s is the current page. Also note the addition of the braces around the variable name.

Also, an unsolicited suggestion, where you're doing:
$yes = $_GET['photo_id'];
$photo = $row['photo'];
if (isset($yes)) {

if $_GET['photo_id'] isn't set you'd generate a warning (which your errorlevel may be suppressing). It would be better to
if(isset($_GET['photo_id']))
$yes = $_GET['photo_id'];
else
...

mcjohnson

9:25 pm on May 22, 2007 (gmt 0)

10+ Year Member



Add your current page to your image link:
echo "&nbsp;<a href='http://www.example.com$PHP_SELF?photo_id={$rows['photo_id']}&s=$s'>";
(or something similar) - I'm not seeing where $s is first calculated; you need to ensure that when you get to this line, the value of $s is the current page. Also note the addition of the braces around the variable name.

SCORE!

That worked. THANK you!

I will get to work on the if/then suggestion as well.

Many thanks! ALWAYS appreciated!

Pat