9 Replies Latest reply on Apr 27, 2010 1:32 PM by EWN-CMI

    Problem in Data Grid

    VyawahareAmit

      Hello all,

       

      I am trying to get all username from my database on to DataGrid. Connection with the database and showing data on data grid are working absolutely right. My question is

       

           I want to show that data grid in horizontal format not in default vertical format. I just have one Column in that data grid. Lets take a example, what i am asking is if you have one Column in data grid with 10 rows. I just want to convert that 10 rows into 10 column with one row. Default data grid will look like first figure, i just want to convert it into second figure.

       

      -------------                                    |     |     |     |     |

      -------------                                           

      -------------                                       

      -------------                                    

      ------------

       

      Please help me to solve this problem.

       

      Thank you,

      -- Amit

        • 1. Re: Problem in Data Grid
          EWN-CMI Level 1

          I would think that you would need to build a new Collection based off the original and populate it so that each row is built from the columns from the original and then use that as your datasource.  Good luck.

          • 2. Re: Problem in Data Grid
            VyawahareAmit Level 1

            I am not sure i got what you are asking me to do. Can you send me a code or algorithm that way i can modify it to get my horizontal data grid working.

            Thank you for your reply.

             

            -- A

            • 3. Re: Problem in Data Grid
              EWN-CMI Level 1

              Difficult to do without much data from your app, but here goes.

              Currently you have returned an arrayCollection from your database via your httpService and it has 10 rows with a single column -Correct?

               

              If the assumptions above are correct then you will need to loop through your existing arrayCollection 0-9 and create a new arrayCollection.

               

               

              [Bindable] public var newDPsource:Array=new Array;

               

              private function makeDataProvider():void {

                    for (var i:int=0;i<oldCollection.length;i++)                //oldCollection already declared somewhere in your code
                            { newDPsource[0][i]=oldCollection.source[i];  //something like this will put each row of the oldCollection into a column of the new.

                       }

                   oldCollection.source=newDPsource;                      //assign the oldCollection the new source with columns instead of rows.

              }

               

               

              I have not tried this, just thought that it would be a way to do it.  Let me know if it works, might have to push stuff into the array newDPsource.

              • 4. Re: Problem in Data Grid
                VyawahareAmit Level 1

                I tried to use your algorithm to modify my code but i am not able to do that. For this example i am not using any httpservice i am  using data services. I created a PHP file i just need to import it into data services planel and drag that services onto data grid it will automatically bring all the data into that data grid.  Can you tell me what i need to modify to achive my goal. am using following code

                 

                 

                protected function dataGrid_creationCompleteHandler(event:FlexEvent):void

                {

                getHorizontalCommentResult.token = horizontalCommentsService.getHorizontalComment();

                }

                 

                .

                .

                .

                <mx:DataGrid x="159" y="123" id="dataGrid" creationComplete="dataGrid_creationCompleteHandler(event)" dataProvider="{getHorizontalCommentResult.lastResult}" height="223" width="245">

                <mx:columns>

                <mx:DataGridColumn headerText="Comments" dataField="Comments"/>

                </mx:columns>

                 

                PP file

                 

                 

                <?php

                 

                class HorizontalCommentsService{

                public function getHorizontalComment() {

                 

                $connection = mysqli_connect("localhost", "root", "root", "my_db", 8889) or die(mysqli_connect_error());

                $sql = "SELECT * FROM photo_comments ORDER BY Image_ID DESC";

                $result = mysqli_query($connection, $sql) or die('Query Failed: ' . mysql_error($connection));

                 

                $rows = array();

                while($row = mysqli_fetch_object($result)) {

                $rows[ ] = $row;

                }

                return $rows;

                }

                }

                ?>

                • 5. Re: Problem in Data Grid
                  EWN-CMI Level 1

                  Some of the assumptions I made were wrong. I think it will be easier for you just to do the conversion in php.  You are not returning a single column so you will need to identify what col you want for rows.  Basically, you are returning an array from your php.  If you change the SQL to SELECT Image_ID, Comments  instead of * then the array indicies would be newDPsource[0][i]=getHorizontalCommentResult.lastResult[i][1];  This puts Row i Col 1 into Row 0 Col i and repeats until there are no more rows in your original array. I think this should return a single row with a bunch of cols.  IF you need other data from photo_comments

                   

                  <?php

                   

                  class HorizontalCommentsService{

                  public function getHorizontalComment() {

                   

                  $connection = mysqli_connect("localhost", "root", "root", "my_db", 8889) or die(mysqli_connect_error());

                  $sql = "SELECT Image_ID,Comments FROM photo_comments ORDER BY Image_ID DESC";

                  $result = mysqli_query($connection, $sql) or die('Query Failed: ' . mysql_error($connection));

                   

                  $rows = array();

                  $cols=array();

                  while($row = mysqli_fetch_object($result)) {

                  $rows[ ] = $row;

                  }

                   

                  for ($i=0; $i < mysql_num_rows ($result); $i++)              
                                 { $cols[0][$i]=rows[$i][1]; 

                           }

                  return $cols;

                  }

                  }

                  ?>

                  • 6. Re: Problem in Data Grid
                    VyawahareAmit Level 1

                    This table contain only two column, so * will work. When i try to use your code i am getting following error on PHP side.

                     

                    @ $cols[0][$i]=rows[$i][1];

                    PHP Parse error:  syntax error, unexpected '[' in /Applications/MAMP/htdocs/HorizontalCommentsService.php

                     

                    If i use $cols[0][$i]=$rows[$i][1];

                    PHP Fatal error:  Cannot use object of type stdClass as array in /Applications/MAMP/htdocs/HorizontalCommentsService.php

                     

                    I think we are getting this error because of two dimentional array. What you think?

                     

                    Thank you,

                    -- Amit

                    • 7. Re: Problem in Data Grid
                      EWN-CMI Level 1

                      Check the bottom of this thread,  http://www.webdeveloper.com/forum/showthread.php?t=54361

                       

                      and you fixed the syntax error  $rows.


                      Good Luck.

                      1 person found this helpful
                      • 8. Re: Problem in Data Grid
                        VyawahareAmit Level 1

                        I tried to change my code according to that blog. But still i am getting the error on that line. Sorry to trouble you but please help me to solve this problem.

                         

                        I am getting same error on

                         

                        $cols[0][$i] = $rows[$i][1];

                         

                        Thank you,

                        -- Amit

                        • 9. Re: Problem in Data Grid
                          EWN-CMI Level 1

                          Yes we already determined that line needed work?  Per the link I provided that line needs to be changed to something like this.  I am sorry I do not have time to test/prototype for you, there should an infinite amount of information on php sites.  It is a fairly easy task, just have to get the syntax down and for me that takes looking it up since I don't do much more than basic php.

                           

                          for ($i=0; $i < mysql_num_rows ($result); $i++)
                                   {
                                         $comment=$rows[$i]->Comments;
                                         $cols[0][$i]=$comment;  

                                   }