Jump to content

Displaying random images on page load


Rectifier

Recommended Posts

I would like to be able to display 5-10 random images on a page, and have 5-10 different images displayed if the page is visited again, or reloaded.

I've got the displaying the images part done, but the randomizing is faulty. The images are displayed in a random order, but when the page is refreshed/reloaded, or visted again, they are always in the same order (say images 1,5,3,8,4 are displayed, reload, and the same images are displayed in the same order)

[code]<?php
$pics = array("img1.jpg", "img2.jpg", "img3.jpg","img4.jpg", "img5.jpg");
shuffle($pics);
for ( $i = 0; $i < 5; $i++ )
{
echo '<img src="portfolio/' . $pics[$i] . '" border="0">&nbsp;&nbsp;';
}
?>[/code]

The shuffle(); function doesn't seem to make it randomize the elements in the array the way I want. Is there another function that does what shuffle(); does, but randomizes  the elements in the array the way I want? (Meaning, on reload it reorders the elements in the array.)
Link to comment
Share on other sites



Hi Rectifier,

it sounds like you're using a PHP prior to 4.2, According to the docs, you don't need to call srand on PHP >= 4.2.0.

If your PHP is an earlier version, try this.

[code]
<?php
$numbers = range(1, 20);
srand((float)microtime() * 1000000);
$pics = array("img1.jpg", "img2.jpg", "img3.jpg","img4.jpg", "img5.jpg");
shuffle($pics);
for ( $i = 0; $i < 5; $i++ )
{
echo '<img src="portfolio/' . $pics[$i] . '" border="0">&nbsp;&nbsp;';
}
?>
[/code]

Jeff
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.