2 Replies Latest reply on Aug 10, 2007 6:13 AM by parkrrr

    Variable XML/HTTP functions in Flex 2

    parkrrr
      I am writing a Flex 2 program that will allow the management of the company I work for to enter time tracking data. Currently this is done in Excel and is very inefficient.

      I am using MySQL to hold the data, and a single PHP script to query. The PHP script returns all values in XML format. Here is an example:

      query.php?table=employees&data=ID

      Returns: <root><employees><ID>1</ID></employees><employees><ID>2</ID></employees><employees><ID>3< /ID></employees></root> etc

      Now, I have it so if I append a third value:

      query.php?table=employees&data=ID&dname="Doe, John"

      Returns: <root><employees><ID>1</ID></employees><employees></root>

      Now, the problem is I have no idea how to query this in Flex. Ideally I'd like a function such as "getID("Doe, John")" and it would query his ID and I could reuse it. HTTP services and an XML data provider are unable to accomplish this as far as I know. Any help would be most appreciated, thank you!
        • 1. Re: Variable XML/HTTP functions in Flex 2
          ntsiii Level 3
          No, HTTPService and XML are the solution.

          A HTTPService call is IDENTICAL to entering the url in a browser. You can "post" or "get" by specifying the "method" on the HTTPService tag. Your code will look something like this:
          private function getEmployee(sDName:String):void {
          var oRequest:Object = {table:"employee",data="ID",dname:sDName};
          ds.send(oRequest);

          Since all RPC data service calls are asynchronous, you will need to use a result handler function to receive the data. Belwo are some more complete snippets of code.

          Tracy

          Sample code using HTTPService, e4x, handler function to populate a list item.
          Also shows usage of AsyncToken.

          The DataGrid tag:
          <mx:DataGrid id="dg" dataProvider="{_xlcMyListData}" .../>


          The HTTPService tag:
          <mx:HTTPService id="service" resultFormat="e4x" result="onResult(event)" fault="..../>

          Script block declaration:
          import mx.rpc.Events.ResultEvent;
          [Bindable]private var _xlcMyListData:XMLListCollection;

          Invoke send:
          var oRequest:Object = new Object();
          oRequest.Arg1 = "value1";
          var callToken:AsyncToken = service.send(oRequest);
          token.callId = "myQuery1";

          Result Handler function:
          private function onResult(oEvent:ResultEvent):void {
          var xmlResult:XML = XML(event.result); //converts result Object to XML. can also use "as" operator
          var xlMyListData:XMLList = xmlResult.myListData; //depends on xml format, is row data
          _xlcMyListData = new XMLListCollection(xlMyListData); //wrap the XMLList in a collection
          trace(_xlcMyListData.toXMLString()); //so you can see exactly how to specify dataField or build labelFunction
          var callToken:AsyncToken = oEvent.token;
          var sCallId = callToken.callId; //"myQuery1"
          switch(sCallId) {
          case "myQuery1":
          doQuery2();
          break;
          ...
          }
          }//onResult
          • 2. Variable XML/HTTP functions in Flex 2
            parkrrr Level 1
            ntsiii,

            Thank you very much! This is very much what I needed. I appreciate your thorough response.