Skip navigation
Currently Being Moderated

populating DataGrid usin amfphp

Apr 27, 2010 8:29 AM

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{

 

listBooks.users(1);

}

 

 

 

private function userdatat(event:ResultEvent):void{

dg.dataProvider = event.result;

}

]]>

</mx:Script>

 

<mx:RemoteObject

 

 

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

 

 

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

</mx:RemoteObject>

 

 

 

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

 

 

<mx:columns>

 

 

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

 

 

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

 

 

 

</mx:columns>

 

</mx:DataGrid>

 

 
Replies
  • Currently Being Moderated
    Apr 27, 2010 8:57 AM   in reply to emirbigezlo

    hi,

     

    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

     

    Application

    ========

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

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

       xmlns:s="library://ns.adobe.com/flex/spark"

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

    <fx:Script>

    <![CDATA[

    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)

    {

    dp.push(res.result[i]);

    i++;

    }

    clientsArray = new ArrayCollection(dp);

    trace("hello");

    }

     

    protected function application1_creationCompleteHandler(event:FlexEvent):void

    {

    lc_clients.getOperation("getClients").send();

    }

     

    ]]>

    </fx:Script>

    <fx:Declarations>

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

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

    </s:RemoteObject>

    </fx:Declarations>

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

    <mx:columns>

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

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

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

    </mx:columns>

    </mx:DataGrid>

    </s:Application>

     

    PHP

    ===

    <?php

    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 );

    }

    }

    ?>

     

     

    config-services

    ===========

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

    <services-config>

    <services>

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

    <destination id="amfphp">

    <channels>

    <channel ref="remote"/>

    </channels>

    <properties>

    <source>*</source>

    </properties>

    </destination>

    </service>

    </services>

    <channels>

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

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

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

    </channel-definition>

    </channels>

    </services-config>

     
    |
    Mark as:
  • Currently Being Moderated
    Apr 23, 2011 11:49 PM   in reply to David_F57

    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.

     
    |
    Mark as:
  • Currently Being Moderated
    Apr 24, 2011 12:06 AM   in reply to kolexinfos

    Hi,

     

    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).

     

     

    David

     
    |
    Mark as:
  • Currently Being Moderated
    Apr 24, 2011 6:12 AM   in reply to David_F57

    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.

     
    |
    Mark as:
  • Currently Being Moderated
    Apr 24, 2011 7:06 AM   in reply to kolexinfos

    Hi,

     

    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).

     

    David

     
    |
    Mark as:
  • Currently Being Moderated
    Apr 25, 2011 12:09 AM   in reply to David_F57

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

    Remote Objects in Flex well using AMFPHP.

     

    Thanks.

     
    |
    Mark as:
  • Currently Being Moderated
    Apr 25, 2011 2:01 PM   in reply to kolexinfos

    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 );
                }

     
    |
    Mark as:

More Like This

  • Retrieving data ...

Bookmarked By (0)

Answers + Points = Status

  • 10 points awarded for Correct Answers
  • 5 points awarded for Helpful Answers
  • 10,000+ points
  • 1,001-10,000 points
  • 501-1,000 points
  • 5-500 points