5 Replies Latest reply on Oct 28, 2009 6:00 AM by Matt Le Fevre

    Populating Datagrid with PHP Array

    virkof25

      Hi everyone,

       

      I have been searching on the web and I don't seem to find anything in how to populate the column of a datagrid with an Array.

      I have a DB call users and I need to get the names stored on the database and display it on a datagrid.

      Any ideas??

        • 1. Re: Populating Datagrid with PHP Array
          Matt Le Fevre Level 4

          PHP and Flex communicate by XML

           

          you'll need the PHP page to print XML, and the Flex to access it using a HTTPService.

           

           

          quick example in lax code,

           

          have the PHP script setup to print something like;

           

           

          print "<users>"
          
          while (there are users)
          {
          
          print "<username>".$username."</username>";
          
          }
          
          print "</users>"
          

           

          then, access it using a HTTP service in flex, with the url of the service directed to the PHP file

           

           

          set a result Event in the HTTP service to call a function...

           

           

          then in the function have something like;

           

           

          datagrid.dataprovider = event.result.users;

           

          set the LabelField property of the datagrid to "username"

           

          and your datagrid would populate with all the user names

          • 2. Re: Populating Datagrid with PHP Array
            virkof25 Level 1

            Thank you! I will give it a try.

            • 3. Re: Populating Datagrid with PHP Array
              Matt Le Fevre Level 4

              Here's a quick example using actual code which might be easier to understand;

               

              Flex code:

              <mx:HTTPService id="getStuff"  showBusyCursor="true" result="sortStuff(event)" method="POST"  url="http://localhost/stuff.php" useProxy="false"/>
              
              private function sortStuff(evt:ResultEvent):void
              {
                   datagrid1.dataprovider = evt.result.stuff;
                   datagrid1.labelField = "thing";
              }
              

               

              PHP code:
              
              <?php
              
                  $i = 0
                  
                  while (i < 10)
                  {
                      print "<stuff><thing>This is Row number ".$i."</thing></stuff>";
                      $i++;
                  }
              
              ?>
              
              • 4. Re: Populating Datagrid with PHP Array
                virkof25 Level 1

                Thank you soo much for replying!

                 

                Just one more question. I am reading those names from a DB. Shouldn't be the PHP file something like this?

                 

                <code>

                $slq = "SELECT user_name FROM tbl_staff";

                 

                $nameresult = mysql_query( $sql,$conn );

                while ($row=mysql_fetch_assoc($nameresult) )

                {

                print "$nameresult";

                }

                mysql_free_result( $nameresult );

                </code>

                • 5. Re: Populating Datagrid with PHP Array
                  Matt Le Fevre Level 4

                  virkof25 wrote:

                   

                  Thank you soo much for replying!

                   

                  Just one more question. I am reading those names from a DB. Shouldn't be the PHP file something like this?

                   

                  <code>

                   

                  $slq = "SELECT user_name FROM tbl_staff";

                   

                  $nameresult = mysql_query( $sql,$conn );

                  while ($row=mysql_fetch_assoc($nameresult) )

                  {

                  print "$nameresult";

                  }

                  mysql_free_result( $nameresult );

                  </code>

                   

                  it'll have to be something like:

                   

                  print "<usernames>";
                  
                  while (whatever)
                  {
                       print "<name>".$nameresult."</name>";
                  }
                  
                  print "</usernames>";
                  

                   

                  each name needs to be stored in a seperate 'name' node.

                   

                   

                  then set the datagrid dataprovider to evt.result.code

                   

                  set the datagrid labelField to "name"

                   

                   

                   

                  the above stores it in XML format, which is what Flex can read.