-
XML to flash problem
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!
-
myXML.ref isnt really needed.. (not sure what it is even for).. your also doing this.ref which is essentially this.this....??
this will trace out what you need.. not sure of the paths to your text fields:
AS:
PHP Code:
var myXML:XML = new XML();
myXML.ignoreWhite = true;
myXML.onLoad = function(success) {
if (!success) {
trace("XML NOT FOUND");
}else{
var root = this.firstChild;
var nodes = root.childNodes; //entry nodes
trace("XML: "+nodes.length);
for (var i = 0; i < nodes.length; i++) {
//var subnodes = nodes[i].childNodes[0]; //headline nodes
//or
var subnodes = nodes[i].firstChild; //headline nodes
var hName = subnodes.attributes.name;
trace("NAME: "+hName);
var detailnodes = subnodes.childNodes;
var hComments = detailnodes[0].firstChild;
trace("COMMENTS: "+hComments);
var hDate = detailnodes[1].firstChild;
trace("DATE: "+hDate);
trace(newline)
//add to text files:
sometextFileName_1.html = true;
sometextFileName_1.htmlText = hName;
sometextFileName_2.html = true;
sometextFileName_2.htmlText = hComments;
sometextFileName_3.html = true;
sometextFileName_3.htmlText = hDate;
}
}
};
myXML.load("data.xml");
XML:
CODE:
Code:
<entries>
<entry>
<headline name="NAME 1">
<text>Test News Description 111</text>
<date>2009-05-17 19:40:33</date>
</headline>
</entry>
<entry>
<headline name="NAME 2">
<text>test news 2222</text>
<date>2009-05-17 19:38:50</date>
</headline>
</entry>
<entry>
<headline name="NAME 3">
<text>Test news news news news 333</text>
<date>2009-05-17 00:00:00</date>
</headline>
</entry>
</entries>
-
thanks thats great and works, but only shows up one result rather than showing up all the entries (all 3)
how do i get it to loop all of them in?
-
huh? it does loop through all 3 entries..
this is what it traces out for me:
XML: 3
NAME: NAME 1
COMMENTS: Test News Description 111
DATE: 2009-05-17 19:40:33
NAME: NAME 2
COMMENTS: test news 2222
DATE: 2009-05-17 19:38:50
NAME: NAME 3
COMMENTS: Test news news news news 333
DATE: 2009-05-17 00:00:00
are you talking about your textfields?
-
sorry whispers, yeah meant the text fields, they just produce the last traced result
-
oh yeah.. I just added that as an example..cause im not sure what it is you want done with the text/data returned..
you can do something like this:
sometextFileName_1.html = true;
sometextFileName_1.htmlText += hName;
sometextFileName_2.html = true;
sometextFileName_2.htmlText += hComments;
sometextFileName_3.html = true;
sometextFileName_3.htmlText += hDate;
that will put all names in one field
all comments in one field
all dates in one field..
(of course you need to have your textfields set to multi-line)
what are you trying to do with the text? (how do you want it displayed)
-
basically trying to make it look like
Title Date
Comment Comment Comment Comment Comment
Comment Comment
so the title is Title_txt
Date is Date_txt
Comment is Comment_txt
-
sorry if thats not clear
title headline Date
Comment Text
______________________________
title headline Date
Comment Text
______________________________
title headline Date
Comment Text
______________________________
-
so do you have pre-set textfields for this? or are you trying to add them dynamically to match as many entries as you have?
if you want data in separate textFields..they need to have a unique instance names..
-
Tried the unique text fields and did not seem to work. is there a way of adding them dynamically? they just need to show all three in that specified format
-
well now your complicating things just a bit.. :)
since you wont know how long the comments are..etc the text field will need to be autosized.. and the textfield after it really cant go to a set/pre-determined place... so we'll have to adjust for that.. gimmie a few.. I'll whip something up..
-
-
here try something like this:
PHP Code:
var nextY:Number = 0;
var myXML:XML = new XML();
myXML.ignoreWhite = true;
myXML.onLoad = function(success) {
if (!success) {
trace("XML NOT FOUND");
}else{
var root = this.firstChild;
var nodes = root.childNodes; //entry nodes
trace("XML: "+nodes.length);
for (var i = 0; i < nodes.length; i++) {
//var subnodes = nodes[i].childNodes[0]; //headline nodes
//or
var subnodes = nodes[i].firstChild; //headline nodes
var hName = subnodes.attributes.name;
trace("NAME: "+hName);
var detailnodes = subnodes.childNodes;
var hComments = detailnodes[0].firstChild;
trace("COMMENTS: "+hComments);
var hDate = detailnodes[1].firstChild;
trace("DATE: "+hDate);
trace(newline)
//add to text fields:
var my_txt = _root.createTextField("my_txt"+i, _root.getNextHighestDepth(), 10, previousY, 300, 100);
my_txt.border = true;
my_txt.borderColor = 0xEEEEEE;
my_txt.multiline = true;
my_txt.wordWrap = true;
my_txt.autoSize = "LEFT";
my_txt.htmlText += hName+" "+hDate+newline;
my_txt.htmlText += hComments;
my_txt._height = Math.round(attachedDiscount.item_txt._height);
//place field
var fieldHeight:Number = (my_txt._y + my_txt._height);
if (my_txt.text != "") {
my_txt._height = fieldHeight + 4;
} else {
trace("no need for scale...no text..(default size)");
}
//update var
my_txt._y = nextY;
nextY = Math.round(my_txt._y + my_txt._height);
}
}
};
myXML.load("data.xml");
-
thank you so so much works a treat! your my hero! x
-
1 Attachment(s)
heres another example.. may be more to your liking..
has more of an area for you to 'decorate/theme'..or add stuff.. you can also easily add a link or interactive capabilities to it.. (i just added the rollover/out highlighting stuff..but you can add anything..
I also put a boarder around all text fields.. just so you could see them..but they are fairly ugly..LOL