Omzy Posted December 22, 2009 Share Posted December 22, 2009 I need help writing some code to display 7 random images from a folder. I have managed to make it display one random image but I just cannot figure out how to make it display multiple random images from a set of 20. The images in the folder will always be JPEG and are named 'image_x.jpg' (x = a number from 1-20) Optionally it could auto-count the number of images in the folder to dynamically assign the end value, but that isn't too important. Quote Link to comment Share on other sites More sharing options...
oni-kun Posted December 22, 2009 Share Posted December 22, 2009 I need help writing some code to display 7 random images from a folder. I have managed to make it display one random image but I just cannot figure out how to make it display multiple random images from a set of 20. The images in the folder will always be JPEG and are named 'image_x.jpg' (x = a number from 1-20) Optionally it could auto-count the number of images in the folder to dynamically assign the end value, but that isn't too important. I'd recommend glob and array_rand EDIT: You can use count on the array of files to get the auto-count, but it's pointless to append numbers to a filename, like that, not as fast. Quote Link to comment Share on other sites More sharing options...
Omzy Posted December 22, 2009 Author Share Posted December 22, 2009 OK I tried using array_rand() however I realised I need to output ALT text on the images. So I created an associative array ('$x'=>'alt text') and then ran a foreach loop over the array_rand() array. But I don't think array_rand() works with associatiave arrays. Is there a better way of doing this? Quote Link to comment Share on other sites More sharing options...
Psycho Posted December 22, 2009 Share Posted December 22, 2009 OK I tried using array_rand() however I realised I need to output ALT text on the images. So I created an associative array ('$x'=>'alt text') and then ran a foreach loop over the array_rand() array. But I don't think array_rand() works with associatiave arrays. Is there a better way of doing this? If you were to provide the code you have it would be easier for us to see what you were already doing and provide an appropriate solution. Here is what I would do: //Array to hold jpeg names and associative text $imageList = array( 'image_1.JPG' => 'Text for image 1.', 'image_2.JPG' => 'Text for image 2.', 'image_3.JPG' => 'Text for image 3.', 'image_4.JPG' => 'Text for image 4.', 'image_5.JPG' => 'Text for image 5.' /// Etc. ); //Create array of just the images $images = array_keys($imageList); //Randomize the image array shuffle($images); //Get 7 random images $randomImages = array_slice($images, 0, 7); //Display the images and the text foreach($randomImages as $image) { echo "<img src=\"folderpath/{$image}\"><br />\n"; echo "{$imageList[$image]}<br />\n"; } Quote Link to comment Share on other sites More sharing options...
Omzy Posted December 22, 2009 Author Share Posted December 22, 2009 Cheers mjdamato, however your code does not output the alt text... Quote Link to comment Share on other sites More sharing options...
ignace Posted December 22, 2009 Share Posted December 22, 2009 @omzy no alt text was defined.. @mjdamato trying to answer to quick ? Because you forgot the alt attribute which is required on the img tag. Quote Link to comment Share on other sites More sharing options...
Omzy Posted December 22, 2009 Author Share Posted December 22, 2009 I managed to figure it out. You can also do: $randomImages = array_rand($imageList, 7); and then you don't even need to use array_keys(), shuffle() or array_slice() Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.