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

  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!"

  2. #2
    ___________________
    Join Date
    May 2004
    Posts
    3,174
    i'd have php echo out xml. if you still wanted an array on the other side, that'd be an easy flip, but probably not even necessary

  3. #3
    Happy FK'er TheWaste's Avatar
    Join Date
    Dec 2003
    Location
    Here
    Posts
    779
    Thanks for the response Moargrius.

    I'm not 'hip' enough for XML just yet. Long story short I've been working too long and hard to expand my technological horizons. Fingers crossed that will change at the end of this project. That's also why I'm using AS2.0...

    Anyway. I don't really know how XML works, but if I'm not mistaken I would need to build node structure syntax to prepare the output. To be honest I'm not even sure if that question is worded correctly.

    With all the XML structure syntax, would that not just be the same problem as making a bunch of NVP's to present the variables? (Lot's of unnecessary characters?)

    Surely there is a simple way to tell Flash not to automatically escape the incoming PHP data... I'm only looking for a quick fix here.

    Thanks!
    "Good lord, you're wasting thousands of dollars worth of Interferon!"
    "... and you're Interferon with our good time!"

  4. #4
    Happy FK'er TheWaste's Avatar
    Join Date
    Dec 2003
    Location
    Here
    Posts
    779
    I've been screwing around with the header/content types on the data and doing whatever I can to stop Flash from URLdecoding/escaping the data I'm sending it.

    While I'd still love to know if there is an answer to this question, for the time being I'm just going to set up the inputs/outputs to be NVP. A painful, timeconsuming and probably unnecessary exercise. I can't however keep putting this off.

    Thanks anyway everyone reading.
    "Good lord, you're wasting thousands of dollars worth of Interferon!"
    "... and you're Interferon with our good time!"

  5. #5
    Flashmatics silentweed's Avatar
    Join Date
    Mar 2005
    Location
    London, UK
    Posts
    4,876
    try serialising the data instead, will make life a much more simple for you.

    check out:
    http://www.sephiroth.it/test/unserializer/
    Flashmatics | Flashmatics Library (free downloads)
    -----------------------------------------------------
    Latest Projects:
    The Dead Room | Are You Smarter | Prison Break (Frame A Friend) | Beck's Fusions | The 24 Experience

    -----------------------------------------------------
    Flash Certified Developer

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