Forum Moderators: open

Message Too Old, No Replies

Get First, Last, Previous, & Next record

         

Nutter

12:58 am on Nov 27, 2005 (gmt 0)

10+ Year Member



I have a page that opens a specific image based on an id stored in a database. Is there a way to get the next and previous id's without a lot of effort? Specifically, I don't want to have to go all the way through the database again just to get two ids.

volatilegx

2:26 am on Nov 28, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Are the IDs sequential integers? Couldn't you just add one/subtract one from the current ID to get the next/previous?

Nutter

2:42 pm on Nov 28, 2005 (gmt 0)

10+ Year Member



The ids are not necessarily sequential, although I had that thought as well. The table stores individual images for albums (photo album script), and the next id may be for a different album.

I wound up creating a function that'll return the first, last, previous, or next depending on a variable passed.

There is already a 'sort_order' field so that the user can sort each album however they want. So, next is 'SELECT id FROM table WHERE sort_order>$current_sort_order AND album_id=$current_album_id ORDER BY sort_order ASC LIMIT 1;'. Previous is similar. Last and First just get the id of the min(sort_order) and max(sort_order).

Maybe not the cleanest, but it works.