Page 1 of 1

Random File Selection.

Posted: Sun Jan 24, 2010 8:32 pm
by awm6392
I am pretty new to PHP and am trying to develop a theme for a Drupal website. I have 5 swf files (pic_x.swf where x is 1-5) The code I use to embed these files is:

Code: Select all

<embed type="application/x-shockwave-flash" src="<?php print $base_path . $directory; ?>/images/pic_x.swf" id="pic" name="pic" quality="high" wmode="transparent" width="300" height="150">
My question is: how can I use PHP to generate a random number to replace "x" in "pic_x.swf" so that one of the 5 files is randomly selected and displayed each time the page loads?

Cheers.

Re: Random File Selection.

Posted: Sun Jan 24, 2010 8:39 pm
by John Cartwright
Take a look at rand() or mt_rand()

Re: Random File Selection.

Posted: Sun Jan 24, 2010 8:42 pm
by Weiry

Code: Select all

 
<?php
$x = rand(1,5);
?>
<embed type="application/x-shockwave-flash" src="<?php print $base_path . $directory; ?>/images/pic_<?php print $x; ?>.swf" id="pic" name="pic" quality="high" wmode="transparent" width="300" height="150">
 

Re: Random File Selection.

Posted: Sun Jan 24, 2010 8:52 pm
by awm6392
Thank you very much, that helped a lot :D Been trying to find a solution to that problem on the Drupal website for a while now. Much appreciated.