Welcome, Guest. Please login or register.
Did you miss your activation email?
05/23/12, 00:40
Home Help Search Login Register
News: Parsley Flex framework review featuring quiz application, in our Flex frameworks series
Flex SDK 4.5 mobile roadmap: begin with your mobile development
Swiz Flex framework review featuring quiz application
New homepage we release our new Homepage, take a look ...

+  Flash-db
|-+  Server side Scripting and Database Support
| |-+  Flash Remoting with AMF (Moderators: Flash-db, Musicman, Jorge Solis, papachan, nothingGrinder)
| | |-+  associative arrays (hashes) won't work when passed to amfphp?
0 Members and 1 Guest are viewing this topic. « previous next »
Pages: [1] Print
Author Topic: associative arrays (hashes) won't work when passed to amfphp?  (Read 7348 times)
cmyk
Jr. Programmer
**
Posts: 55



View Profile WWW Email
« on: 03/05/03, 10:57 »

hi,

i am able to send normal numbered arrays to amfphp but it won't take associative arrays (also called named arrays).

this is what i get in the NetConnection Debugger:

Code:
..........ID: "23"
..........firstName: "Stefan"
..........lastName: "Keller"
..........phoneNumber: "071 939 34 89"
..........street: "bahnhostrasse"
..........streetNumber: "12a"
..........town: "Würmlibach"
so the data is being sent properly as an assoc. array, right?

i am using something like this to access the array in php:

Code:
$arg["ID"]
it just won't work.  Undecided

so long,
cmyk
« Last Edit: 03/05/03, 15:05 by cmyk » Logged
Jorge Solis
Administrator
Systems Administrator
*****
Posts: 14600


View Profile
« Reply #1 on: 03/06/03, 12:35 »

The first example Jeff gives about the use of the original Musicman class use named arrays. Take a look at the first post in the original thread: http://www.flash-db.com/Board/index.php?board=4;action=display;threadid=3163

Jorge
Logged

cmyk
Jr. Programmer
**
Posts: 55



View Profile WWW Email
« Reply #2 on: 03/06/03, 16:56 »

the example you mention is the other way around, i.e. sending assoc arrays to flash.
i just don't get it. why isn't this working?

here's my example:

myTesting.php:
Code:
<?

class myTesting{
     
   function myTesting(){
   
   // method table
       $this->methodTable = array(
      "receiveAssocArr" => array(
      "description" => "receives a assoc arr from flash",
      "access" => "remote",
      "roles" => "role, receiveAssocArr",
      "returntype" => "string",
      "arguments" => array ("arg1", "arg2")
      )
      );
     
   }

// myTesting methods
   
   function receiveAssocArr($arg,$arg1){
      $type1 = gettype($arg);
      $type2 = gettype($arg1);
      $test = array();
      $test["entryOne"] = "entryOneData";
     
      return "received: " .
      "arg['firstName']=" . $arg["firstName"] . "; " .
      "test= " . $test["entryOne"] .
      "; type1 received= " . $type1 .
      "; type2 received= " . $type2 . " value= " . $arg1;
   }
   
}
?>

the as:
Code:
#include "NetServices.as"
#include "NetDebug.as"

conn = NetServices.setDefaultGatewayURL("http://localhost/~cmyk/flashservices/discovery.php");
conn = NetServices.createGatewayConnection();

myservice = conn.getService("myTesting", this);

function sendAssocArr () {
   var mysend = new Array();
   mysend["ID"] = "1";
   mysend["firstName"] = "Dude";
   mysend["lastName"] = "Lebowski";
   mysend["street"] = "bummer";
   mysend["streetNumber"] = "19";
   mysend["town"] = "los angeles";
   mysend["phoneNumber"] = "001 293 750 87 74";
   var mystring = "this is a stinknormal string";
   myservice.receiveAssocArr(mysend,mystring);
}

sendAssocArr();
NetDebug.trace(myservice.receiveAssocArr);

this is what i see in the NetConnection Debugger:
Code:
Result: "received: arg['firstName']=; test= entryOneData; type1 received= NULL; type2 received= double value= 1.8950367089917E-316"

1. $arg's type is NULL?
2. $arg1 is a double?
3. if i send $arg1 allone, without $arg, it's a string
4. also if i send the assocative array ($arg) allone it will be of type NULL.

what am i missing here?

cmyk


« Last Edit: 03/06/03, 17:02 by cmyk » Logged
mrluke
Server what's that
*
Posts: 22



View Profile WWW Email
« Reply #3 on: 03/06/03, 17:23 »

Maybe you can look at what I am doing, it may be of use to you.
The ActionScript:
[script]

#include "NetDebug.as"
#include "NetServices.as"
#include "DataGlue.as"

if (isGateWayOpen ==null){
   //do only once
   isGateWayOpen = true;
   //make the gateway connection
   NetServices.setDefaultGatewayURL("http://localhost/flashservices/service.php");
   gatewayConnection=NetServices.createGatewayConnection();
   scdbservice = gatewayConnection.getService("scdbservice");


function addClient() {
   var newClient = new Object();
   newClient.companyName = CompanyName;
   newClient.businessType = BusinessType;
   newClient.contactName = ContactName;
   newClient.Position = Position;
   newClient.phoneNumberAC = PhoneNumberAC;
   newClient.phoneNumber = PhoneNumber;
   newClient.faxNumberAC = FaxNumberAC;
   newClient.faxNumber = FaxNumber;
   newClient.mobile = Mobile;
   newClient.email = Email;
   newClient.postalAddress = PostalAddress;
   newClient.city = City;
   newClient.state = State;
   newClient.country = Country;
   newClient.postcode = Postcode;
   newClient.streetAddress = StreetAddress;
   newClient.sCity = SCity;
   newClient.sState = SState;
   newClient.sCountry = SCountry;
   newClient.sPostCode = SPostCode;
   newClient.Url = Url;
   newClient.cdrom = CDROM;
   newClient.interest = Interest;
   trace("sent request. newClient: " + newClient.toString());
   scdbservice.addClient(addClientReply, newClient);
   }
}
[/script]

