Welcome Guest, Not a member yet? Register   Sign In
Get id of item before and after current id
#1

[eluser]codex[/eluser]
Is there an easy way to get the id of the record before and the record after the current id? It's for a small picturebrowser kinda like Twitpic.

I'd say: get array with picture id's, find which one is current, then select the id before and the id after that in the array.

How would you go about this? My way seems very inefficient, especially if you have a lot of pictures.

Help appreciated!


EDIT: Never mind. Should have Googled first
#2

[eluser]richthegeek[/eluser]
This works; post your solution?

Code:
SELECT id FROM table ORDER BY ABS( 1234 - id ) LIMIT 1, 2;
Where 1234 is the current id, changing column and table name to fit.

Actually that selects the two closest, not the one before and after..
#3

[eluser]codex[/eluser]
Hmm, this is actually a very nice solution. Thanks!




Theme © iAndrew 2016 - Forum software by © MyBB