7 Replies Latest reply on Apr 25, 2011 2:01 PM by EWN-CMI

    populating DataGrid usin amfphp


      Hi, im trying to populate data grid using amfphp but its not working code for flex and php below


      php code


      public function users(){

             $u = array();
             $R = sql_query("SELECT * FROM users");
             while($row = mysql_fetch_assoc($R)){
                 $u[] = $row;

             return $u;


      flex code




      private function onStartup():void{







      private function userdatat(event:ResultEvent):void{

      dg.dataProvider = event.result;








      id="listBooks" destination="zend" source="LSBU_backend_flex" showBusyCursor="true">



      <mx:method name="addEdit" result="userdatat(event)"/>





      <mx:DataGrid x="10" y="237" width="100%" height="104" id="dg">






      <mx:DataGridColumn headerText="Title" dataField="b_title"/>



      <mx:DataGridColumn headerText="Author" dataField="b_author"/>








        • 1. Re: populating DataGrid usin amfphp
          David_F57 Level 5



          This is done in flex 4 but its pretty much the same as flex 3 just need to replace the spark stuff with the mx stuff. Hopefully it will help




          <?xml version="1.0" encoding="utf-8"?>

          <s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"


             xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600" creationComplete="application1_creationCompleteHandler(event)">



          import mx.collections.ArrayCollection;

          import mx.controls.Alert;

          import mx.events.FlexEvent;

          import mx.rpc.events.FaultEvent;

          import mx.rpc.events.ResultEvent;



          [Bindable] private var clientsArray: ArrayCollection;


          protected function lc_clients_faultHandler(event:FaultEvent):void


          Alert.show("Connection failed","Error");



          private function qryClients(res:ResultEvent):void


          var dp:Array=new Array();

          var i:Number=0;

          while(i < res.result.length)





          clientsArray = new ArrayCollection(dp);




          protected function application1_creationCompleteHandler(event:FlexEvent):void








          <s:RemoteObject id="lc_clients" fault="lc_clients_faultHandler(event)" showBusyCursor="true" source="lifecalc" destination="amfphp">

          <s:method name="getClients" result="qryClients(event)"/>



          <mx:DataGrid id="dg" dataProvider="{clientsArray}" doubleClickEnabled="true" showHeaders="false" horizontalCenter="0" verticalCenter="0" width="500" height="400">


          <mx:DataGridColumn headerText="Column 1" dataField="Firstname"/>

          <mx:DataGridColumn headerText="Column 2" dataField="Surname"/>

          <mx:DataGridColumn headerText="Column 3" dataField="DOB"/>








          class lifecalc


          var $db_host = 'localhost';

          var $db_name = 'flashhub_lifecalc';

          var $db_user = 'flashhub_lifecalc';

          var $db_pwd = 'xxxxxx';


          function lifecalc ()



          // Define the methodTable for this class in the constructor


          $this->methodTable = array(

          "getClients" => array(

          "description" => "Return a list of clients",

          "access" => "remote"





          function getClients ()


          $mysql = mysql_connect($this->db_host, $this->db_user, $this->db_pwd);

          mysql_select_db( $this->db_name);

          $Query = "SELECT ID,Surname,Firstname,DOB from clients";

          $Result = mysql_query( $Query );

          while ($row = mysql_fetch_object($Result))


          $ArrayOfClients[] = $row;


          return( $ArrayOfClients );








          <?xml version="1.0" encoding="UTF-8"?>



          <service id="amfphp-remoting" class="flex.messaging.services.RemotingService" messageTypes="flex.messaging.messages.RemotingMessage">

          <destination id="amfphp">


          <channel ref="remote"/>









          <channel-definition id="remote" class="mx.messaging.channels.AMFChannel">

          <endpoint uri="http://flashhub.net/insurance/amfphp/gateway.php" class="flex.messaging.endpoints.AMFEndpoint"/>


          <channel-definition id="local" class="mx.messaging.channels.AMFChannel">

          <endpoint uri="http://localhost/amfphp/gateway.php" class="flex.messaging.endpoints.AMFEndpoint"/>




          • 2. Re: populating DataGrid usin amfphp

            Why do  u  have 2 channel-definition. I implemented this code exactly but all I keep getting his the error pop-up box. I dont know what I am doing wrong, could it be the channel-definition or what cld it be.

            • 3. Re: populating DataGrid usin amfphp
              David_F57 Level 5



              I have 2 definitions so that when compiling the application it can be compiled for both local development and for the release build if you look at the beginning of the xml it defines which end point is to be used.


              Just note this is really old stuff now and probably using zendamf is an easier option(not necessarily the best option though).


              If you like using amfphp, this is an example where you don't need to compile the gateway configuration xml into the application, basically you define your channel set at runtime. Sorry I can't post the php at the moment as I am not at home(having an easter holiday ) so currently don't have access to the local development environment or my host.


              http://flashhub.net/filter/ (right click on app for the source).




              • 4. Re: populating DataGrid usin amfphp
                kolexinfos Level 1

                Thanks for the heads up. After I removed the channel-definition for the remote endpoint, the error stopped though. Now to the next step, I want send arguments along with my service calls, so that my php functions can make use of arguement in querying d database, wats my best option. I am considering using the <mx:arguments> tag within the method tag, but cant find my way around it yet or if it is possible can  i get book that fully describes using flex and amfphp cos all d materials I found on the internet are doing it bit per bit.

                • 5. Re: populating DataGrid usin amfphp
                  David_F57 Level 5



                  Unfortunately a lot of information on amfphp is spread over the internet and sometimes a little hard to follow, my personal preference is for amfphp due to its simplicity but the reality is Adobe have commited a lot of time and effort in giving us backend solutions for php through the zend framework.


                  Unless you are using flexbuilder which is now well and truly outdated it is best to use Flashbuilder and its DCD wizards which will help you by creating basic CRUD services for both the front end(actionscript) and the backend(php). There are lots of tutorials and blogs from Adobe and the community on Flashbuilders php wizards.


                  I haven't used amfphp for sometime now as I felt that for my clients it was a better choice to stick with products that are designed to work with Flahbuilder and fully supported by Adobe. If you wish to continue to work with amfphp  I can give you some links to my earlier stuff with amfphp once I return home from my vacation(I need access to my office so I can upload some code to my host).



                  • 6. Re: populating DataGrid usin amfphp
                    kolexinfos Level 1

                    Still waiting to hear from I need a material that would help me use

                    Remote Objects in Flex well using AMFPHP.



                    • 7. Re: populating DataGrid usin amfphp

                      Not sure I follow Remote Objects in reference to your earlier requirement to pass variables from Flex to php and return.  amfphp is very easy to use, Include a RemotingConnection.as class in your Flex source folder (google it)  then parameter 1, 2 ,3 etc can be most any data type that php can work with.

                      The yourPHPclass file is stored in your amfphp/services folder and contains the method you are calling. hostServer is URL of your amfphp server/gateway - something like   hostServer:String=svr+"/amfphp/gateway.php";   Then onCreateComplete or some other event callPHP();



                      Flex 3:

                                  public var gateWay : RemotingConnection;


                                  public function callPHP() : void
                                      gateway = new RemotingConnection( hostServer );
                                      gateway.call( "yourPHPclass.method", new Responder(onSuccess, onFault), parameter1,parameter2);


                      // set myGrid dp to the php query result


                                  public function onSuccess( phpReturnValue : Array ) : void
                                      myGrid.dataProvider=new ArrayCollection(phpReturnValue);


                      //on fault - there are much better fault handling examples out there.


                                 public function onFault( fault : String ) : void
                                      trace( fault );