...

View Full Version : actionscript for loop doesn't work properly



ktsixit
12-10-2008, 12:57 PM
var all_news = new LoadVars();
all_news.load("http://localhost/website/news.php?");
all_news.onLoad = function(success){
if(success){
for(i=1; i<=all_news.total; i++) //all_news.total = 3
{ var this_new = new LoadVars();
this_new.load("http://localhost/website/news.php?id="+i);
trace("http://localhost/website/news.php?id="+i);
this_new.onLoad = function(success){
if(success){
trace(this_new.news_title.toString());
trace(this_new.news_text.toString());
}else trace("error");
}
}
}else trace("error");
}

this code is about getting data from a php file. The output it gives is:
http://localhost/website/news.php?id=1
http://localhost/website/news.php?id=2
http://localhost/website/news.php?id=3
undefined
undefined
undefined
undefined
title 3
text 3

but why is this happening? Shouldn't it be like this?>>
http://localhost/website/news.php?id=1
undefined
undefined
http://localhost/website/news.php?id=2
undefined
undefined
http://localhost/website/news.php?id=3
title 3
text 3

gnomeontherun
12-10-2008, 02:08 PM
It does work, it just functions differently than you might think. All of the data appears to be getting processed and you could put it into an array.

I think the issue is that sometimes Flash isn't always able to grab data from external places and kind of 'queues' it up. So in this case, it does the parts of the loop that don't have an event handler (onLoad) and puts the onLoad afterwards.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum