Web Worm
Web Worm

Reputation: 2066

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 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

Upvotes: 1

Views: 336

Answers (2)

oezi
oezi

Reputation: 51797

try it like this:

SELECT
  picture_id
FROM
  whatever
WHERE
  album_id = 23
AND
  picture_id > 3242
ORDER BY 
  picture_id
LIMIT 1

Upvotes: 1

Amber
Amber

Reputation: 526543

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.

Upvotes: 1

Related Questions