Then the PHP script - thanks to Louis Simpson

[script]

function addClient($newClient){
                       $sql = "INSERT INTO client SET ";
                       $a = array();
                       foreach($newClient as $key => $val) {
                                $a[] = "$key = '".addslashes($val)."'";
                       }
                       $sql .= implode(",", $a);
                       $q = mysql_query($sql);
                       if(mysql_affected_rows()){
                               return "Client added successfully";
                       }else{
                               return "Error adding record";
                       }
               }

[/script]

All my object properties are named exactly as the corresponding table field title.

Hope this helps.
Luke
Logged
cmyk
Jr. Programmer
**
Posts: 55



View Profile WWW Email
« Reply #4 on: 03/06/03, 18:46 »

ok mrlurke,

i found the bug.

[script]
function sendAssocArr () {
  var mysend = new Object(); //<<< i changed this to be an Object instead of an array.
  mysend["ID"] = "1";
  mysend["firstName"] = "Dude";
  mysend["lastName"] = "Lebowski";
  mysend["street"] = "bummer";
  mysend["streetNumber"] = "19";
  mysend["town"] = "los angeles";
  mysend["phoneNumber"] = "001 293 750 87 74";
  var mystring = "this is a stinknormal string";
  myservice.receiveAssocArr(mysend,mystring);
}
[/script]

now everything's just fine!
my question still is: "Why the heck wouldn't it work using an Array()?"

cmyk

« Last Edit: 03/06/03, 18:54 by cmyk » Logged
mrluke
Server what's that
*
Posts: 22



View Profile WWW Email
« Reply #5 on: 03/06/03, 19:20 »

I don't know why not an array. ???
Logged
Louis Simpson
Server what's that
*
Posts: 25



View Profile Email
« Reply #6 on: 03/06/03, 19:31 »

Quote

function sendAssocArr () {
 var mysend = new Array();
 mysend["ID"] = "1";
 mysend["firstName"] = "Dude";
 mysend["lastName"] = "Lebowski";
 mysend["street"] = "bummer";
 mysend["streetNumber"] = "19";
 mysend["town"] = "los angeles";
 mysend["phoneNumber"] = "001 293 750 87 74";
 var mystring = "this is a stinknormal string";
 myservice.receiveAssocArr(mysend,mystring);
}


The answer is that Flash does not have support for associative arrays. Even though it may look like since you can access object properties like

[script]
foo = {};
foo.val = "fooval";
trace (foo["val"]); //traces "fooval"
[/script]

But you are still merely accessing a property of the object foo. So that is why it was not working for you.
Logged
ev
Global Moderator
Systems Administrator
*****
Posts: 502



View Profile WWW
« Reply #7 on: 03/06/03, 19:41 »

foo = new Object();
foo["a"] = 12;
foo["b"] = "some text";

is the same as

foo={a:12, b:"sometext"};

is the same as

foo = new Object();
foo.a = 12;
foo.b = "some text";

notice: these are all objects, not arrays.  object properties can be accessed in an associatetive array formate, but realize the following
foo["a"]
evaluates to foo.a
much like
i=0
myarray
evaluate to myarray.0

the [] just evaluates the statement within and appends it to the parent object.
Logged
cmyk
Jr. Programmer
**
Posts: 55



View Profile WWW Email
« Reply #8 on: 03/06/03, 19:52 »

Quote
The answer is that Flash does not have support for associative arrays.
now wait a minute.

that's a bummer:
[script]
foo = []; // now it's an array
foo.val = "fooval";
trace (foo["val"] + " = " +typeof(foo)); //traces "fooval = object"
[/script]

means arrays are just generic objects in flash.
absolutely right. so why hasn't anybody told me eralier?  Tongue

but now it's crystal clear.

cmyk

ps. i was writing this while you posted, ev.
« Last Edit: 03/06/03, 19:53 by cmyk » Logged
cmyk
Jr. Programmer
**
Posts: 55



View Profile WWW Email
« Reply #9 on: 03/06/03, 19:58 »

but why do i get the values into the service, if i use just numbered arrays, and if asscociative arrays are used, i would not?
is that locigal?

i mean. flash sends the same stuff to amfphp, whether it was created using the Array constructor or the Object constructor...

cmyk

ps. the []  operator is the Array-Element *and* Object-Property Operator.
« Last Edit: 03/06/03, 20:05 by cmyk » Logged
ev
Global Moderator
Systems Administrator
*****
Posts: 502



View Profile WWW
« Reply #10 on: 03/06/03, 21:16 »

in flash, arrays are pretty much toned down objects whose elements are numbered only.  but they do have the advantage of having prebuilt methods to sort, and get lengths, splice, push, etc.
if u think through your development, u will be able to discern when u need to use generic objects and when to use the more specialized array object.
Logged
Pages: [1] Print 
« previous next »
Jump to:  


Powered by MySQL Powered by PHP Powered by SMF 1.1.16 | SMF © 2011, Simple Machines Valid XHTML 1.0! Valid CSS!
anything