PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197
Sending an array to Flash from PHP
A Flash Developer Resource Site

Results 1 to 5 of 5

Thread: Sending an array to Flash from PHP

Threaded View

  1. #1
    Happy FK'er TheWaste's Avatar
    Join Date
    Dec 2003
    Location
    Here
    Posts
    779

    Sending an array to Flash from PHP

    So I'm trying to send an array of values from PHP to Flash, but the problem is any character I could use to separate the values could potentially be in the value itself.

    Fine, no problem I thought. I'll just urlencode the variables and allow (for arguments sake) commas to separate the 'encoded' values.

    Just after I'd finished laughing about my mighty victory over code adversary as I often do, I attempted to fetch the array from the PHP file and I quickly learned that Flash automatically applies the 'escape' function to the data after it is received. My laughter was replaced by (for theatrical sake) tears!

    Well that's great. It seems I need one of the following:
    A way to tell Flash to stop automatically 'escaping' the incoming variables, or
    A way to send an array of values without using a character that could be used in the variable
    (which could be anything... fullstops, commas, dashes. Pretty much anything within reason)

    Now, before you mention that I should just make a NVP (Name value pair) for each variable in the array, know that there is quite a lot of data already being loaded, and adding (what I consider is) unnecessary extra NVP syntax (which would double the large amount of data I'm sending) could potentially cause trouble, not to mention a general lack of bandwidth.

    Should I bite the bullet and NVP my arrays (ugh!), or maybe use an obscure character as the separator... but I would like a practical approach to this solution... like stopping Flash from automatically escaping the input.

    Also, if you're having trouble visualising the scenario, here's some example code I whipped up to illustrate the scenario:

    PHP Code:
    <?
    $one 
    "apple";
    $two "pear";
    $three "banana,strawberry";
    //Remember, $three should be one array item.

    $one urlencode($one);
    $two urlencode($two);
    $three urlencode($three);

    echo 
    "&resultingData=" $one "," $two "," $three;
    ?>
    PHP returns:

    &resultingData=apple,pear,banana%2Cstrawberry
    And the AS:

    Actionscript Code:
    sender = new LoadVars();
    loader = new LoadVars();
    sender.sendAndLoad("TESTFILE.php", loader, "GET")
    loader.onLoad = function(){
        arrayData = loader.resultingData
        arrayData = arrayData.split(',')
        numOfResults = arrayData.length
        trace ("Fetched array. There were " + numOfResults + " fetched. There should be three.")
        for(i=0;i<arrayData.length;i++){
            trace("Var: " + i + "=" + arrayData[i])
           
        }
    }

    Flash returns:

    Fetched array. There were 4 fetched. There should be three.
    Var: 0=apple
    Var: 1=pear
    Var: 2=banana
    Var: 3=strawberry
    Edit: Damnit. How did this end up in AS3.0? I could have sworn I chose AS2.0. If a mod could kindly move it to the right spot I would be infinitely grateful. Sorry about the confusion.
    Last edited by TheWaste; 03-02-2010 at 09:24 PM. Reason: Wrong section warning
    "Good lord, you're wasting thousands of dollars worth of Interferon!"
    "... and you're Interferon with our good time!"

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