Fijiannn Posted January 7, 2014 Share Posted January 7, 2014 Hi in my code below, echo $body; outputs the link to every song in my $SongsFolder. How can I make it echo only one link and that link is the song the $Shuffle chose? I know I have array but is there a way to get around it? Anything I should change up? <?php function fooFucker($url) { $a = join('/', array_map('urlencode', explode('/', $url))); return str_replace('+', '%20', $a); } $SongsFolder = "songs"; header("Content-Type: audio/x-mpegurl"); $body = ''; $Songs = glob($SongsFolder."/*.mp3"); shuffle($Songs); $first = 0; foreach($Songs as $Song) { $SongPath = pathinfo($Song); if ($first != 0) { $body .= "\n"; } $first = 1; $body .= "http://".$_SERVER['HTTP_HOST'].fooFucker(dirname($_SERVER['PHP_SELF']))."/".fooFucker($SongsFolder)."/".fooFucker($SongPath['basename']); } header('Content-Length: ' . strlen($body)); echo $body; ?> Quote Link to comment Share on other sites More sharing options...
ginerjm Posted January 7, 2014 Share Posted January 7, 2014 Might I suggest that you post code that others may not find patently offensive? Quote Link to comment Share on other sites More sharing options...
jazzman1 Posted January 7, 2014 Share Posted January 7, 2014 The shuffle function shuffles all files in the array. What did you mean by saying this: How can I make it echo only one link and that link is the song the $Shuffle chose? Quote Link to comment Share on other sites More sharing options...
Fijiannn Posted January 7, 2014 Author Share Posted January 7, 2014 The shuffle function shuffles all files in the array. What did you mean by saying this: When I echo $body it echo's the link to every mp3 song in my folder called "songs" ($SongsFolder) when all I want is for the shuffle() to just shuffle, choose a link to a song then output the link to the song it chose. Quote Link to comment Share on other sites More sharing options...
jazzman1 Posted January 7, 2014 Share Posted January 7, 2014 Well, use the foreach only to display all shuffled file and get the link of the foreach if you want to display the current shuffled file with index 0 (zero) Quote Link to comment Share on other sites More sharing options...
Fijiannn Posted January 7, 2014 Author Share Posted January 7, 2014 Well, use the foreach only to display all shuffled file and get the link of the foreach if you want to display the current shuffled file with index 0 (zero) Hmmm, I'm kinda new to this PHP stuff. Got an example? Quote Link to comment Share on other sites More sharing options...
ginerjm Posted January 7, 2014 Share Posted January 7, 2014 Where did you get the idea to use foreach???? From some other example? Read up on what foreach does. Then think about what you want to do and how to access arrays. And then the solution should be plain. Quote Link to comment Share on other sites More sharing options...
jazzman1 Posted January 7, 2014 Share Posted January 7, 2014 (edited) Your first shuffled file into an array could be: $Songs = glob($SongsFolder."/*.mp3"); shuffle($Songs); echo $Songs[0]; // or am I wrong? Edited January 7, 2014 by jazzman1 Quote Link to comment Share on other sites More sharing options...
GetFreaky Posted January 7, 2014 Share Posted January 7, 2014 (edited) Your first shuffled file into an array could be: $Songs = glob($SongsFolder."/*.mp3"); shuffle($Songs); echo $Songs[0]; // or am I wrong? Based on his previous thread, he believes that shuffle($Songs) in conjunction with $Songs[0] creates an array for each song, rather it the return value of Glob is an array which has all the songs in it Someone gave this solution to him before, not sure why the second thread. Edited January 7, 2014 by GetFreaky Quote Link to comment Share on other sites More sharing options...
Fijiannn Posted January 7, 2014 Author Share Posted January 7, 2014 Based on his previous thread, he believes that shuffle($Songs) in conjunction with $Songs[0] creates an array for each song, rather it the return value of Glob is an array which has all the songs in it Someone gave this solution to him before, not sure why the second thread. So this code above will give me the link to the chosen song after shuffle? That's what I don't get. Quote Link to comment Share on other sites More sharing options...
ginerjm Posted January 7, 2014 Share Posted January 7, 2014 You're off the track. Try this code. <?php $songsfolder = "songs"; header("Content-Type: audio/x-mpegurl"); $body = ''; $songs = glob($songsfolder."/*.mp3"); if (count($songs) == 0) { echo "Error - no songs found"; exit(); } shuffle($songs); $body .= "http://" . $_SERVER['HTTP_HOST'] . $songs[0]; header('Content-Length: ' . strlen($body)); echo $body; ?> Not sure about the syntax of my $body content since it's been awhile since I used glob and pathinfo functions. You may have to add a slash or another folder reference, but this will do what you want. Plus - not sure what at all you expect this to do since you are simply echoing a text line that has a url in it, but you have specified headers that imply something else. AND - your original post mentioned a "link", which is not in your code either. And it does it without using offensive or childish function names. Quote Link to comment Share on other sites More sharing options...
Fijiannn Posted January 7, 2014 Author Share Posted January 7, 2014 You're off the track. Try this code. <?php $songsfolder = "songs"; header("Content-Type: audio/x-mpegurl"); $body = ''; $songs = glob($songsfolder."/*.mp3"); if (count($songs) == 0) { echo "Error - no songs found"; exit(); } shuffle($songs); $body .= "http://" . $_SERVER['HTTP_HOST'] . $songs[0]; header('Content-Length: ' . strlen($body)); echo $body; ?> Not sure about the syntax of my $body content since it's been awhile since I used glob and pathinfo functions. You may have to add a slash or another folder reference, but this will do what you want. Plus - not sure what at all you expect this to do since you are simply echoing a text line that has a url in it, but you have specified headers that imply something else. AND - your original post mentioned a "link", which is not in your code either. And it does it without using offensive or childish function names. I'll try it when I get home but I wanted to make the link that it outputted a variable so I can call that variable in my iPhone app for something in my code. Quote Link to comment Share on other sites More sharing options...
GetFreaky Posted January 7, 2014 Share Posted January 7, 2014 (edited) So this code above will give me the link to the chosen song after shuffle? That's what I don't get. An array has a set of indexes, much like memory address, its has an address (key) and a value. -> SongArray [0] => "Song 1" [1] => "Song 2" [2] => "Song 3" - > Invoke Shuffle(SongArray) -> SongArray [0] => " Song 2" [1] => "Song 1" [2] => "Song 3" Index 0 of the array will most likely never contain the same value after using shuffle, so it only makes sense to grab the index 0. Edited January 7, 2014 by GetFreaky Quote Link to comment Share on other sites More sharing options...
ginerjm Posted January 7, 2014 Share Posted January 7, 2014 Do you know what a link is? And why do you have those header calls in there? If all you want is to display an html page with a link no it get rid of them. Quote Link to comment Share on other sites More sharing options...
Fijiannn Posted January 7, 2014 Author Share Posted January 7, 2014 An array has a set of indexes, much like memory address, its has an address (key) and a value. -> SongArray [0] => "Song 1" [1] => "Song 2" [2] => "Song 3" - > Invoke Shuffle(SongArray) -> SongArray [0] => " Song 2" [1] => "Song 1" [2] => "Song 3" Index 0 of the array will most likely never contain the same value after using shuffle, so it only makes sense to grab the index 0. Very good explanation man! See I just needed it explained like this. Quote Link to comment Share on other sites More sharing options...
GetFreaky Posted January 8, 2014 Share Posted January 8, 2014 Very good explanation man! See I just needed it explained like this. No problem, everyone helped out equally as well, Is your code working now? Quote Link to comment Share on other sites More sharing options...
Fijiannn Posted January 8, 2014 Author Share Posted January 8, 2014 No problem, everyone helped out equally as well, Is your code working now? Well I replaced my code with the code above. <?php $songsfolder = "songs"; header("Content-Type: audio/x-mpegurl"); $body = ''; $songs = glob($songsfolder."/*.mp3"); if (count($songs) == 0) { echo "Error - no songs found"; exit(); } shuffle($songs); $body .= "http://" . $_SERVER['HTTP_HOST'] . $songs[0]; header('Content-Length: ' . strlen($body)); echo $body; ?> It does give me a link "http://virsamusic.comsongs/21 Bharat Mata Ki Jai.mp3 " I messed up link, not linking to the actual song but it does get the name of the .mp3. Also, it's suppose to stream music and my music isn't playing. What could be wrong now? Quote Link to comment Share on other sites More sharing options...
Fijiannn Posted January 8, 2014 Author Share Posted January 8, 2014 No problem, everyone helped out equally as well, Is your code working now? So with a little editing on the code below. I got it to output the correct link to a song but the thing is my music isn't playing so I can't tell if link the console outputted is correct with the song playing. What's wrong? <?php function myFunction($url) { $a = join('/', array_map('urlencode', explode('/', $url))); return str_replace('+', '%20', $a); } $songsfolder = "songs"; header("Content-Type: audio/x-mpegurl"); $body = ''; $songs = glob($songsfolder."/*.mp3"); if (count($songs) == 0) { echo "Error - no songs found"; exit(); } shuffle($songs); $body .= "http://" . $_SERVER['HTTP_HOST'].myFunction(dirname($_SERVER['PHP_SELF']))."/".myFunction($SongsFolder).$songs[0]; header('Content-Length: ' . strlen($body)); echo $body; ?> Quote Link to comment Share on other sites More sharing options...
ginerjm Posted January 8, 2014 Share Posted January 8, 2014 Why not google 'how to play music from my web page'? Quote Link to comment Share on other sites More sharing options...
Fijiannn Posted January 8, 2014 Author Share Posted January 8, 2014 Why not google 'how to play music from my web page'? My orginal code already did that! Quote Link to comment Share on other sites More sharing options...
ginerjm Posted January 8, 2014 Share Posted January 8, 2014 Didn't work, did it? There's a lot of wrong stuff out there googleland. Keep trying. Quote Link to comment Share on other sites More sharing options...
Fijiannn Posted January 8, 2014 Author Share Posted January 8, 2014 Didn't work, did it? There's a lot of wrong stuff out there googleland. Keep trying. Your code is good but it just doesn't play my music anymore. Quote Link to comment Share on other sites More sharing options...
ginerjm Posted January 8, 2014 Share Posted January 8, 2014 my code? I just gave you what you had. I've never tried to play music on my web pages. Nope - that was your code with those headers Quote Link to comment Share on other sites More sharing options...
adam_bray Posted January 8, 2014 Share Posted January 8, 2014 This should give you the answer - http://www.w3schools.com/tags/tag_audio.asp 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.