Hi All
I am trying to load data from mysql to xml to flash using actionscript and php. Im doing something wrong but not sure what.
Basically, my php file (latestnews.php) looks like this
Code:
<?php
header("Content-type: text/xml");
$host = "host";
$user = "user";
$pass = "password";
$database = "dbname";
$linkID = mysql_connect($host, $user, $pass) or die("Could not connect to host.");
mysql_select_db($database, $linkID) or die("Could not find database.");
$query = "SELECT * FROM latestnews ORDER BY timestamp DESC LIMIT 0,3";
$resultID = mysql_query($query, $linkID) or die("Data not found.");
$xml_output = "<?xml version=\"1.0\" encode=\"UTF-8\"?>\n";
$xml_output = "<!DOCTYPE data[
<!ELEMENT headline (text, date)>
<!ATTLIST headline name CDATA #REQUIRED>
<!ELEMENT text (#PCDATA)>
<!ELEMENT date (#PCDATA)>
]>\n";
$xml_output .= "<entries>\n";
for($x = 0 ; $x < mysql_num_rows($resultID) ; $x++){
$row = mysql_fetch_assoc($resultID);
$xml_output .= "\t<entry>\n";
$xml_output .= "\t\t<headline name=\" ". $row['headline'] . "\">\n";
// Escaping illegal characters
$row['content'] = str_replace("&", "&", $row['content']);
$row['content'] = str_replace("<", "<", $row['content']);
$row['content'] = str_replace(">", ">", $row['content']);
$row['content'] = str_replace("\"", """, $row['content']);
$xml_output .= "\t\t<text><![CDATA[" . $row['content'] . "]]></text>\n";
$xml_output .= "\t\t<date><![CDATA[" . $row['timestamp'] . "]]></date>\n";
$xml_output .= "\t</headline>\n";
$xml_output .= "\t</entry>\n";
}
$xml_output .= "</entries>";
echo $xml_output;
?>
this produces the following xml layout
Code:
<entries>
−
<entry>
−
<headline name=" This is a Test News">
<text>Test News Description</text>
<date>2009-05-17 19:40:33</date>
</headline>
</entry>
−
<entry>
−
<headline name=" Site launch 21st May 2009">
−
<text>
test news
</text>
<date>2009-05-17 19:38:50</date>
</headline>
</entry>
−
<entry>
−
<headline name=" This is another test news">
<text>Test news news news news</text>
<date>2009-05-17 00:00:00</date>
</headline>
</entry>
</entries>
my actionscript is then as follows
Code:
myXML = new XML();
myXML.ignoreWhite = true;
myXML.load("admin/includes/latestnews.php");
myXML.ref = this;
myXML.onLoad = function(success){
if(success){
var root = this.firstChild;
nodes = root.childNodes;
for(var i=0; i<nodes.length; i++) {
this.ref["Title_txt"+i].htmlText = nodes[i].attributes.name;
subnodes = nodes[i].childNodes;
this.ref["Comments_txt"+i].htmlText = subnodes[0].firstChild.toString();
this.ref["Date_txt"+i].htmlText = subnodes[1].firstChild.toString();
}
} else trace("Error loading XML document");
}
this is placed in the same frame number as the dynamic text fields but on a seperate layer. The dynamic text fields are:
Title_txt
Comments_txt
Date_txt
There is no output error but nothing is showing up. The file is using actionscript 2.0 and wondering if the actionscript i am using is write for that format?
any help would be great, bit of a novice with xml loading!