Page 1 of 1
XML - PHP - JavaScript Help
Posted: Sun Apr 19, 2009 10:05 pm
by Brad7928
I have a .xml file with this contents:
Songs.xml
Code: Select all
<?xml version="1.0" encoding="UTF-8"?>
<xml>
<track>
<path>songs/song1.mp3</path>
<title>song 1</title>
</track>
<track>
<path>songs/song2.mp3</path>
<title>song 2</title>
</track>
<track>
<path>songs/song3.mp3</path>
<title>song 3</title>
</track>
<track>
<path>songs/song4.mp3</path>
<title>song 4</title>
</track>
</xml>
I have a flash player that views that list and displays the content in that order. I'm wanting to make a shuffle button that when clicked, uses JavaScript or PHP to shuffle each <track> ... </track> tag.
I've done a bit of research and found that it mainly points to Javascript, creating an array, randomise the array then write it back into the XML file, or a XML file, or PHP, not sure how to do it, i know Javascript can't save the write the random array back though.
I have no idea how to do this, i only know very basic PHP and javascript.
Any ideas?
Re: XML - PHP - JavaScript Help
Posted: Mon Apr 20, 2009 4:22 am
by MasterBeta
The only line of code you need to change is line 2 to your xml file name. You will need to add the path if this file is not in the same directory as your xml file.
This will replace your existing xml file with the shuffled data (
make a backup first)!
Code: Select all
<?php
$xml_file = "songs.xml"; // your XML file. you can add a directory if needed (xml/songs.xml or ../songs.xml)
if($_POST['form_submit']){
$xml = simplexml_load_file("$xml_file");
$i=1;
foreach($xml->track as $value){
$trackList[$i]['path'] = "$value->path";
$trackList[$i]['title'] = "$value->title";
$i++;
}
shuffle($trackList);
foreach($trackList AS $value) $newXML .= "<track>\n<title>".$value['title']."</title>\n<path>".$value['path']."</path>\n</track>\n\n";
$newXML = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<xml>\n".$newXML."</xml>";
$file = fopen("$xml_file","w");
$writeFile = fwrite($file,"$newXML");
fclose($file);
if($writeFile) print "Shuffle Complete";
else print "There was a problem writing the file";
}else{
?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" enctype="multipart/form-data" name="shuffle">
<input type="submit" name="submit" id="submit" value="Shuffle"><input name="form_submit" type="hidden" value="1">
</form>
<?php
}
?>
Re: XML - PHP - JavaScript Help
Posted: Mon Apr 20, 2009 4:35 am
by jazz090
yh it spossible but CERTAINLY not the best way to go about doing it, i am all PHP-AS3 hardocre and i can tell u that th ebest way u can go about this is to first indentify ur database: this can be an static xml file which u hardcoded from scratch or a more dynamic xml in which u load data from a mysql, oracle or sqlite and u use php to generate a xml file for you which u can load into flash. note that javascript comes no where near in what u gonna do, u dont even need it. if u harcoded your xml just load that into ur AS file, i dont know which one u do but in AS3 i would do it like this:
Code: Select all
var xmlLoader:URLLoader = new URLLoader();
var xmlData:XML = new XML();
xmlLoader.addEventListener(Event.COMPLETE, LoadXML);
xmlLoader.load(new URLRequest("static_xml_file.xml"));
function LoadXML(e:Event):void {
xmlData = new XML(e.target.data);
trace(xmlData);
}
if u wanna get a more dynamic result from a database, its even simplrer, simply run a header content type on ur php file and assign a application/xml mime-type and just use the same thing but chnage the extension to php:
Code: Select all
xmlLoader.load(new URLRequest("dynamic_xml_file.php"));
now in AS3 or 2 or whatever, parse this XML and put it into an array and simply shuffle the array with AS instead of complicated the whole procedure with java.
Re: XML - PHP - JavaScript Help
Posted: Mon Apr 20, 2009 7:41 am
by Brad7928
Both seem like very good options.
I was going to go for the database option, but then it seemed very very hard, and as i don't know much php, i kinda hit a dead end.
What would be good is a front-end that has the flash player reading the XML file that is generated via a database.
Then have a back end where you can add the songs within a certain folder (on the server) to a the databasem maybe even a genre's so playlists can be made based on genre's?
I'd be happy for anyone to help on this one and even claim all the credit for it! I have spent countless weeks trying to get this working and haven't gotten very far, i have a server running php5 and mysql, just need the php knowledge.
But then again, if someone could point me in the right direction (well maybe a hard nudge would be better) that would be great!
Cheers,
Brad