-1

For example, say I want an array of songs which gets randomized, outputting a song title.

  var myArray = ['song1', 'song2', 'song3'];
  var random = myArray[Math.floor(Math.random() * myArray.length)];
  document.write(random);

but if I also wanted that songs album cover plus the description of the artist, is that possible? I imagine it is if I were to manually add each one individually to my code, but what if I wanted 1000's of songs?

roguerat
  • 219
  • 3
  • 16
  • 1
    Yes. it's possible. Coding for thousands of songs will likely call for a database and ajax interactions for search capability and reasonable application load times. – Radio Mar 31 '15 at 00:13
  • Don't use [document.write](http://stackoverflow.com/questions/802854/why-is-document-write-considered-a-bad-practice). – epascarello Mar 31 '15 at 00:20
  • @Radio hm, say we limit it to 2,000 titles with an image and short artist summery. Without a database and ajax how long would a load time be with randomize? I don't want a search functionality. – roguerat Mar 31 '15 at 00:22
  • @epascarello thank's for the link, I didn't know that! – roguerat Mar 31 '15 at 00:24

1 Answers1

1

Avoid document.write. By putting each Song Object inside of your Array you can achieve this:

var myArray = [{title:'Song 1 Title', artist:'Song 1 Artist'}, {title:'Song 2 Title', artist:'Song 2 Artist'}, {title:'Song 3 Title', artist:'Song 3 Artist'}];
function getRandomSong(ary){
  return ary[Math.floor(Math.random()*ary.length)];
}
var randomSong = getRandomSong(myArray);
console.log(randomSong.title); console.log(randomSong.artist);

Of course, if you have thousands of songs you'll want to execute something similar Sever Side on your database. In PHP this might look like:

<?php
class Song{
  private $db; public $currentSelection;
  public function __construct($databaseConnection){
    $this->db = $databaseConnection;
    $this->currentSelection = $this->getRandomSong();
  }
  public function getRandomSong(){
    $db = $this->db;
    if($sq = $db->query('SELECT COUNT(*) count FROM songTable')){
      if($sq->num_rows > 0){
        $c = $sq->fetch_object(); $rc = rand(1, $c->count);
        if($rq = $db->query("SELECT * FROM songTable WHERE primaryKey=$rc"){
          if($rq->num_rows > 0){
            return json_encode($rq->fetch_assoc());
          }
          else{
            // no rows
          }
          $rq->free();
        }
        else{
          // connection failure
        }
      } 
      else{
        // no rows
      }
      $sq->free();
    }
    else{
      // connection failure
    }
    return false;
  }
}
$songObj = new Song($databaseConnectionHere);
echo $songObj->currentSelection;
echo $songObj->getRandomSong();
$dataBaseConnectionHere->close();
?>
StackSlave
  • 10,198
  • 2
  • 15
  • 30
  • But when I use console.log it only displays it in the console not the window? So the user wouldn't be able to see this unless they opened up the console? – roguerat Mar 31 '15 at 00:44
  • You have to be kidding. `document.getElementById('someId').innerHTML = randomSong.title` is one way. – StackSlave Mar 31 '15 at 01:16