Photo Gallery Next/Previous

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
SheDesigns
Forum Commoner
Posts: 42
Joined: Tue Nov 18, 2008 9:51 am
Location: Buffalo, NY

Photo Gallery Next/Previous

Post 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.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Photo Gallery Next/Previous

Post 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 ...
User avatar
SheDesigns
Forum Commoner
Posts: 42
Joined: Tue Nov 18, 2008 9:51 am
Location: Buffalo, NY

Re: Photo Gallery Next/Previous

Post 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. :?
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Photo Gallery Next/Previous

Post by requinix »

SheDesigns wrote:I know there's a way to do this with a loop
Do what?
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: Photo Gallery Next/Previous

Post 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.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
SheDesigns
Forum Commoner
Posts: 42
Joined: Tue Nov 18, 2008 9:51 am
Location: Buffalo, NY

Re: Photo Gallery Next/Previous

Post by SheDesigns »

:) thank you, that worked great.. when i frigged the photoid and used limit..
Post Reply