Ive currently got the below code which builds an array.
I run this with the following commands: build_playlist_array('A'); build_playlist_array('B'); build_playlist_array('C');
Code:
function build_playlist_array($list)
{
$playlist_query = "SELECT * FROM t_playlist WHERE list = '" . $list . "'";
$playlist_result = mysql_query($playlist_query) or die(mysql_error());
while($row = mysql_fetch_array($playlist_result))
{
$items[] = $row['songid'];
}
}
Id now somehow like to get the a random item from each array! Any ideas how i can accomplish this? (I will then be running the command using if's and loops to return 3 random items which are obviously not the same as each other)
edit: this solves the problem of how to select songs randomly, but you may also want to store each random selection to a variable and check it each time through the loop to prevent php's rand() function from selecting the same random number twice in a row and playing the same song twice in a row...If your items array is large, back to back selection would be much more rare than if the items array was small...but it still would be cheesy to play the same song twice.
This method does not depend upon the array being sequentially enumerated (e.g. it will work with associative arrays or enumerated arrays with gaps in the indexes).
"Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
~ Terry Pratchett in Nation
This method does not depend upon the array being sequentially enumerated (e.g. it will work with associative arrays or enumerated arrays with gaps in the indexes).
"Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
~ Terry Pratchett in Nation
Bookmarks