how can i get next and previous field id with php from mysql
i am trying to develop php photo album...and i have picture_id and album_id field in mysql table...and table name is photos
now开发者_如何学Go i want to get next and previous picture_id field in same album...rather it is in series or not...i.e
picture_id = 3242
album_id = 23
next picture
picture_id = 5249
album_id = 23
try it like this:
SELECT
picture_id
FROM
whatever
WHERE
album_id = 23
AND
picture_id > 3242
ORDER BY
picture_id
LIMIT 1
This query should help you:
$cur_picture_id = 3242;
$cur_album_id = 23;
$result = mysql_query("SELECT picture_id FROM photos
WHERE album_id = $cur_album_id AND picture_id > $cur_picture_id
ORDER BY picture_id LIMIT 1");
$result_ar = mysql_fetch_array($result);
if($result_ar) {
$next_picture_id = $result_ar['picture_id'];
} else {
// end of album
}
Note that there are better ways to do queries like this (prepared statements, so you can let the compiler bind the variables for you instead of just inserting them in the string), but this should give you a general idea of what needs to be done.
精彩评论