rich_traff Posted September 23, 2010 Share Posted September 23, 2010 Hi, im trying to apply a random css class to a div so that a random image is generated on each refresh. This is the code i am using to apply the class. <div class="<?php echo($randomimage); ?>"> Say i have an array of $image1 $image2 $image3 etc, how can i make $randomimage = 1 of these at random on each refresh? I've found lots of tutorials that generate a random number online but none that generate a random variable and am having trouble piecing this together. Any help would be hugely appreciated. Richard Quote Link to comment Share on other sites More sharing options...
Chris92 Posted September 23, 2010 Share Posted September 23, 2010 <div class="<?php echo $image[rand(1,3)]; ?>"> Quote Link to comment Share on other sites More sharing options...
rich_traff Posted September 23, 2010 Author Share Posted September 23, 2010 Thats great, thanks very much Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted September 23, 2010 Share Posted September 23, 2010 If the image filenames are in an array, you'd be better off using array_rand(). Then if the number of files changes, you don't have to edit the rand() function parameters. IOW, it would be more scalable. 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.