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; ?> Link to comment https://forums.phpfreaks.com/topic/285172-shuffle-issue/ 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? Link to comment https://forums.phpfreaks.com/topic/285172-shuffle-issue/#findComment-1464281 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? Link to comment https://forums.phpfreaks.com/topic/285172-shuffle-issue/#findComment-1464289 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. Link to comment https://forums.phpfreaks.com/topic/285172-shuffle-issue/#findComment-1464291 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) Link to comment https://forums.phpfreaks.com/topic/285172-shuffle-issue/#findComment-1464292 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? Link to comment https://forums.phpfreaks.com/topic/285172-shuffle-issue/#findComment-1464293 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. Link to comment https://forums.phpfreaks.com/topic/285172-shuffle-issue/#findComment-1464295 Share on other sites More sharing options...
jazzman1 Posted January 7, 2014 Share Posted January 7, 2014 Your first shuffled file into an array could be: $Songs = glob($SongsFolder."/*.mp3"); shuffle($Songs); echo $Songs[0]; // or am I wrong? Link to comment https://forums.phpfreaks.com/topic/285172-shuffle-issue/#findComment-1464299 Share on other sites More sharing options...
GetFreaky Posted January 7, 2014 Share Posted January 7, 2014 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. Link to comment https://forums.phpfreaks.com/topic/285172-shuffle-issue/#findComment-1464301 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. Link to comment https://forums.phpfreaks.com/topic/285172-shuffle-issue/#findComment-1464325 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. Link to comment https://forums.phpfreaks.com/topic/285172-shuffle-issue/#findComment-1464329 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. Link to comment https://forums.phpfreaks.com/topic/285172-shuffle-issue/#findComment-1464331 Share on other sites More sharing options...
GetFreaky Posted January 7, 2014 Share Posted January 7, 2014 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. Link to comment https://forums.phpfreaks.com/topic/285172-shuffle-issue/#findComment-1464332 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. Link to comment https://forums.phpfreaks.com/topic/285172-shuffle-issue/#findComment-1464334 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. Link to comment https://forums.phpfreaks.com/topic/285172-shuffle-issue/#findComment-1464347 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? Link to comment https://forums.phpfreaks.com/topic/285172-shuffle-issue/#findComment-1464391 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? Link to comment https://forums.phpfreaks.com/topic/285172-shuffle-issue/#findComment-1464404 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; ?> Link to comment https://forums.phpfreaks.com/topic/285172-shuffle-issue/#findComment-1464405 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'? Link to comment https://forums.phpfreaks.com/topic/285172-shuffle-issue/#findComment-1464432 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! Link to comment https://forums.phpfreaks.com/topic/285172-shuffle-issue/#findComment-1464472 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. Link to comment https://forums.phpfreaks.com/topic/285172-shuffle-issue/#findComment-1464473 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. Link to comment https://forums.phpfreaks.com/topic/285172-shuffle-issue/#findComment-1464474 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 Link to comment https://forums.phpfreaks.com/topic/285172-shuffle-issue/#findComment-1464475 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 Link to comment https://forums.phpfreaks.com/topic/285172-shuffle-issue/#findComment-1464477 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.