Random File Selection.

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
awm6392
Forum Newbie
Posts: 2
Joined: Sun Jan 24, 2010 8:23 pm

Random File Selection.

Post 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.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: Random File Selection.

Post by John Cartwright »

Take a look at rand() or mt_rand()
User avatar
Weiry
Forum Contributor
Posts: 323
Joined: Wed Sep 09, 2009 5:55 am
Location: Australia

Re: Random File Selection.

Post 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">
 
awm6392
Forum Newbie
Posts: 2
Joined: Sun Jan 24, 2010 8:23 pm

Re: Random File Selection.

Post 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.
Post Reply