> Hi, what's the simplest way to provide previous/next buttons that navigate
> through individual records of a database?  This is for a photo gallery, so I
> want to do one record at a time until I'm out of records, then I want the
> "next" button to disappear.  I've been fiddling with it for a while now, but
> haven't been very successful.  Anyone have any snippets, or ideas for me?
> 
> Thanks in advance.

I'll show you what I did but realize I'm an advanced beginner/intermediate 
programmer. This may not be the most efficient way to do it but seems to
be the simplest code compared to other ways of doing it that I've seen. Not
sure if I will explain this well enough either. And this is for a fairly small 
database. With a larger one it may be too inefficient.

First count how many records are in the database for the album and put
it in $count.

Then loop through all the records in the album, place the id for each record
in an array and find out where in the array the current record is. $photo_id
is fed to this script which tells it which picture to display:

$x = 0;
while ($row = mysql_fetch_array($result)) {
        $photo_id1 = $row['photoid'];
        $array[$x] =  $photo_id1;
                if ($photo_id1 == $photo_id) {
                        $position = $x;
                }
        $x++;
}

In another sql query for retrieving the specific image (have to compensate for
array[] starting at zero but record count starting at 1):

$row = mysql_fetch_array($result);
        $photo_id = $row['photoid'];
        if ($position == 0) {
                $photo_pos = $position + 1; 
                echo "<p>Showing $photo_pos of $count &lt;&lt; Previous"; //no link
        }
        else {
                $photo_pos = $position + 1; 
                $previous_pos = $photo_pos - 2;
                $previous = $array[$previous_pos];
                echo "<p>Showing $photo_pos of $count <a 
                href=\"display.php?photo_id=$previous\">&lt;&lt; Previous</a>";
        }
        if ($count == $photo_pos) {
                echo " | Next &gt;&gt;"; //no link
        }
        elseif ($photo_pos < $count) {
                $next_pos = $photo_pos;
                $next = $array[$next_pos];
                echo " | <a 
                href=\"display.php?photo_id=$next\">Next &gt;&gt;</a>";
        }

Comments from more experienced programmers welcome. I don't want to lead
astray.
Jeff Oien

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
To contact the list administrators, e-mail: [EMAIL PROTECTED]

Reply via email to