Random row, but with next and previous button
I have a MySQL database with pictures within it. When a user comes to my page I want to show him random picture and from then he can go to the next or previous picture (not random)
Database开发者_如何学运维 example
id | picture
11 | foto1.gif
12 | foto2.gif
16 | foto3.gif
23 | foto4.gif
66 | foto5.gif
How can I make something like that with PHP and SQL? Thank you!
Use ORDER BY rand()
in your query to get the random item.
To retrieve the next/previous item (by id), use this:
WHERE id < current_id ORDER BY id DESC LIMIT 1 (for prev)
WHERE id > current_id ORDER BY id ASC LIMIT 1 (for next)
精彩评论