Traveller29 Posted October 2, 2009 Share Posted October 2, 2009 Morning all - I am very new to PHP so please be gentle! I currently use the <bgsound ...> in an HTML file which plays a .wav sound byte on opening. I want to provide a random sound (from a list of six) but can't seem to find a PHP script anywhere that will provide this for me. I can find random images and have this working just fine! Could some kind soul point me in the right direction please? MTIA, Quote Link to comment Share on other sites More sharing options...
Bricktop Posted October 2, 2009 Share Posted October 2, 2009 Hi Traveller29, You could place the sounds in an array and then use the array_rand() function to choose a random sound. e.g.: $sounds = array("sound1.mp3", "sound2.mp3", "sound3.mp3", "sound4.mp3", "sound5.mp3"); $rand_sound = array_rand($sounds); echo '<BGSOUND SRC="'.$rand_sound.'">'; array_rand() manual: http://us2.php.net/manual/en/function.array-rand.php Hope this helps. Quote Link to comment Share on other sites More sharing options...
Traveller29 Posted October 2, 2009 Author Share Posted October 2, 2009 Thanks for that but no luck so far?! As I said, I am new to PHP so can you confirm what exactly I do? I put the array into a file (changing the .mp3 to .wav) and made a file called random_sound.php. I then included in my HTML file a calling line <?php include("random_sound.php"); ?> and put the six sounds in my root directory. one thing just crossed my mind - the index file I am using is .HTML on the server; does this have to be .PHP also?? Quote Link to comment Share on other sites More sharing options...
Bricktop Posted October 2, 2009 Share Posted October 2, 2009 Hi Traveller29, Don't bother about putting the code into a separate file. Rename your index.html to index.php and stick the code into the top of your page exactly like: <?php $sounds = array("sound1.wav", "sound2.wav", "sound3.wav", "sound4.wav", "sound5.wav"); $rand_sound = array_rand($sounds); ?> Then, scroll down your page until you come across the php code which deals with the background sound and change it to read: <BGSOUND SRC="<?php echo $rand_sound; ?>"> Hope this helps. Quote Link to comment Share on other sites More sharing options...
thebadbad Posted October 2, 2009 Share Posted October 2, 2009 array_rand() returns a key, so you would have to use $sounds[$rand_sound] instead of $rand_sound. Quote Link to comment Share on other sites More sharing options...
Bricktop Posted October 2, 2009 Share Posted October 2, 2009 Whoops, yes I did put that orginally, then amended it for some reason! Doh! Traveller29, use: <BGSOUND SRC="<?php echo $sounds[$rand_sound]; ?>"> Hope this helps. Quote Link to comment Share on other sites More sharing options...
Traveller29 Posted October 2, 2009 Author Share Posted October 2, 2009 Thank you gents - BINGO!! Works a treat now - very greatful for your input. Just one other question - now I have my index named as a .PHP what is the best way to be able to view locally? I use CodeLobster for the PHP editing but the viewer doesn't view the files in the entriety. Quote Link to comment Share on other sites More sharing options...
Bricktop Posted October 2, 2009 Share Posted October 2, 2009 Hi Traveller29, To run PHP files locally install a WAMP package, Google "WAMP" (there are loads to choose from) or have a look at: http://www.wampserver.com/en/ or http://www.uniformserver.com/ Hope this helps. Quote Link to comment Share on other sites More sharing options...
Traveller29 Posted October 2, 2009 Author Share Posted October 2, 2009 Installed both (one at a time!) without a lot of success? When I click on the index.php it still opens in CodeLobster but if I open it in a server then it says 'localhost' - Viewer is running TeamViewer (I know I am but only when doing some remote work which I am not and the proggy is 'dormant'!?). Will persist over the weekend ... onwards! Thanks again for your patience. Quote Link to comment Share on other sites More sharing options...
Traveller29 Posted October 2, 2009 Author Share Posted October 2, 2009 Well, I seem to have WAMPserver working as I can now open my PHP project site in Firefox! Changed all the HTML links to PHP and they all seem to call OK - with the exception of the sound as previously sorted!? btw, in my original <bgsound src= ...> code line there was 'loop="1" /' afetr the wav file. How do I get that into the array please? Other than that not really a problem other than I like to have things working entirely for test purposes :-/ I mentioned the 'localhost' and 'TeamViewer' irregulatory. Well the only way I could get the above to work was to remove the TeamViewer files running in the background; now that can't be right can it? Ho hum .... Quote Link to comment Share on other sites More sharing options...
Bricktop Posted October 2, 2009 Share Posted October 2, 2009 Hi Traveller 29, To get the loop code in, just change your code to read: <BGSOUND SRC="<?php echo $sounds[$rand_sound]; ?>" loop=1> With regard to TeamViewer, have a look here for a workaround http://tech.nabtron.com/run-teamviewer-with-localhost-webserver-xampp-wamp-etc/310/ Hope this helps. Quote Link to comment Share on other sites More sharing options...
Traveller29 Posted October 2, 2009 Author Share Posted October 2, 2009 Taa for that - my code is now complete! TeamViewer: I will have a look at that but I would have preferred a fix for the WAMPserver port listening rather than mess with TeamViewers! Firefox sound: Seems there is a 'bug' in firefox that stops the <bgsound> statement from working? 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.