开发者

Populating a flex 4.5 DataGrid using PHP and MySQL

开发者 https://www.devze.com 2023-04-08 21:57 出处:网络
Hey so I recently started working with Flash Builder 4.5 so I am facing a lot of silly problems while building a mobile application, specially when connecting my application to a back end server.

Hey so I recently started working with Flash Builder 4.5 so I am facing a lot of silly problems while building a mobile application, specially when connecting my application to a back end server.

The thing I am working on currently should update a DataGrid and this data is retrieved from a MySQL Database.

This code displays the data clearly on the browser, however the datagrid is not updated. Instead it throws the following error.

TypeError: Error #1034: Type Coercion failed: cannot convert mx.utils::ObjectProxy@1e43bc1 to mx.collections.IList.
    at views::connectPHPHomeView/userRequest_resultHandler()[C:\Documents and Settings\s4710935\Adobe Flash Builder 4.5\connectPHP\src\views\connectPHPHomeView.mxml:17]
    at views::connectPHPHomeView/__userRequest_result()[C:\Documents and Settings\s4710935\Adobe Flash Builder 4.5\connectPHP\src\views\connectPHPHomeView.mxml:29]
    at flash.events::EventDispatcher/dispatchEventFunction()
    at flash.events::EventDispatcher/dispatchEvent()
    at HTTPOperation/http://www.adobe.com/2006/flex/mx/internal::dispatchRpcEvent()[E:\dev\4.5.1\frameworks\projects\rpc\src\mx\rpc\http\HTTPService.as:993]
    at mx.rpc::AbstractInvoker/http://www.adobe.com/2006/flex/mx/internal::resultHandler()[E:\dev\4.5.1\frameworks\projects\rpc\src\mx\rpc\AbstractInvoker.as:318]
    at mx.rpc::Responder/result()[E:\dev\4.5.1\frameworks\projects\rpc\src\mx\rpc\Responder.as:56]
    at mx.rpc::AsyncRequest/acknowledge()[E:\dev\4.5.1\frameworks\projects\rpc\src\mx\rpc\AsyncRequest.as:84]
    at DirectHTTPMessageResponder/completeHandler()[E:\dev\4.5.1\frameworks\projects\rpc\src\mx\messaging\channels\DirectHTTPChannel.as:451]
    at flash.events::EventDispatcher/dispatchEventFunction()
    at flash.events::EventDispatcher/dispatchEvent()
    at flash.net::URLLoader/onComplete()

Can someone please guide me through this?

EDIT

