[RESOLVED]Getting the previous and next Mysql entries
I am storing paths to images in a db, what I want to do is get an image path from the db, but then also get the previous entry and next entry around the current path that I selected. I am making a photo gallery and want to display 2 thumbnails for navigation on each side of the "main" bigger picture.
Any help would be great :)
Re: Getting the previous and next Mysql entries
How are you retrieving the data? If you could post the code you are using to do that we can suggest something.
Re: Getting the previous and next Mysql entries
What's your concept of "previous" and "next"? The DB itself doesn't really have one - it depends on the sorting of the data.
Re: Getting the previous and next Mysql entries
Basically i'll pass the photo id in the url, then I have a function that gets the id and queries the db for that id.
This is the type of url I am using:
photo.php?imageid=9
PHP Code:
function display_image() {
if(isset($_GET['imageid'])){
$imageid = $_GET['imageid'];
$result = mysql_query("SELECT * FROM photos WHERE id = '$imageid'");
$myrow = mysql_fetch_array($result);
$uid = $myrow[userid];
$imagename = $myrow[fname] . "." . $myrow[ext];
$uresult = mysql_query("SELECT * FROM users WHERE userid = '$uid'");
$umyrow = mysql_fetch_array($uresult);
$path = "/photo/upload_pic/$umyrow[username]/$imagename";
echo $path;
}
}
I would just use the photo id and subtract one or add one but im giving the user the option to delete photos so they will not always be sequencial.
Re: Getting the previous and next Mysql entries
PHP Code:
$imageid = intval($_GET['imageid']); // Prevent SQL injection.
// Query for the current row:
$query = "SELECT * FROM photos WHERE id = $imageid";
// For the previous:
$query = "SELECT * FROM photos WHERE id < $imageid ORDER BY id DESC LIMIT 1";
// For the next:
$query = "SELECT * FROM photos WHERE id > $imageid ORDER BY id ASC LIMIT 1";
Re: Getting the previous and next Mysql entries
Something so simple yet so far away from my brain :) Thanks alot Cornedbee, it worked like a charm.