Okay so i have an image gallery script that creates two links, "Next Image" and "Previous Image". The script works but i'd like to know if i could make it more efficent with a better SQL query.
Here's my database
What i am currently doing is querying the database and getting a list of all the image id's in the same gallery, and putting them into an array. Which doesn't seem very logical (but it's the only solution i could come up with).
// CREATE AN ARRAY WITH ALL THE IMAGE ID'S IN THE GALLERY
//
$query = ("SELECT id FROM images WHERE gallery_id=(SELECT gallery_id FROM images WHERE id='".$_REQUEST['id']."') ORDER BY id DESC");
$result = mysql_query($query);
$images_in_gallery = array();
while ($row = mysql_fetch_array($result))
{
array_push($images_in_gallery, $row['id']);
}
And then searching through said the array to get the id of the next image and the previous image.
// CREATE THE LINK
//
$next_link = "<a href=\"?id=".$images_in_gallery[array_search($_REQUEST['id'], $images_in_gallery)+1]."\">$next_text</a>";
$prev_link = "<a href=\"?id=".$images_in_gallery[array_search($_REQUEST['id'], $images_in_gallery)-1]."\">$prev_text</a>";
Is there a way i can do this easier with a better query, for example a query that selects the next image in the gallery and the previous, i am not brilliant with SQL but i do know that if you have a good query your code can be considerably shorter.
The full code can be found here on pastebin.
http://pastebin.com/GzTpq6Uj