Here is the PHP code:

  <?php

    // -----------------------------------------------------------------------------
    // Initialization of the sql server
    // -----------------------------------------------------------------------------


    // hostname or ip of server (for local testing, localhost should work)
    $dbServer='localhost';

    // username and password to log onto db server
    $dbUser='root';
    $dbPass='password';

    // name of database
    $dbName='test';

    mysql_connect("$dbServer", "$dbUser", "$dbPass") or die(mysql_error());
    mysql_select_db("$dbName") or die(mysql_error());


    //Creating the table and inserting the values in it...



    mysql_query("CREATE TABLE IF NOT EXISTS mariah_transaction_log(
    id INT NOT NULL AUTO_INCREMENT, 
      PRIMARY KEY(id),
        date DATE, 
        details VARCHAR(150),
        debit DOUBLE,
        credit DOUBLE,
        balance DOUBLE)")
    or die(mysql_error()); 


    mysql_query("INSERT INTO mariah_transaction_log 
        (date, details, debit, credit, balance) VALUES('2011-08-08','Zellers','12.56','0.00','12343.54') ") 
    or die(mysql_error());  


    $username = $_GET["username"];

    // $namedGreeting = mysql_query("SELECT * FROM customer_list WHERE username='$username'") or die(mysql_error());
    // $row = mysql_fetch_array( $namedGreeting );

    // echo "Hello ".$row['name']."!";


    $Query = "SELECT * from mariah_transaction_log"; 
    $Result = mysql_query( $Query ); 

    $Return = "<users>"; 

    while ( $User = mysql_fetch_object( $Result ) ) 
    { 
    $Return .= "<user><date>".$User->date."</date><details>". 
                $User->details."</details><debit>". 
                $User->debit."</debit><credit>".
                $User->credit."</credit><balance>".
                $User->balance."</balance></user>"; 
    } 
    $Return .= "</users>"; 
    mysql_free_result( $Result ); 
    print ($Return) 



    ?>

And here is the MXML and AS3 part to my code: NOTE The sample XML result was added just for simplicity

<?xml version="1.0" encoding="utf-8"?>
<s:View xmlns:fx="http://ns.adobe.com/mxml/2009" 
        xmlns:s="library://ns.adobe.com/flex/spark" title="HomeView">

    <fx:Script>
        <![CDATA[
            import mx.collections.IList;
            import mx.collections.XMLListCollection;
            import mx.rpc.events.ResultEvent;






            protected function callService(event:ResultEvent):void
            {
                var results:XML = sampleResult;
                var resultsList:XMLListCollection = results.users as XMLListCollection;
                showTransactions_dg.dataProvider = resultsList;


            }

        ]]>
    </fx:Script>


    <fx:Declarations>


        <fx:XML id="sampleResult" xmlns="">
            <users>
                <user>
                    <date>2011-08-07</date>
                    <details>Camello Shop</details>
                    <debit>67.32</debit>
                    <credit>0.00</credit>
                    <balance>1233.42</balance>
                </user>

                <user>
                    <date>2011-08-07</date>
                    <details>Deposit</details>
                    <debit>0.00</debit>
                    <credit>600.00</credit>
                    <balance>1833.42</balance>
                </user>

            </users>



        </fx:XML>

        <s:HTTPService id="userRequest" url="{phpFile}"
                       method="GET" resultFormat="e4x" 
                       result="callService(event)" useProxy="false">
            <s:request xmlns="">
                <username>{username_txt.text}</username>
            </s:request>
        </s:HTTPService>


        <!-- The link to the php file -->
        <fx:String id="phpFile">http://localhost:8080/connectPHP/phpFile.php </fx:String>       

    </fx:Declarations>


    <s:TextInput id="username_txt" x="11" y="78"/>
    <s:Label x="11" y="55" text="Username"/>
    <s:Button id="getData_button" x="10" y="122" label="Done?" 
              click="userRequest.send()"/>
    <s:Label id="showName_lbl" x="13" y="173" text="show name开发者_如何学编程 here"/>


    <s:DataGrid id="showTransactions_dg" x="10" y="218" width="300" height="86">
        <s:columns>
            <s:ArrayCollection>
                <s:GridColumn dataField="date" headerText="Date" />
                <s:GridColumn dataField="details" headerText="Details"/>
                <s:GridColumn dataField="debit" headerText="Debit"/>
                <s:GridColumn dataField="credit" headerText="Credit"/>
                <s:GridColumn dataField="balance" headerText="Balance"/>

            </s:ArrayCollection>
        </s:columns>
    </s:DataGrid>
    <s:Label id="testLbl" x="13" y="334" width="266" height="90" text="Label"/>




</s:View>


In a spark DataGrid the dataProvider must implement an iList interface. It looks like you are returning XML and storing it in a generic object. A generic object does not support the IList interface.

Instead of binding the results of the remote call to the dataGrid's dataProvider, you should add a result handler to the HTTPService:

<s:HTTPService id="userRequest" url="{phpFile}"
               method="GET" resultFormat="object" useProxy="false" result="onResult(event)">

Then cast your data in the result Handler to something that implements IList. I recommend an XMLListCollection in this case.

protected function onResult(event:ResultEvent):void{
 var resultsXML : XML = event.results as XML
 showTransactions_dg.dataProvider = new XMLListCollection(resultsXML);
}

This code was written in the browser and may not be perfect.

0

精彩评论

暂无评论...
验证码 换一张
取 消