dcsimg
A Flash Developer Resource Site

Results 1 to 15 of 15

Thread: XML to flash problem

  1. #1
    Junior Member
    Join Date
    Oct 2007
    Posts
    15

    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(">", "&gt;", $row['content']);
            $row['content'] = str_replace("\"", "&quot;", $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!
    Last edited by nade93; 05-18-2009 at 03:44 AM.

  2. #2
    Moderator whispers's Avatar
    Join Date
    Mar 2001
    Location
    CFA2h (respect the HEX)
    Posts
    12,755
    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 
    0nodes.lengthi++) {
                
                
    //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>

  3. #3
    Junior Member
    Join Date
    Oct 2007
    Posts
    15
    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?

  4. #4
    Moderator whispers's Avatar
    Join Date
    Mar 2001
    Location
    CFA2h (respect the HEX)
    Posts
    12,755
    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?

  5. #5
    Junior Member
    Join Date
    Oct 2007
    Posts
    15
    sorry whispers, yeah meant the text fields, they just produce the last traced result

  6. #6
    Moderator whispers's Avatar
    Join Date
    Mar 2001
    Location
    CFA2h (respect the HEX)
    Posts
    12,755
    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)

  7. #7
    Junior Member
    Join Date
    Oct 2007
    Posts
    15
    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

  8. #8
    Junior Member
    Join Date
    Oct 2007
    Posts
    15
    sorry if thats not clear

    title headline Date
    Comment Text
    ______________________________

    title headline Date
    Comment Text
    ______________________________

    title headline Date
    Comment Text
    ______________________________

  9. #9
    Moderator whispers's Avatar
    Join Date
    Mar 2001
    Location
    CFA2h (respect the HEX)
    Posts
    12,755
    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..

  10. #10
    Junior Member
    Join Date
    Oct 2007
    Posts
    15
    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

  11. #11
    Moderator whispers's Avatar
    Join Date
    Mar 2001
    Location
    CFA2h (respect the HEX)
    Posts
    12,755
    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..

  12. #12
    Junior Member
    Join Date
    Oct 2007
    Posts
    15
    your a star!! thanks!

  13. #13
    Moderator whispers's Avatar
    Join Date
    Mar 2001
    Location
    CFA2h (respect the HEX)
    Posts
    12,755
    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 
    0nodes.lengthi++) {
                
                
    //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(), 10previousY300100);
                
    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"); 

  14. #14
    Junior Member
    Join Date
    Oct 2007
    Posts
    15
    thank you so so much works a treat! your my hero! x

  15. #15
    Moderator whispers's Avatar
    Join Date
    Mar 2001
    Location
    CFA2h (respect the HEX)
    Posts
    12,755
    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
    Attached Files Attached Files

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center