13 Replies Latest reply on Dec 13, 2009 9:05 AM by jdesko

    Send an array to php with data/services

    jdesko Level 2

      With FB and data/services can we send an array of objects to php for insertion into mysql?  Any example php code?  ( using mysql - *not mysqli). 

       

      So I have an example object below, but I want to send a list of these to php for insert into mysql.

       

      var obj:Object = new Object();

      obj.first_name = "John";

      obj.middle_name = "Middle";

      obj.last_name = "Name";

       

       

      Thanks,

      John

        • 1. Re: Send an array to php with data/services
          jdesko Level 2

          A thought ....  in FB create an XML object,  send this thru data services (as oppose to httpservice) to PHP, then use that object with SimpleXML, loop thru the XML and insert each child node. 

           

          John

          • 2. Re: Send an array to php with data/services
            SunilAdobe Adobe Employee

            You can send arrays, just create the object array and access it as an array in the other side.

             

            var objArray:Array;

            obj1 = new Object();

            objArray.push(obj1);

            obj2 = new Object();

            objArray.push(obj2);

             

            You can also configure your input types of your operation to tell that the operation actually takes an object array. (In fact it could be any AS class array also, which could be used on the other side as php object array)

             

            Let us know if you run into issues.

             

            Thanks

            -Sunil

            • 3. Re: Send an array to php with data/services
              David_F57 Level 5

              Hi,

               

              you could just send an arraycollection to your amfphp/zend, something like this

               

              php

              ====

              function insertRecords($data) {

              $mysql = mysql_connect(localhost,"root","");

              mysql_select_db( "clients" );

              foreach($data as $key){

              $Query = "INSERT INTO names (FIRSTNAME,SURNAME) VALUES(.$key['name'].,.$key['surname'].)";

              $Result = mysql_query( $Query );

              }

              }

               

              if I get time i'll test this as I always get the .$key[field]. syntax wrong

               

              David

              • 4. Re: Send an array to php with data/services
                David_F57 Level 5

                hi Sunil,

                 

                you probably have a better grasp than me, the php example above may have the $key var a bit askew, what do you think..

                 

                David

                • 5. Re: Send an array to php with data/services
                  jdesko Level 2

                  Hello David and Sunil;

                   

                  Thanks for the replies.  Sunil, I probably worded the question wrong, but David got it ... I see that data/services allows you to have arrays for input and returns, but I can't find how php accepts a value object array (if that's what it would be). 

                   

                  David, thanks for a starting point ... now to find the end point.

                   

                  Thanks again,

                  John

                  • 6. Re: Send an array to php with data/services
                    David_F57 Level 5

                    Hi,

                     

                    I've attached an example project, it uses amfphp, (I broke my host trying  to implementing zend so I couldn't upload a functional project for you to play with )

                     

                    basically the php function is

                     

                    function insertRecords($data) {

                    $mysql = mysql_connect(localhost,"root","");

                    mysql_select_db( "clients" );

                    foreach($data as $key){

                    $firstname=$key['FIRSTNAME'];

                    $surname=$key['SURNAME'];

                    $Query = "INSERT INTO names (FIRSTNAME,SURNAME) VALUES('$firstname','$surname')";

                    $Result = mysql_query( $Query );

                    }

                    $Query = "SELECT * FROM names";

                    $Result = mysql_query( $Query );

                    while ($row = mysql_fetch_object($Result)) {

                    $ArrayOfData[] = $row;

                    }

                    return($ArrayOfData);

                    }

                     

                    the mxml code

                     

                    protected function button1_clickHandler(event:MouseEvent):void

                    {

                    var myData:Array=new Array();

                    var D1:Object = {

                    FIRSTNAME: "David",

                    SURNAME  : "Fabris"}

                    myData.push(D1);

                    var D2:Object = {

                    FIRSTNAME: "Ronald",

                    SURNAME  : "Peters"}

                    myData.push(D2);

                    var D3:Object = {

                    FIRSTNAME: "Dick",

                    SURNAME  : "Jones"}

                    myData.push(D3);

                    clients.getOperation("insertRecords").send(myData);

                    }

                     

                     

                    David.

                    • 7. Re: Send an array to php with data/services
                      David_F57 Level 5

                      hi,

                       

                      Also if you rather use arraycollection

                       

                      var dataCollection:ArrayCollection=new ArrayCollection();

                      dataCollection.addItem({FIRSTNAME:"One",SURNAME:"Smith"});

                      dataCollection.addItem({FIRSTNAME:"Two",SURNAME:"Smith"});

                      dataCollection.addItem({FIRSTNAME:"Three",SURNAME:"Smith"});

                      clients.getOperation("insertRecords").send(dataCollection);

                       

                       

                      David.

                      • 8. Re: Send an array to php with data/services
                        jdesko Level 2

                        Thanks again David for your help!

                         

                        Below I put my basic test php code (this was just a test, there would normally be a return statement along with other stuff - that's a tech term isn't it?) and mxml function, I'm using zend/php/mysql.  * I noticed from other discussions that you're not a big fan of zend, sorry to hear that, I'm actually having a great time with it.

                         

                        Thanks,

                        John

                         

                        php function:

                         

                         

                        public function createProduction($productions)

                        {

                         

                        foreach($productions as $production)

                        {

                        $sql = "INSERT INTO productions_temp (first, second, third)

                        VALUES ('$production->first', '$production->second', '$production->third')";

                        $result = mysql_query($sql) or die('Query failed: ' . mysql_error());

                        }

                        }

                         

                         

                        mxml function:

                         

                         

                        private function init():void

                        {

                        var array:Array = new Array;

                        var obj1:Object = new Object();

                        obj1.first = "John";

                        obj1.second = "A";

                        obj1.third = "Doe";

                        array.push(obj1);

                        var obj2:Object = new Object();

                        obj2.first = "Jane";

                        obj2.second = "J";

                        obj2.third = "Doe";

                        array.push(obj2);

                        var obj3:Object = new Object();

                        obj3.first = "Thank";

                        obj3.second = "U";

                        obj3.third = "David";

                        array.push(obj3);

                         

                        createProduction_result.token = productions.createProduction(array);

                        createProduction_result.addEventListener(FaultEvent.FAULT, faultHandler);

                        createProduction_result.addEventListener(ResultEvent.RESULT, createProduction_resultHandler);

                        }

                         

                         

                        ------

                        Assistant: The instruction book said that was impossible!
                        Joe Patroni: That's one nice thing about the 707. She can do everything but read.

                        • 9. Re: Send an array to php with data/services
                          David_F57 Level 5

                          Hi,

                           

                          Glad to have got you pointed in some sort of direction, hopefully it was the right one .

                           

                          I have to admit the object handling on yours is a much cleaner $value->field.

                           

                          David.

                          • 10. Re: Send an array to php with data/services
                            jdesko Level 2

                            David;

                             

                            You got me in the right direction, you also got the star on your 6:28pm post!

                             

                            John

                            • 11. Re: Send an array to php with data/services
                              Peter deHaan Level 4

                              I think this forum needs a (grouphug) emoticon.

                               

                              Go team!

                              • 12. Re: Send an array to php with data/services
                                David_F57 Level 5

                                @jdesko - ah community spirit, ain't it grand

                                 

                                @Peter - while we are talking enhancements that don't fit into a jira category, how about collaborative services as an extension to the forum, ok maybe it's little more than an emoticon, but how quick could we resolve issues as a group then...

                                 

                                 

                                @all the guys in this discussion group -  This has to have the best closure rate of requests of any dev forum around.

                                 

                                     go team   (:(::):)   <<============== group hug emoticon.

                                 

                                David

                                • 13. Re: Send an array to php with data/services
                                  jdesko Level 2

                                  Hello Peter and David;

                                   

                                  Belated group hug.

                                   

                                  John