Hello there,
I'm coping with a problem which drives me mad. For days, I am searching for a solution to display XML data in Flash which has been sent from PHP. To understand why I all want this, let me explain.
I have a game in Flash where people can post their highscores when the game is over. The highscore will be sent from AS3 to the MySQL database, where PHP grabs it with a query. This part is working succesfully. But, I want to sent the values back to Flash to show the best highscores. I tried to generate an XML in my PHP and call it from my ActionScript. I get this:
PHP Code:
<data><player><id>51</id><user></user><score></score></player><player><id>52</id><user></user><score></score></player><player><id>53</id><user></user><score></score></player><player><id>57</id><user></user><score></score></player><player><id>58</id><user></user><score></score></player><player><id>34</id><user>sdfd</user><score>0</score></player><player><id>38</id><user>987d</user><score>0</score></player><player><id>39</id><user>Niet</user><score>0</score></player><player><id>50</id><user>test!</user><score>0</score></player><player><id>59</id><user>Dit is een te</user><score>0</score></player></data></highscores>
Those are my database values, wrapped up in an xml tree in PHP. But I just need the values to be displayed into Flash. How do I do this??
PHP Code:
public function loadHighScores():void
{
var myrequest:URLRequest = new URLRequest("http://stap.iam.hva.nl/~schildm001/Game/getHighScoreList.php");
myrequest.method = URLRequestMethod.POST;
var variables:URLVariables = new URLVariables();
variables.test = "Hello";
trace(variables);
myrequest.data = variables;
var loadPHP:URLLoader = new URLLoader();
loadPHP.dataFormat = URLLoaderDataFormat.VARIABLES;
loadPHP.addEventListener(Event.COMPLETE, dataOnLoad);
loadPHP.load(myrequest);
}
private function dataOnLoad(e:Event):void
{
var xmlData:XML = XML(e.target.data);
trace(xmlData.player[0]);
trace(xmlData.player.id[0]);
trace(unescape(xmlData[0]));
}
PHP Code:
<?php
//Include database connection details
require_once('connection.php');
//Connect to mysql server
$link = mysql_connect(DB_SERVER, DB_USER, DB_PASS);
if(!$link) {
die('Failed to connect to server: ' . mysql_error());
}
//Select database
$db = mysql_select_db(DB_NAME);
if(!$db) {
die("Unable to select database");
}
$list = mysql_query("SELECT id, user, score FROM highscores ORDER BY score ASC LIMIT 10 ", $connection);
if(!$list)
{
die ("db query failed" . mysql_error());
}
$results = '<data>';
while ($row = mysql_fetch_array($list))
{
$results .= "<player>";
$results .= "<id>".$row['id']."</id>";
$results .= "<user>".$row['user']."</user>";
$results .= "<score>".$row['score']."</score>";
$results .= "</player>";
}
$results .= '</data>';
print($results);
while($row = mysql_fetch_assoc($list))
{
echo "<name>" . $row["user"] . "</name>\n";
}
echo "</highscores>\n";
?>
<!-- while ($row = mysql_fetch_array($list))
{
echo $row['user'] . $row['score'];
} -->
<!--$test = "This is a test";
$test2 = "This is a test2";
echo $test;
echo $test2;
-->
Thanks in advance!
Mushrambo