views:

60

answers:

3

Here's what I have in PHP:

for ($i = 0; $i < 355; $i++)
{
    echo "vote";
    echo "$i";
    echo "=$votesArray[$i]";
    if($i != 354)
    {
        echo "&";
    }
}

Which should send data to Flash that looks something like "vote0=2&vote1=5&..." and so on.

Here is the Actionscript 3 side:

var i:int;
for (i = 0; i < 355; i++)
{
    var tempString:String = "vote" + i; 
    voteResults[i] = event.target.data.tempString;
}

I haven't attempted to run this yet, but I get the feeling it won't work. Can you see what I'm trying to get at, though? For each iteration of the for loop, I would like it to take the data from a different part of event.target.data. For the first iteration, it should be event.target.data.vote0. Second, event.target.data.vote1, and so on.

+2  A: 

Check out AMFPHP. Makes passing data from PHP to Flash almost effortless.

AMFPHP is a free open-source PHP implementation of the Action Message Format(AMF). AMF allows for binary serialization of Action Script (AS2, AS3) native types and objects to be sent to server side services. AMFPHP is challenged with implementing the entire AMF protocol to be an alternative to Flex Data Services (AMF3) and Flash Remoting (AMF0).

Mike B
I'm sure AMFPHP would be effective, but that seems to me like using an ax to do a job for which a scalpel would suffice.
VGambit
Very true, could be overkill for your purposes :)
Mike B
Well i have used AMFPHP in many of my facebook apps but little stupid to ask but still i can not understand that (AMFPHP is challenged with implementing the entire AMF protocol to be an alternative to Flex Data Services (AMF3) and Flash Remoting (AMF0))?? Does it mean that AMFPHP is as good as FLex Data Services (AMF3) specially??
Muhammad Irfan
+1  A: 

I would use xml for something like this, but it can be done with url encoded strings.

Assuming you loaded your data with URLLoader and specified the dataFormat as URLLoaderDataFormat.VARIABLES, you are close.

If you have a raw string, you should parse it first to break it down to name/value pairs. This is what URLVariables does.

Anyway, once you have an object containing names/values, you can do:

var i:int;
for (i = 0; i < 355; i++)
{
    var tempString:String = "vote" + i; 
    voteResults[i] = event.target.data[tempString];
}

If you use dot access, it will take "tempString" literally. If you use bracket access, the value of the variable tempString will be evaluated.

PS: By the way, I don't think your php will do what you want. A cleaner way, IMO would be:

$pairs = array();
for ($i = 0; $i < 355; $i++)
{
    $pairs[] = 'vote' . $i . '=' . $votesArray[$i]; 
    // you might want to use rawurlencode($votesArray[$i]) to be safe if these are not numeric values.
}
echo implode('&',$pairs);

PS 2: Also, this is rather brittle since you're hardcoding 355. If you ever change your php, you'll have to change your AS code as well. You could try something like this:

var data:URLVariables = event.target.data as URLVariables;
for(var fieldName:String in data) {
    var index:int = parseInt(fieldName.substr(4));
    //  or optionally, add an underscore to the name: vote_1
    //  so you can change the above to something like
    //  fieldName.split("_")[1]
    voteResults[index] = data[fieldName];
}

Or, as I said before, you could use xml, which is simple enough and a better option for this, I think.

Juan Pablo Califano
Here's the error I got:Error: Error #2101: The String passed to URLVariables.decode() must be a URL-encoded query string containing name/value pairs. at Error$/throwError() at flash.net::URLVariables/decode() at flash.net::URLVariables() at flash.net::URLLoader/onComplete()I loaded the data and specified the dataFormat exactly how you said. I think it might have something to do with the PHP side of the equation.
VGambit
@VGambit. Yes. Check the string you're returning from php. Maybe there's other output in your php script (other echos, warnings, etc). This will most likely cause the string not to be correctly parsed (and possibly will give the Error you pasted if the string is not a valid querystring. rawurlencode() could help if what you've got in $votesArray are in turn strings.
Juan Pablo Califano
I tried your PHP code, and it worked perfectly. Thanks a lot.All I had in the votesArray were characters. Specifically, numbers.
VGambit
@VGambit. No problem!
Juan Pablo Califano
+1  A: 

I often use JSON to interact pass data from server to client, even with Flash. Almost all my projects include the as3corelib and amongs other functionalities has a JSON encoder/decoder. I think that it's easier to work and debug code using json than AMF (of course if i have lot of messages AMF it's the way) and i'd love the ability of PHP to convert object to JSON with native code

 echo(json_encode($votesArray));

and on the client side:

var votesArray:Array = JSON.decode(jsonString) as Array;

In this way you are not forced to write conversion code in the PHP part and if you have some problems it's easier to debug than the binary AMF, jsonString are normal human readable string that doesn't explode event if the PHP interpreter append some error warning

wezzy
+1 JSON is easy, lightweight and works almost out of the box. That or XML would be my choice for this.
Juan Pablo Califano