Jump to content

using shuffle() to randomize an array


hellonoko

Recommended Posts

I have a script that creates a xspf playlist from a directory of files.

 

However when I try to make the playlist random by using: shuffle(); instead of arsort(); the script write 0 1 2 3 etc to the XML file rather than the file names as arsort(); does.

 

The line in question is 59: shuffle($arr);

 

Any ideas?

 

<?php

//require_once("/flashupload2/config.php");

////////////////////////////////////////////////
///////////////////////////////////////////////

$username = "sharingi_music";
$password = "music";
$hostname = "localhost";

$db_connect = mysql_connect( $hostname , $username, $password) or die ("Unable to connect to database");

mysql_select_db( "sharingi_music" , $db_connect);

/////////////////////////////////////////////////
////////////////////////////////////////////////

// create doctype
$dom = new DOMDocument("1.0");

// display document in browser as plain text
// for readability purposes
//header("Content-Type: text/plain");

// create root element
$root = $dom->createElement("playlist");
$dom->appendChild($root);


// create attribute node
$version = $dom->createAttribute("version");
$root->appendChild($version);

// create attribute value node
$versionValue = $dom->createTextNode("1.0");
$version->appendChild($versionValue);

// create attribute node
$xmnls = $dom->createAttribute("xmlns");
$root->appendChild($xmnls);

// create attribute value node
$xmnlsValue = $dom->createTextNode("http://xspf.org/ns/0/");
$xmnls->appendChild($xmnlsValue);

// create track element
$trackList = $dom->createElement("trackList");
$root->appendChild($trackList);


	$dir = '/home2/sharingi/public_html/REPOSITORY/';

	foreach (glob( $dir.'*.*') as $filename) 
   		{
      		$arr[$filename] = filemtime($filename);
   		}

	shuffle($arr);

	foreach($arr as $key => $value) 
	{
     	
		if ( $key !== "/home2/sharingi/public_html/REPOSITORY/filelist3.php" && $key !== "/home2/sharingi/public_html/REPOSITORY/index.php" )
		{

			$key = str_replace( "/home2/sharingi/public_html/REPOSITORY/" , "" , $key);

			//echo $key;
			//echo '<br>';

			// create track element
			$track = $dom->createElement("track");
			$trackList->appendChild($track);

				// create child element of track called location.
				$location = $dom->createElement("location");
				$track->appendChild($location);

					//create location link
					$link = $dom->createTextNode("http://www.site.com/REPOSITORY/". $key);
					$location->appendChild($link);

				// create child element of track called title.
				$title = $dom->createElement("title");
				$track->appendChild($title);

					///get the song name//

					$result = mysql_query("SELECT SONG FROM songs WHERE HIDDENNAME = '$key'");

					while ($row = mysql_fetch_row($result))
					{
						$songname =  $row[0];
					}

					//create title data
					$songTitle = $dom->createTextNode("$songname");
					$title->appendChild($songTitle);


		}
	}

// save and display tree
$dom->save("playlist.xspf");

mysql_close ( $db_connect);

?>


<object type="application/x-shockwave-flash" width="400" height="170"
data="http://www.site.com/fullplayer/xspf_player.swf?playlist_url=http://www.site.com/fullplayer/playlist.xspf">
<param name="movie" 
value="http://www.site.com.com/fullplayer/xspf_player.swf?playlist_url=http://www.site.com/fullplayer/playlist.xspf" />
</object>

 

 

 

Link to comment
Share on other sites

Shuffle restructures the array/rebuilds it.

 

array_rand look into using that, as it leaves your array in-tact but will just give you a random order of keys.

 

$arrayKeys = array_rand($array, count($array));

 

I am not sure if that will return duplicate keys, but give that a try and see if that works for you.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.