Well I guess two short queries would work:
Previous Image
SELECT pic_id FROM pictures
WHERE pic_id < '$pic_id'
ORDER BY pic_id DESC
LIMIT 0,1
Next Image
SELECT pic_id FROM pictures
WHERE pic_id > '$pic_id'
ORDER BY pic_id ASC
LIMIT 0,1
If you quickly execute those two, you'll get one result (or none if your pic_id is 1 😉) for each, and those will be your next and previous picture IDs.
The way the SQL works is this:
- We need the pic_id value, so we SELECT that
- We get the values from the those in the "pictures" table
- We only want values that are greater than (>) $pic_id for the next one, and less than (<) $pic_id for the previous one
- We want it ordered from highest to lowest for the previous one, and lowest to highest (ASC) for the next one.
- We only want the 1 row from the set of rows returned, starting at row 0 (the first row returned)
So if your database looked like this:
| pic_id |
+--------+
| 1 |
| 2 |
| 3 |
| 5 |
| 6 |
| 7 |
| 12 |
| 17 |
+--------+
and you're given the following pic_id, the results would be:
pic_id: 5
Previous: 3
Next: 6
pic_id 12
Previous: 7
Next: 17
Hope that helps.