Link to home
Start Free TrialLog in
Avatar of 3xtr3m3d
3xtr3m3d

asked on

generate json data with mysql help

Hi

This is the sample of data type i need to generate with 2 mysql queries

$obj = array(
				"attr" => array(
								"id" => 1,
								"rel" => "drive"					
							 ),
				"data" => "My Documents",
	
		"children" => array(
		array(
				"attr" => array(
								"id" => 2,
								"rel" => "file"
							),
				"data" => "file1.doc",
				"state" => ""
			 )
	)	
);
		
echo json_encode($obj); 

Open in new window


this block should generate with

				"attr" => array(
								"id" => 1,
								"rel" => "drive"					
							 ),
				"data" => "My Documents",

Open in new window


SELECT id,rel,data FROM tbl_drives ORDER BY id

Open in new window


below block should generate with

		"children" => array(
		array(
				"attr" => array(
								"id" => 2,
								"rel" => "file"
							),
				"data" => "file1.doc",
				"state" => ""
			 )

Open in new window


SELECT id,rel,data FROM tbl_files WHERE parent_id = '$id' ORDER BY id

Open in new window



Any idea how to loop two queries to generate above json data?

Thanks
ASKER CERTIFIED SOLUTION
Avatar of CvD
CvD
Flag of Netherlands image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of 3xtr3m3d
3xtr3m3d

ASKER

Thanks for the reply thats really helpful

problem is only loop

		array(
				"attr" => array(
								"id" => 2,
								"rel" => "file"
							),
				"data" => "file1.doc",
				"state" => ""
			 )

Open in new window


SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
still couldn't find a way to loop this array

		array(
				"attr" => array(
								"id" => 2,
								"rel" => "file"
							),
				"data" => "file1.doc",
				"state" => ""
			 )

Open in new window


seems like putting above inside a while loop is impossible
Try someting like below:

function loopArray($arr) {
  foreach ($arr as $key => $val) {
    if (is_array($val) loopArray($val);
    else {
      //do something
    }
  }
}

USAGE:
$obj = array("attr" => array( "id" => 2,"rel" => "file"),"data" => "file1.doc","state" => "");
loopArray($obj);