Page 1 of 1

Photo Gallery Next/Previous

Posted: Wed Dec 03, 2008 2:44 pm
by SheDesigns
~pickle | Please use [ code=html ], [ code=php ], etc tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: :arrow: Posting Code in the Forums to learn how to do it too.


Here's a challenge for anyone willing to save me from throwing office supplies at my co-workers.

If you read through my slop of code here, I'm trying to build a photo gallery for the client. I want it to go to the next picture (or previous), but I need to know the photoid (a field from the table) to display the picture. How the heck would you go about this?

Should I use a select with a limit??

Code: Select all

    if($function == "View Image")
    {
    $album = $_GET['album'];
    $photoid = $_GET['photoid'];
    $o = $_GET['o'];  // the number photo it is in the album
    $a = $_GET['a']; // the number of photos in the album
    echo "<a href=\"?pg=6\">Back to All Albums</a><br>";
    echo "<a href=\"?pg=6&function=See Album&album=$album\">Back to $album</a><br><br><center>";
    
    $query = "select * from photos where photoid = '$photoid';";
    $result = mysql_query($query);
    $fetch = mysql_fetch_array($result);
    extract($fetch);
    
    // Previous & Next Photo
    echo "<table border=\"0\" width=\"500\"><tr><td width=\"250\">";
// if this is the first photo, there is no previous photo
if($o != 1)
{ $newo = $o - 1;
 echo "<a href=\"?pg=6&function=View Image&album=$album&photoid=x&o=$newo&a=$a\">Previous Photo</a>"; }
 
    echo "</td><td width=\"250\" style=\"text-align: right;\">";
// if this is the last photo, there is no next photo
if($o != a)
{ $newo = $o + 1;
 echo "<a href=\"?pg=6&function=View Image&album=$album&photoid=x&o=$newo&a=$a\">Next Photo</a>"; }
 
    echo "</td></tr></table>";
    
    echo "<img src=\"scope.php?image=$imgsrc&maxsize=500\" style=\"border: 1px solid #513751;\"><br><span style=\"font-family: arial; color: black;\">
    $caption";
    
    }
}
// Edit 4pm - I editted this a little bit.. and it works great for the 1st photo in the album... but after that I'm getting Warning: extract() [function.extract]: First argument should be an array in ... - why!?

Code: Select all

if($o != a)
{
 $newo = $o + 1;
 echo "( $newo )";
 $query = "select * from photos where album = '$album' limit $newo, 1";
 $result = mysql_query($query);
 $fetch = mysql_fetch_array($result);
 extract($fetch);
 
 echo "<a href=\"?pg=6&function=View Image&album=$album&photoid=$photoid&o=$newo&a=$a\">Next Photo</a>"; }
 
    echo "</td></tr></table>";
    
    echo "<img src=\"scope.php?image=$imgsrc&maxsize=500\" style=\"border: 1px solid #513751;\"><br><span style=\"font-family: arial; color: black;\">
    $caption";
    
    }
}

~pickle | Please use [ code=html ], [ code=php ], etc tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: :arrow: Posting Code in the Forums to learn how to do it too.

Re: Photo Gallery Next/Previous

Posted: Wed Dec 03, 2008 3:02 pm
by requinix
If you can't make any assumptions about the ID numbers (besides that they are in increasing order) then the best I've ever come up with is to use two queries:

Code: Select all

SELECT fields FROM table WHERE id < current-id ORDER BY id DESC LIMIT 1
SELECT fields FROM table WHERE id >= current-id LIMIT 2
The first query gives you the previous record; the second gives you the current and the next.

You could do a UNION on the two if it made you feel better:

Code: Select all

SELECT ... UNION SELECT ...

Re: Photo Gallery Next/Previous

Posted: Wed Dec 03, 2008 5:14 pm
by SheDesigns
=\


I know there's a way to do this with a loop.. I've seen it done before, I just don't have the source code for it anywhere. :?

Re: Photo Gallery Next/Previous

Posted: Wed Dec 03, 2008 5:22 pm
by requinix
SheDesigns wrote:I know there's a way to do this with a loop
Do what?

Re: Photo Gallery Next/Previous

Posted: Wed Dec 03, 2008 5:27 pm
by pickle
Why do you need to know both the 'photoid' and 'o'? I assume photoid is something like 12337 & is unique for every photo. I also assume 'o' is something like '4'.

So, your query string would look like:

Code: Select all

?album=christmas_party&photoid=12337&o=4&a=20
It seems to me the photoid isn't necessary. If you know 'o' and 'album', can't you just do one query to determine photoid?

extract() is very dangerous, as it overwrites any variables you already have defined. I'd highly recommend not using it, and just use your $fetch variable as an array.

I'd also suggest making your code less Italian (get rid of the spaghetti code). You can do all your business logic (determining the new 'o', retrieving data about the current photo) at the top, then have the html code you output be all together in one statement. Much easier to debug.

Re: Photo Gallery Next/Previous

Posted: Wed Dec 03, 2008 9:01 pm
by SheDesigns
:) thank you, that worked great.. when i frigged the photoid and used limit..