0

A script I've inherited is showing a random image on the homepage. The random image is currently determined (ugh) by grabbing an array of all of the images, then

$rand_keys = array_rand($array_photoid, 2);
$photoid = $photo_rows[$rand_keys[0]];

No doubt this worked swell when there were only a few dozen images, but now there are 5000 and this needs fixing.

While rewriting this I'd like to add in some functionality that allows users to go to the Previous and Next image. Going forward is easy enough, just draw up another random ID from the table.

How would I allow users to go back over previously shown random images? Is this even possible without resorting to a hack like above (storing all id's in a session array).

syrkull
  • 2,295
  • 4
  • 35
  • 68
a coder
  • 7,530
  • 20
  • 84
  • 131

1 Answers1

1

You might want to use jQuery for this instead and use the image slider plugin that supports ajax. So you load the next image via ajax request. When going back to previous image, its already loaded before so don't need to worry about the 'prev' function. (https://www.google.com.sg/search?q=photo+slider).

And then you load the page to a random image on the slideshow.

  • That's a good suggestion. Also looked at this: http://stackoverflow.com/questions/4329396/mysql-select-10-random-rows-from-600k-rows-fast/4329447#4329447, which works nice. Without reworking the front end, I could add some limited "back" functionality by creating a small, say 10 element, session array, so users could go back over the previous 10 pictures. Each new random id pushed onto the stack, the oldest one drops. – a coder Feb 08 '13 at 04:13