Kevin
Kevin

Reputation: 23634

Getting the output in Array Collection

/*
        [Bindable]
        public var rows1:ArrayCollection=new ArrayCollection([
            ['Google',      [{Projectname:"1", Client:0},
                                {Projectname:"2", Client:1},
                                {Projectname:"3", Client:2},
                                {Projectname:"4", Client:3}]
            ],
            ['Yahoo',               [{Projectname:"1", Client:4},
                                {Projectname:"2", Client:1},
                                {Projectname:"3", Client:2},
                                {Projectname:"4", Client:1}]
            ],
        ]);
        */

I have a table and i need to get the output in this format back to Flex which i am not able too... can anyone point me where i am going wrong in my php which does not throw this output above.

PHP code:

public function getAllProjects()
{
    $findings=array();
    $sql="SELECT id,projectname FROM project";
    $result=mysql_query($sql);
    if(!$result)
    {
        throw new Exception("QUERY FAILED.\n\n".mysql_error());
    }
    while(list($id,$projectname)=mysql_fetch_row($result))
    {
        $dataArray=array();
        $sql="SELECT state AS state FROM project WHERE id= '$id'";
        $result2=mysql_query($sql);
        if(!$result2)
        {
            throw new Exception("QUERY FAILED.\n\n".mysql_error());
        }
        while($row=mysql_fetch_array($result2))
        {
            $dataArray[]=$row;
        }
        $findings[]=array($projectname,$dataArray);
    }//while
    return $findings;
}

I know that PHP does not have ArrayCollection.

Desired output from PHP

$rows=array(
        array('ssss1232',array(array("projectname"=>"1", "clientname"=>0),
            array("projectname"=>"2", "clientname"=>1),
            array("projectname"=>"3", "clientname"=>3),
            array("projectname"=>"4", "clientname"=>3))
            ),
            array('sssss',array(array("projectname"=>"1", "clientname"=>0),
                    array("projectname"=>"2", "clientname"=>1),
                    array("projectname"=>"3", "clientname"=>2),
                    array("projectname"=>"4", "clientname"=>1))
            ),

    );

Upvotes: 0

Views: 197

Answers (2)

Amarghosh
Amarghosh

Reputation: 59451

Even if you successfully make a string that looks like an ArrayCollection and send it to flex, you still would have to parse it back at the flex side - you cannot just initialize an array collection from a string. Use json (which would be straightforward here, but you need a library to parse it at the flex side) or xml (flex has native support for e4x) instead.

Upvotes: 0

Justin Johnson
Justin Johnson

Reputation: 31300

The final format that you've described looks like JSON. You might be able to just do

$projectData = getAllProjects();
...
$projectDataFormatted = json_encode($projectData);

Upvotes: 1

Related Questions