I would like to know what is the best way to create next/previous buttons for an entry in the database. Let's say we have a table with images and every image has an ID and a Path. If the current image has ID equal to "9" how can I get the next and the previous IDs?
I already found a solution for this right here https://stackoverflow.com/a/8874382 but this seems to be the latest choice. If we have over 10000 entries (images) in our database, this will overwhelm the server. First of all, it selects all entries and then it tries to copy all of the results in an array. After this, the array is searched for the current ID. It's very slow.
I was thinking about a limit set to the query but how can I get the next and previous pictures after this?
Another (better, I think) solution would be to get the ID of the current picture and decrement/increment it until another image with the new ID is found. I think this is faster than the function provided in the link.
Also, in the same link there is another idea, to set the prev_id and next_id in the model of the current picture. But here we have the same problem, how do we find them? And even if we find them, is it correct to store data about prev/next pictures in the current picture? It does not seem to be right but I'm sure it's more convenient when you need to access that data.
Thank you!