-4

I have a div with an UL list in there, which currently has 20 images in, all named 1.jpg - 20.jpg.

I wanted to change this and have the images displayed in a random order on each page load / refresh.. I was wondering if you are able to help point me in the right direction?

All the images are on the webserver in "images/"

At the moment I need the images to fall in line with the below code:

<li><a></a><a><img alt="" src="images/Random1.jpg" /></a></li>
<li><a></a><a><img alt="" src="images/Random2.jpg" /></a></li>
<li><a></a><a><img alt="" src="images/Random3.jpg" /></a></li>
<li><a></a><a><img alt="" src="images/Random4.jpg" /></a></li>

At the moment I've tried several different bits of code I've seen on stack overflow, but I can't seem to get it working.

Dharman
  • 30,962
  • 25
  • 85
  • 135
emmy
  • 33
  • 4

1 Answers1

0
$range = range(0,10); //how many pictures you have
shuffle($range);
foreach($range as $index => $value)
{
  echo "<li><a></a><a><img alt='' src='images/Random".$value.".jpg' /></a></li>";
}
Mike
  • 1
  • 1
  • Thank's for writing back! I'm getting the below error when putting the code in... any ideas? Parse error: syntax error, unexpected 'as' (T_AS), expecting ';' in /var/sites/xxx/auto.php on line 20 – emmy Oct 01 '19 at 09:43
  • the `for` should be `foreach` in that snippet. – moopet Oct 01 '19 at 09:47
  • Wanted to help fast, it was a mistake, it is foreach instead of for. – Mike Oct 01 '19 at 13:04