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
this block should generate with
below block should generate with
Any idea how to loop two queries to generate above json data?
Thanks
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);
this block should generate with
"attr" => array(
"id" => 1,
"rel" => "drive"
),
"data" => "My Documents",
SELECT id,rel,data FROM tbl_drives ORDER BY id
below block should generate with
"children" => array(
array(
"attr" => array(
"id" => 2,
"rel" => "file"
),
"data" => "file1.doc",
"state" => ""
)
SELECT id,rel,data FROM tbl_files WHERE parent_id = '$id' ORDER BY id
Any idea how to loop two queries to generate above json data?
Thanks
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
still couldn't find a way to loop this array
seems like putting above inside a while loop is impossible
array(
"attr" => array(
"id" => 2,
"rel" => "file"
),
"data" => "file1.doc",
"state" => ""
)
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);
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);
ASKER
problem is only loop
Open in new window