6 Replies Latest reply on May 18, 2010 6:16 AM by Matt Le Fevre

    Datagrid sort, new items to top

    djh88ukwb Level 1

      Hello All,

       

      I have the following datagrid

       

          <mx:DataGrid id="chatDG" width="60%" height="100%" x="0" y="0" fontSize="25" variableRowHeight="true" >
              <mx:columns>
                  <mx:DataGridColumn headerText="" dataField="message" wordWrap="true"/>
              </mx:columns>
          </mx:DataGrid>

       

      That is pulling information from a mysql database using the following php script

       

      <?php
      $hostname_conn = "localhost";
          $username_conn = "u";
          $password_conn = "pw";

          $conn = mysql_connect($hostname_conn, $username_conn, $password_conn);

          mysql_select_db("db");

          $query = "SELECT * FROM chat";

          $result = mysql_query($query);

          while($row = mysql_fetch_array($result))
          {
              echo "<chat><message>".$row['message']."</message></chat>\n";
          }

      ?>

       

      and following flex functions

       

                  private function GetAllMessage():void
                  {
                      getMessage.send();
                      setTimeout(GetAllMessage,1000);
                  }
                  private function viewMessageResult(evt:ResultEvent):void
                  {
                      chatDG.dataProvider = evt.result.chat;
                  }
              ]]>
          </mx:Script>
         
          <mx:HTTPService id="getMessage" url="getMessage.php" method="POST" result="viewMessageResult(event)">
              <mx:request xmlns="">
                  <data>{dataDump}</data>
              </mx:request>
          </mx:HTTPService>

       

      As it stands at the moment any new questions that are submitted to the database get shown at the bottom of the datagrid, is there an easy and efficent way to swap this ?  So that new message go to the top ?

       

      I have been looking about but cant seem to get it to work =[

        • 1. Re: Datagrid sort, new items to top
          BhaskerChari Level 4

          Try to sort in flex based on the primary key filed in the database table....or else if you are also having the createddate for the database table you mentioned then you can also sort by that CreatedDate field so that new message go to the top ...

           

          If this post answers your question or helps, please mark it as such.

           

          Thanks,

          Bhasker Chari

          1 person found this helpful
          • 2. Re: Datagrid sort, new items to top
            Matt Le Fevre Level 4

            There are multiple ways to accomplish this.

             

             

            You could keep the data entirely as it is, reverse the array collection in flex and then feed it into the datagrid.

             

             

            You could append a new field to your messages, such as the date it was created. Then you could sort on that. I imagine the best date to store with the messages would be the Unix Epoch; date("U") in php.

             

             

            Or, the way i would do this, would be to create a new integer primary key in your database as a messageID, and set it to auto increment. Then when retrieving your results, use a SQL statement such as;

             


            "SELECT * FROM chat ORDER BY messageID Desc"

             

            this method would remove the data manipulation required from flex, keeping it more efficient

            1 person found this helpful
            • 3. Re: Datagrid sort, new items to top
              djh88ukwb Level 1

              Thank you both for your input.

               

              Matt i think i am going to go with your last option.

               

              I have added the extra field into my MySql database sa you mentioned,

               

              However i am having trouble altering my php script.

               

              The script i am using is

              <?php
              $hostname_conn = "localhost";
                  $username_conn = "u";
                  $password_conn = "p";

                  $conn = mysql_connect($hostname_conn, $username_conn, $password_conn);

                  mysql_select_db("db");

                  $query = "SELECT * FROM chat";

                  $result = mysql_query($query);

                  while($row = mysql_fetch_array($result))
                  {
                      echo "<chat><message>".$row['message']."</message></chat>\n";
                  }

              ?>

               

              Where would i include the statement as you mentioned ?

               

              "SELECT * FROM chat ORDER BY messageID Desc"

               

              If you could help i would be most thankful

              • 4. Re: Datagrid sort, new items to top
                Matt Le Fevre Level 4

                 

                Where would i include the statement as you mentioned ?

                 

                "SELECT * FROM chat ORDER BY messageID Desc"

                 

                If you could help i would be most thankful

                 

                assuming your database now looks like this:

                 

                test.jpg

                 

                 

                 

                you'd just place it where your original query statement was:

                 

                <?php
                    $hostname_conn = "localhost";
                    $username_conn = "u";
                    $password_conn = "p";
                
                    $conn = mysql_connect($hostname_conn, $username_conn, $password_conn);
                
                    mysql_select_db("db");
                
                    $query = "SELECT * FROM chat ORDER BY messageID Desc";
                
                    $result = mysql_query($query);
                
                    while($row = mysql_fetch_array($result))
                    {
                        echo "<chat><message>".$row['message']."</message></chat>\n";
                    }
                
                ?>
                
                • 6. Re: Datagrid sort, new items to top
                  Matt Le Fevre Level 4

                  djh88ukwb wrote:

                   

                  Thank you

                   

                  no worries

                   

                  i imagine you'll be back later asking how to make your chat application more bandwidth efficient i'll see you then!