【发布时间】:2017-05-09 02:19:45
【问题描述】:
我有两张桌子,
1 - 类别
2 - 子类别
Category (table 1)
---------------
Cid | Name
1 | Vegetable
2 | Fruit
SubCategory (table 2)
----------------
Cid | Sid | S_name
1 | 1 | Carrot
1 | 2 | Beans
2 | 3 | Mango
我想要 Json 中的结果:-
-> Category found
-> Vegetable
-> carrot
-> beans
-> Fruit
-> mango
我正在使用下面的代码!任何人都可以帮助我更正我的代码并获得我上面提到的输出!我试过很多次。但没有得到解决方案!请帮助我获得目录形式的输出。谢谢
`$srtResult = "SELECT * FROM `category` `c` LEFT JOIN `subcategory` `s` ON
(`c`.`cid` = `s`.`cid`)";
//Execute Qu**strong text**ery
$result=mysql_query($srtResult);
//Iterate Throught The Results
while ($row = mysql_fetch_assoc($result, MYSQL_ASSOC))
{ $count = $row['cid'];
while($row['cid'] = $count)
{ $subcatitem[] = Array( "cid" => $row['cid'], "sid" =>$row['sid'],
"sname" => $row['sc_name'] );
}
$json['category'][] = Array("category" => Array( "cid" => $row['cid'],
"name" => $row['name'], "subcategory" => Array( $subcatitem)));
}
header('Content-type: application/json');
echo json_encode($json);`
【问题讨论】: