Click to See Complete Forum and Search --> : Simple Array for Photo Gallery with 'Next' Button


ripcurlksm
10-24-2007, 02:08 PM
I am trying to create an image array where it will load the first image in the array, and if a user clicks the 'Next Image' button, it will go to the next image in the array. When the user views the last image and clicks next, it should go back to the first image.

I need help (1) revising my array (2) building a loop to cycle through the images.

Here is my image table
id | path | caption
---------------------------------------------------------------
1 | image1.jpg | This is caption #1
1 | image2.jpg | This is caption #2
2 | image3.jpg | This is caption #3
3 | image4.jpg | This is caption #4



$query = "SELECT * FROM images WHERE id='1'";
$result = mysql_query($query) or die(mysql_error());

///////////////////////////////
//////////BUILD ARRAY //////
///////////////////////////////
while (mysql_fetch_array($query))
{
$pics[] = array(
'id' => $pics['id'],
'path' => $pics['path'],
'caption' => $pics['caption']
);

}

///////////////////////////////
//////////BUILD LOOP ///////
///////////////////////////////

// I really need help with this part -- if a user clicks the 'Next Image' button, it will go to the next image in the array.

for each (x as y){
<a href="$pics['path']">Next Photo</a>
}

bokeh
10-24-2007, 07:44 PM
// Get the current image
$current_id = isset($_GET['id']) ? intval($_GET['id']) : 0 ;
$query = <<<END
SELECT id, path
FROM images
WHERE id = (SELECT IFNULL(MIN(IF(id='$current_id',id,null)),MIN(id)) FROM images)
LIMIT 1
END;

$row = mysql_fetch_assoc(mysql_query($query));
// echo the current image
echo 'img src="'.$row['path'].'">'."\n";
$current_id = $row['id'];

// Get the id of the next image
$query = <<<END
SELECT id
FROM images
WHERE id = (SELECT IFNULL(MIN(IF(id>'$current_id',id,null)),MIN(id)) FROM images)
LIMIT 1
END;

// echo a link to the next image
echo '<a href="'.$_SERVER['PHP_SELF'].'?id='.mysql_result(mysql_query($query), 0).'">Next</a>'."\n";