# Loading different XML via PHP

**URL:** <https://forum.kirupa.com/t/loading-different-xml-via-php/262639>\
**Category:** flash\
**Created:** [June 8, 2008, 11:27am UTC](https://forum.kirupa.com/t/loading-different-xml-via-php/262639 "2008-06-08T11:27:13Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Pilot](https://avatars.discourse-cdn.com/v4/letter/p/da6949/32.png) [@Pilot](https://forum.kirupa.com/u/Pilot)\
**Post date:** [June 8, 2008, 11:27am UTC](https://forum.kirupa.com/t/loading-different-xml-via-php/262639/1 "2008-06-08T11:27:13Z")

</div>

Hi

I’m trying to load XML via PHP to Flash.  
It all works good if I specify the: xmlData.load(“db\_query.php?level0=1”);  
But I have 5 buttons and I want them to load different XML (level0=1,2,3,4,5)

The PHP file is generating correct XML but I can’t get Flash to import it correctly.

In frame 1 I have the following code for handling the XML :

```php

function loadThumbs() {
    
            image* = xmlNode.childNodes*.childNodes[0].firstChild.nodeValue;
            description* = xmlNode.childNodes*.childNodes[1].firstChild.nodeValue;
            thumbnails* = xmlNode.childNodes*.childNodes[2].firstChild.nodeValue;
            twidth* = xmlNode.childNodes*.childNodes[3].firstChild.nodeValue;
            articlegroupName* = xmlNode.childNodes*.childNodes[4].firstChild.nodeValue;
            catId* = xmlNode.childNodes*.childNodes[5].firstChild.nodeValue;
        
            thumbnails_fn(i);

}

function loadXML(loaded) {
    
    if (loaded) {
        xmlNode = this.firstChild;
        image = [];
        description = [];
        thumbnails = [];
        twidth = [];
        articlegroupName = [];
        catId = [];
        total = xmlNode.childNodes.length;
        for (i=0; i<total; i++) {
            image* = xmlNode.childNodes*.childNodes[0].firstChild.nodeValue;
            description* = xmlNode.childNodes*.childNodes[1].firstChild.nodeValue;
            thumbnails* = xmlNode.childNodes*.childNodes[2].firstChild.nodeValue;
            twidth* = xmlNode.childNodes*.childNodes[3].firstChild.nodeValue;
            articlegroupName* = xmlNode.childNodes*.childNodes[4].firstChild.nodeValue;
            catId* = xmlNode.childNodes*.childNodes[5].firstChild.nodeValue;
        }
        
        content = "XML loaded!";

    } else {
        content = "XML NOT loaded!";
    }

}

    xmlData = new XML();
    xmlData.ignoreWhite = true;
    xmlData.onLoad = loadXML;
    xmlData.load("db_query.php?level0=1");

```

in movieclip “categories” there’s 5 buttons (cat1,cat2…) that, when clicked, should load the different levels and load the correct thumbnails and information.

the buttons in “categories” i have: (cat2)

```php

on (release) {
    _root.xmlData.load("db_query.php?level0=2");
    _root.loadThumbs();
    _root.categories.gotoAndPlay("unloadCats");
}

```

This code doesn’t load correctly, sometimes it loads 1 thumbnail, sometimes 2… but there is at least 5 in every level…

I’ve tried 100 different sollutions whithout any results, please help me!

Thanks, Michael.

Ps. It seems like it want’s to load the XML but doesn’t have the time = loadXML isn’t finnished when the loadThumbs() function is called, leading to only show 1-2 thumbnails and incorrect information… maby?? A recall of the xmldata.load is maby needed, but I can’t find out how…

---

<div class="post-metadata">

**Author:** ![creatify](https://avatars.discourse-cdn.com/v4/letter/c/d07c76/32.png) [@creatify](https://forum.kirupa.com/u/creatify)\
**Post date:** [June 8, 2008, 10:00pm UTC](https://forum.kirupa.com/t/loading-different-xml-via-php/262639/2 "2008-06-08T22:00:19Z")

</div>

> [@Pilot;2338886](#):
>
> Ps. It seems like it want’s to load the XML but doesn’t have the time = loadXML isn’t finnished when the loadThumbs() function is called, leading to only show 1-2 thumbnails and incorrect information… maby?? A recall of the xmldata.load is maby needed, but I can’t find out how…

Thats exactly whats happening - your trying to load your thumbs immediately after the xml.load is called. Flash is asynchronous - in that when the xml.load(“file.php?sdf=sdf”); is called, the next line of code is executed right away (loadThumbs) - flash doesn’t wait for the xml to load, then execute the next line of code.

You need to use the xml.onLoad method - you create the xml object, you set up the onLoad method, then you load the xml. When the xml is completely loaded, the onLoad method is fired. In that onLoad method is where you will call your loadThumbs() calling - knowing that the xml is completely loaded an in memory for use. Just search around here for xml.onLoad or “loading xml” - you’ll find many examples. Hope this helps.

---

<div class="post-metadata">

**Author:** ![Pilot](https://avatars.discourse-cdn.com/v4/letter/p/da6949/32.png) [@Pilot](https://forum.kirupa.com/u/Pilot)\
**Post date:** [June 9, 2008, 6:16pm UTC](https://forum.kirupa.com/t/loading-different-xml-via-php/262639/3 "2008-06-09T18:16:48Z")

</div>

Thanks, but it doesn’t work =(

I have [COLOR=#000000][COLOR=#0000BB]xmlData[/COLOR][COLOR=#007700].[/COLOR][COLOR=#0000BB]onLoad [/COLOR][COLOR=#007700]= [/COLOR][COLOR=#0000BB]loadXML[/COLOR][COLOR=#007700] [/COLOR][/COLOR]and then at the end of [COLOR=#000000][COLOR=#0000BB]function loadXML(loaded)[/COLOR][COLOR=#007700][/COLOR][COLOR=#0000BB][/COLOR][COLOR=#007700][/COLOR][COLOR=#0000BB][/COLOR][COLOR=#007700][/COLOR][/COLOR] I have set the [COLOR=#000000][COLOR=#0000BB]loadThumbs()[/COLOR][COLOR=#007700][/COLOR][COLOR=#0000BB][/COLOR][COLOR=#007700][/COLOR][COLOR=#0000BB][/COLOR][COLOR=#007700][/COLOR][/COLOR];

This results in, what I can see, that the thumbnail background (white square) moves correctly and places itself at a position like there where 20 thumbnails before it… But there are none…

If i set the \*\* xmlData.load(“db\_query.php?level0=1”);\*\* [COLOR=Navy][/COLOR]directly in frame 1 and **\_root.loadThumbs();** in one of the buttons it loads the thumbnails correctly.

Now I have **\_root.xmlData.load(“db\_query.php?level0=1”);** in one of the buttons and the\*\* loadThumbs();\*\* in the loadXML function… not working…

What is wrong!?

---

<div class="post-metadata">

**Author:** ![Pilot](https://avatars.discourse-cdn.com/v4/letter/p/da6949/32.png) [@Pilot](https://forum.kirupa.com/u/Pilot)\
**Post date:** [June 10, 2008, 5:25pm UTC](https://forum.kirupa.com/t/loading-different-xml-via-php/262639/4 "2008-06-10T17:25:32Z")

</div>

BUMP - Please someone!?

---

<div class="post-metadata">

**Author:** ![rondog](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/rondog/32/2478_2.png) [@rondog](https://forum.kirupa.com/u/rondog)\
**Post date:** [June 10, 2008, 9:00pm UTC](https://forum.kirupa.com/t/loading-different-xml-via-php/262639/5 "2008-06-10T21:00:20Z")

</div>

try it like this:

```auto

var xmlData:XML = new XML();
xmlData.ignoreWhite = true;
xmlData.onLoad = function(success:Boolean)
{
    if (success)
    {
        xmlNode = this.firstChild;
        image = [];
        description = [];
        thumbnails = [];
        twidth = [];
        articlegroupName = [];
        catId = [];
        total = xmlNode.childNodes.length;
        for (i = 0; i < total; i++)
        {
            image* = xmlNode.childNodes*.childNodes[0].firstChild.nodeValue;
            description* = xmlNode.childNodes*.childNodes[1].firstChild.nodeValue;
            thumbnails* = xmlNode.childNodes*.childNodes[2].firstChild.nodeValue;
            twidth* = xmlNode.childNodes*.childNodes[3].firstChild.nodeValue;
            articlegroupName* = xmlNode.childNodes*.childNodes[4].firstChild.nodeValue;
            catId* = xmlNode.childNodes*.childNodes[5].firstChild.nodeValue;
        }
        trace("XML loaded!");
        loadThumbs();
    } else
    {
        trace("XML NOT loaded!");
    }
};
xmlData.load("db_query.php?level0=1");

function loadThumbs()
{
    image* = xmlNode.childNodes*.childNodes[0].firstChild.nodeValue;
    description* = xmlNode.childNodes*.childNodes[1].firstChild.nodeValue;
    thumbnails* = xmlNode.childNodes*.childNodes[2].firstChild.nodeValue;
    twidth* = xmlNode.childNodes*.childNodes[3].firstChild.nodeValue;
    articlegroupName* = xmlNode.childNodes*.childNodes[4].firstChild.nodeValue;
    catId* = xmlNode.childNodes*.childNodes[5].firstChild.nodeValue;
    thumbnails_fn(i);
}

```

take out the loadthumbs on your onRelease…thats what getting called before its done loading. I moved the loadThumbs function call to the end of the XML after its loaded.

---

<div class="post-metadata">

**Author:** ![rondog](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/rondog/32/2478_2.png) [@rondog](https://forum.kirupa.com/u/rondog)\
**Post date:** [June 10, 2008, 9:05pm UTC](https://forum.kirupa.com/t/loading-different-xml-via-php/262639/6 "2008-06-10T21:05:23Z")

</div>

You might also move your:

```auto

 _root.categories.gotoAndPlay("unloadCats");

```

to a separate function, similar to loadthumbs then at the end of that unloadCats function call load thumbs

---

<div class="post-metadata">

**Author:** ![Pilot](https://avatars.discourse-cdn.com/v4/letter/p/da6949/32.png) [@Pilot](https://forum.kirupa.com/u/Pilot)\
**Post date:** [June 12, 2008, 5:28pm UTC](https://forum.kirupa.com/t/loading-different-xml-via-php/262639/7 "2008-06-12T17:28:02Z")

</div>

Thank you, but the result is still the same.  
I dont have the loadThumbs() on my onRelease, it’s in the end of loadXML function.  
It’s loading the correct amount of thumbnails, I can see it, but they doesn’t show up.

For ex. if I press category 1 it loads 14 thumbnails, result = the white background of the thumbnail loader moves to “square” 15. If I press category 2 it loads 6 thumbnails, result = another white background moves to “square” 7. Like if flash got the XML, but then theres something wrong with the attachMovie-function…

Here’s the thumbnails\_fn(k) function, maby something wrong with \_root-syntax or similar?

```auto

function thumbnails_fn(k) {    

    _root.attachMovie("thumbnailMC","tma"+k,k);
    _root["tma"+k]._y = 75;

    tlistener = new Object();
    
    image_mcl = new MovieClipLoader();
    image_mcl.addListener(tlistener);    
    

    tlistener.onLoadInit = function(target_mc) {

        target_mc.pictureValue = k;
        target_mc.onRelease = function() {
            p = this.pictureValue-1;
        };
        target_mc.onRollOver = function() {
            this._alpha = 50;
            infobox.catName.text = articlegroupName[k];
            infobox._visible=true;
            this.filters = thumbsFilter;
        };
        target_mc.onRollOut = function() {
            this._alpha = 100;
            this.filters = no;
            infobox._visible=false;
        };
        target_mc.onRelease = function() {
            for (i=0;i<total;i++) {
                _root.image_mcl.unloadClip(_root["tma"+i])
            }
            testfield.text = "db_query.php?catid=" + catId[k];
            newQuery = "db_query.php?catid=" + catId[k];
            infobox._visible=false;
            k=0;
            loadThumbs();
        };
    };
    tlistener.onLoadComplete = function(target_mc:MovieClip) {
        i++;
        _root["tma"+k].bredd._visible = false;
        if (i<total) {
        loadThumbs();
        
        }
    }
    tlistener.onLoadProgress = function(target_mc:MovieClip, loaded:Number, total:Number) {
        percent = Math.floor(loaded/total*100);
            
        _root["tma"+k].bredd._visible = true;
        _root["tma"+k].bredd.text = percent+"%";
        _root["tma"+k]._alpha=0;
        _root["tma"+k].onEnterFrame = function() {
            if (_root["tma"+k]._alpha < 100) {
                _root["tma"+k]._alpha += 5;
            } else {
                delete _root["tma"+k].onEnterFrame;
            }
        }
    
    };    

    _root["tma"+k]._y = 75; 
    _root["tma"+k]._x = (220 + (k*76));
    _root["tma"+k].thumbnail._x = (70-twidth[k])/2;
    if (k>8 && k<18){ 
    _root["tma"+k]._y = 150; 
    _root["tma"+k]._x = (220 + ((k-9)*76));
    }
    if (k>17){ 
    _root["tma"+k]._y = 225; 
    _root["tma"+k]._x = (220 + ((k-18)*76));
    }
    
    image_mcl.loadClip(thumbnails[k],_root["tma"+k].thumbnail);

}

```
