11 Replies Latest reply on Apr 13, 2010 6:39 PM by David_F57

    Flex and MySQL via PHP!

    playwiz

      Hey guys!  I'm working on a project for class and decided to try Flex.  I have some data stored in MySQL that I'm retrieving with PHP.  Then, PHP converts the data into XML format to be used in Flex.  However, I'm wondering if it would be generally much easier to just use data services and have PHP send the data as strings?

       

      I'm building a schedule builder.  I have all the courses, location, time, and instructor in the database.  One problem I am encountering right now (mind you, I'm very new to Flex) is that I am unable to turn an ArrayCollection (or part of it) into a new Array for use in populating my datagrid.

       

      All suggestions are welcomed.  Thanks!

        • 1. Re: Flex and MySQL via PHP!
          David_F57 Level 5

          Hi,

           

          If you are using flashbuilder, its all done for your using zendphp. The data wizard will access your mysql database and create the required php for the backend and the vo's(value objects) for the flex front end.

           

          There are several tutorials around, if you go to AdobeTV you'll find something there.

           

           

          David.

          1 person found this helpful
          • 2. Re: Flex and MySQL via PHP!
            playwiz Level 1

            Thanks for the reply, David.  I had no idea about ZendPHP and Flash Builder.  I will upgrade to Flash Builder from Flex Builder 3.

             

            However, this is not a project I'm working on alone.  The objective of the project is to work with two others; there is a front-end, back-end, and middle. I'm doing the front-end.  The middle is retrieving info from the back-end person (who in turn is using scripts to scrape data into the database).  We are supposed to communicate via functions and stuff.  I cannot bypass the middle guy.  I have to let him handle the requests to the database while I display the results.

             

            My question really is:  what is the easiest way for me to retrieve data from MySQL through PHP-- XML format or strings sent via PHP functions to Flex?

            • 3. Re: Flex and MySQL via PHP!
              Matt Le Fevre Level 4

              playwiz wrote:

               

              Thanks for the reply, David.  I had no idea about ZendPHP and Flash Builder.  I will upgrade to Flash Builder from Flex Builder 3.

               

              However, this is not a project I'm working on alone.  The objective of the project is to work with two others; there is a front-end, back-end, and middle. I'm doing the front-end.  The middle is retrieving info from the back-end person (who in turn is using scripts to scrape data into the database).  We are supposed to communicate via functions and stuff.  I cannot bypass the middle guy.  I have to let him handle the requests to the database while I display the results.

               

              My question really is:  what is the easiest way for me to retrieve data from MySQL through PHP-- XML format or strings sent via PHP functions to Flex?

               

              depends what data you're trying to send

               

               

              are you sending complex arrays of information? with individual complex objects making up a collection?

               

              if so, use XML formatting.

               

              or are you simply sending individual bits of information that don't need to have a structure and simply need to get from A to B without a great deal of data-manipulation?

               

              if so, use simple strings.

               

               

               

              Personally, i always lean towards using XML. But that's due to my experience in large team projects where you really have to think about how the information you're going to be using is handled, it's also vastly easier to maintain, and to append to suit future needs.

               

              although again, that's only personal opinion.

               

              If you're working in a team like you said, i'd highly recommend using an XML format. It'll allow others to quitely look through your code and understand how it all fits together, although i imagine it would be a good idea for all of you to sit down and discuss how you're going to be handling your data, what your PHP scripts are going to do, and how much of a backend support are they going to provide.

              1 person found this helpful
              • 4. Re: Flex and MySQL via PHP!
                kkm19 Level 1

                where is adobetv url??

                • 5. Re: Flex and MySQL via PHP!
                  SpaghettiCoder Level 3

                  tv.adobe.com

                  • 6. Re: Flex and MySQL via PHP!
                    David_F57 Level 5

                    Hi,

                     

                    XML is the 'standard' way, strings not so much, xml is basically a string so if it makes sense to use one methodology for everything, code starts to look messy when you bounce between 2 methodolgies which would have been just as efficient using 1. Looking at serialised data is a very good option, In a team if the front end guy uses flex and can have better performance etc the back end guy needs to listen and look at the options.

                     

                    Its about team,

                    -backend guy needs you to use php so you find the best tool for that,

                     

                    So you go to the backend guy tell him if the backend is amfphp(fast data serialisation and all you need are the amfphp classes on the php/mysqlserver), data throughput will be faster and more secure as data is transfered in compressed binary format. At your end you have nice flex freindly arrays to handle.

                     

                    Below is a bit of code to give you an idea of flex 3 with 'php' this is all you need to query a table in a mysql database prepare the information and display it in a grid.

                     

                    David.

                     

                    The Flex bits

                    ==========

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

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

                    </mx:RemoteObject>

                    private function getInfo(): void

                    {

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

                    }

                     

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

                    }

                     

                     

                    private function qryClient(res:ResultEvent):void

                    {

                    var myObject : Object = res.result;

                    client = new ClientData(res.result);

                    }

                     

                    <mx:DataGrid id="dg" dataProvider="{clientsArray}" doubleClickEnabled="true" click="getclient()" showHeaders="false" left="2" right="2" top="2" bottom="36">

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

                    The Php Bit
                    <?php
                    class lifecalc
                    {
                    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(localhost, "root", "");
                    mysql_select_db( "lifecalc" );
                    $Query = "SELECT ID,Surname,Firstname,DOB from clients";
                    $Result = mysql_query( $Query );
                    while ($row = mysql_fetch_object($Result)) {
                    $ArrayOfClients[] = $row;
                    }
                    return( $ArrayOfClients );
                    }
                    function getClient($which_id)
                    {
                      $mysql = mysql_connect(localhost, "root", "");
                      mysql_select_db( "lifecalc" );
                      return mysql_query("select * FROM clients where ID='$which_id'");
                    }
                    }
                    ?>

                     

                     

                     

                    • 7. Re: Flex and MySQL via PHP!
                      playwiz Level 1

                      Wow, thanks for the elaborate example!

                       

                       

                      Here is what I have noticed in Flex -- I can only use ONE HTTPService tag at a time.  I tried creating multiple <HTTPService> tags, but only the first runs.  What I am trying to do in essence is to be able to have the HTTPService variable.send() call different links depending on certain events in the application.  I don't know if that makes sense.

                       

                      Now here is where I'm really stuck --  I'm able to use the HTTPService tag to get the XML formatted data as an ArrayCollection. Here is an example of what the XML looks like:

                       

                      <courses>

                           <course id="12" subject="MATH 201" section="002" title="Calculus I" />

                           <course id="13" subject="CS 301" section="001" title="Algorithm Design" />

                           <course id="14" subject="MGMT 401" section="004" title="Finance" />

                      </courses>

                       

                       

                      This XML gets stored in an ArrayCollection variable.  What I want to do now is to create a new Array variable type with ONLY a list of all the TITLES from the ArrayCollection.  How do I do that?  It seems to be easy to go from Array to ArrayCollection, but not vice versa; maybe I'm missing something...

                       

                      Thanks!

                      • 8. Re: Flex and MySQL via PHP!
                        SpaghettiCoder Level 3

                        Why do you only need the titles for?

                         

                        You made a server call for 3 columns.  If you only needed title, do a server call for just the one column?

                        • 9. Re: Flex and MySQL via PHP!
                          playwiz Level 1

                          I need to use different pieces of the XML data in different areas of my application.  Since I cannot have more than one HTTPService tag, this is the only way I can think of doing this.  Enlighten me if you have a better solution.

                           

                          Suppose we store that XML data into an ArrayCollection called arrData, this is what I have tried to do and it hasn't worked:

                           

                          var arrTitles:Array = new Array();

                           

                          1)     arrTitles = arrData["title"].source;

                           

                          2)     arrTitles = arrData[title].source;

                           

                          3)

                          for (var i:int = 0; i < arrData.length; i++) {

                            arrTitles[i].push( arrData["title"][i] );

                          }

                           

                          None of those worked...

                          • 10. Re: Flex and MySQL via PHP!
                            SpaghettiCoder Level 3

                            Not exactly sure how you've structured your app, but In most cases I just use the ArrayCollection as is.

                             

                            arrData.getItemAt(i).title

                            • 11. Re: Flex and MySQL via PHP!
                              David_F57 Level 5

                              hi,

                               

                              for the arraycollection it should be arrData[i].title

                               

                              and you push a value into the array

                               

                              arr.push(arrData[i].title)

                               

                              David.