1

I have researched this question a lot, yet have not found a suitable solution.

What I have is a few images using album covers and when you click on them, I want a random song from the album to be pulled. The albums are 01-05, and their cover images are in them. When clicked, they call song = "<?php echo (random_song("album_number")); ?>"; which is then embedded in a div with id #player.

PHP:

<?php
 function random_song($dir) {
  $files = array (
   (array) glob('01/*.mp3'),
   (array) glob('02/*.mp3'),
   (array) glob('03/*.mp3'),
   (array) glob('04/*.mp3'),
   (array) glob('05/*.mp3')
  );

  $set = substr($dir,0,2) - 1;
  shuffle($files[$set]);
  $key = array_rand($files[$set]);
  $file = $files[$set][$key];
  return $file;
 }
?>

So far, I have gotten it to play a song; however, it never picks another song. If I click on one album, it will pick a song; click on a second one, it will play a song. Click back on the first one, and it plays the same song.

I have tried adding shuffle(), as you can see, but it has not made a difference. I have also done a var_dump() on every part of $files to ensure that glob() is working; there have been no problems.

I'm stuck. What do I need to do?


Solution:

What Fritz van Campen said was correct: the php is only called once. It was written in the <script> and called as soon as it was parsed, therefore each album only called upon the song it was originally coded for. What I needed to do, as he suggested, was to move the song picker to JavaScript. My Solution:

var songs = <?php echo json_encode($files); ?>;
...
var set = parseInt(dir.substr(0,2))-1;
var song = songs[set][Math.floor(Math.random() * songs[set].length)];

...which transfers the array created by the php to JavaScript, and uses the same system to pick a random song.

Thank you for answering!

2 Answers2

0

You're only asking PHP to pick and random value once. What is the other language you're using, JavaScript? If so, move the random song picker to JavaScript.

PHP runs on the server. JavaScript runs in the browser. Once the server returns the HTML page PHP is done and will not run again.

Halcyon
  • 57,230
  • 10
  • 89
  • 128
  • I am calling the PHP every time an album is clicked. The album image's `src` is grabbed (since the albums change,) and from there the php is called to return the randomly selected song. It can run for each album but doesn't return a different song _per album_. It's running every time, but for some reason always returns the same key despite shuffling. – Carson Wilber Mar 27 '13 at 18:10
  • No, you're not. You think you are but you're not. Infact, you can't because it's impossible. PHP is server-side, not client-side. – Halcyon Mar 27 '13 at 18:13
  • If not then why can I call it for each album separately and it still run? – Carson Wilber Mar 27 '13 at 18:15
  • Ah, I understand! Solution added to the thread. Thank you very much! – Carson Wilber Mar 27 '13 at 20:55
0

Solution 1 = Server Side

I am assuming clicking the album sends a new query for a new page that has the random song off the album you clicked. I am also assuming you have a tree structure like:

Albums/
  01/
    Track01.mp3
    Track02.mp3
    Track03.mp3
    ...
  02/
  03/
  ...

I would have the link include the album number to the album and pass that to $dir. Then just glob that directory. Once I have an array of filenames I use array_rand to pull out a random filename.

<?php
  function random_song($dir) {
    $root = "Albums/"
    if(is_dir($root.$dir)){
      $songs = glob($root.$dir.'/*.mp3');
      return $root.$dir.array_rand($songs);
    }
    //Error
  }
?>

Solution 2 - Client Side If the page isn't reloading every time the album image is clicked I would include a ul in the album image div with display:none; and in each li have the path to each song. Then when the album div was clicked I would use JQuery choose a random child li (example here) and read the contents of the li and pass that string to the player.

Community
  • 1
  • 1
Tyson of the Northwest
  • 2,086
  • 2
  • 21
  • 34
  • This is what I'm doing - the problem is, `array_rand` only ever gives me one song. It doesn't pick another one if I call the function again. If I reload the page, it will pick a different song for each album - but that will always be the song. If I click the album again, it won't pick a different song: it will continue to play the song it picked the first time. – Carson Wilber Mar 27 '13 at 20:09
  • you may want to try `$songs = shuffle($songs); return $song[0];` Also you may want to loop through a bunch of shuffles just to test. It is possible that your server is not changing the random seed before you shuffle. Resulting in the same sequence of random values every time you run test. – Tyson of the Northwest Mar 27 '13 at 20:14
  • also what about `return $root.$dir.$songs[mt_rand(0,count($songs)];`? – Tyson of the Northwest Mar 27 '13 at 20:20
  • Oh! I see what Frits says. The php in my JavaScript calling for a file is only called once because it is filled in at the beginning, so the song is set when the php is called. That's why it sets the same each time. Hmm... – Carson Wilber Mar 27 '13 at 20:25