Page 1 of 1

Get the first and last id key from a query?

Posted: Wed Aug 24, 2005 2:06 pm
by klair
Hi there, I hope someone can help with this.

I have a gallery with thumbnail pictures that when selected bring up the larger image. On the larger image I have "next" and "previous" buttons to loop through just the larger images one at a time.

So far the buttons work, but I need to be able to specify the first id-key in the range so that you can't go back any further, and likewise the last id-key so that you can't go past the last image (within the specified criteria).

I am posting an idea of what I think it should look like but obviously it doesn't work. Maybe someone can fix it or give me some ideas of how to get around this problem.

Code: Select all

<?php
function shownext ($gallery_id) {
$self = "page.php";
$gallery_id = $_GET['gallery_id']; //id selected from thumbnail gallery

$query = "SELECT MAX(gallery_id) FROM gallery WHERE foldername='2003' AND location='gallery'";
$result = mysql_query($query) or die('Error, query failed');
$row = mysql_fetch_array($result);
	$max_id = $row['gallery_id']; //this needs to be the highest id key available within the above criteria

if ($gallery_id < $max_id) {
$nextid = $gallery_id + 1;
$next = "$self?gallery_id=$nextid";
} else {
$next = "$self?gallery_id=$max_id";
}
echo $next;
}

function showprev ($gallery_id) {
$self = "large2003.php";
$gallery_id = $_GET['gallery_id']; //id selected from thumbnail gallery

$query = "SELECT MIN(gallery_id) FROM gallery WHERE foldername='2003' AND location='gallery'";
$result = mysql_query($query) or die('Error, query failed');
$row = mysql_fetch_array($result);
	$min_id = $row['gallery_id']; //this needs to be the lowest id key available within the above criteria
	
if ($gallery_id > $min_id) {
$prev_id = $gallery_id - 1;
$prev = "$self?gallery_id=$prev_id";
} else {
$prev = "$self?gallery_id=$min_id";
}
echo $prev;
}
?>

feyd | Please use

Code: Select all

and

Code: Select all

tags where approriate when posting code. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]

Posted: Wed Aug 24, 2005 2:28 pm
by Stewsburntmonkey
I think the problem may be with this line:

$max_id = $row['gallery_id'];


Try this:

$max_id = $row[0];

And make the same change for the $min_id as well. :)

Posted: Wed Aug 24, 2005 2:35 pm
by Sander
Or even better, change

Code: Select all

$row = mysql_fetch_array($result);

to

Code: Select all

$row = mysql_fetch_assoc($result);
:)

Posted: Wed Aug 24, 2005 2:45 pm
by Stewsburntmonkey
Although I believe the array indexes would then have to be "MAX(gallery_id)" and "MIN(gallery_id)". :)

Thanx for the help

Posted: Thu Aug 25, 2005 3:39 am
by klair
Hi there,

Thanx all for the help.

It turns out I was supposed to use the following:

SELECT MAX(gallery_id) AS max_id, MIN(gallery_id) AS min_id FROM gallery WHERE foldername='2003' AND location='gallery'

that should get both the min and max in one query. and can be called with the names max_id and min_id

Many thanx to "IAmALlama" for this.

Posted: Thu Aug 25, 2005 6:52 am
by s.dot
Seems like it would be easier to store the images in an array

Code: Select all

$array = array('image1.jpg','image2.jpg','image3.jpg');

$max = count($array);

if($current_image != $array[1])
{
   // display next link
}
if($current_image != $max)
{
   // display previous link
}