Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    Regular Coder
    Join Date
    Sep 2008
    Posts
    106
    Thanks
    27
    Thanked 3 Times in 3 Posts

    actionscript for loop doesn't work properly

    Code:
    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

  • #2
    Senior Coder gnomeontherun's Avatar
    Join Date
    Sep 2007
    Location
    Houston
    Posts
    2,846
    Thanks
    10
    Thanked 238 Times in 229 Posts
    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.
    jeremy - gnomeontherun
    Educated questions often get educated answers, and simple questions often get simple answers.


  •  

    Posting Permissions

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