John Michael Rivera John Michael Rivera - 1 month ago 12
PHP Question

PHP echo json with only two category and multiple records

Hi Guys need some help.

So I'm trying to echo a json array format like this:

{"ttype": [{ "id":"1", "type":"Order No.","code":"ORDNO" },
{"id":"2", "type":"Orderline", "code":"ORDLINE"} ]

,"input":[{"id":"1","order":"Order No.", "limit":"5"},
{"id":"2","order":"Order No.", "limit":"5"},
{"id":"2", "order":"Order Line No.","limit":"10"}]}


So here is my code:

$query = "SELECT * FROM `order` WHERE is_deleted = 0 ORDER BY id ASC";
$que = mysql_query($query);
while($row = mysql_fetch_array($que)){
$id = $row['id'];
$keyword = $row['keyword'];
$parameters = $row['parameters'];

$json = array(
'ttype'=>array(array(
'id'=>$id,
'type'=>'',
'code'=>$keyword
)),'input'=>array(array(
'id' =>$id,
'order'=>'',
'limit'=>20
))
);

echo json_encode($json);
}


Problem is here is what i'm getting:

{"ttype":[{"id":"1","type":"","code":"ORDNO"}],"input":[{"id":"1","order":"","limit":20}]}{"ttype": [{"id":"2","type":"","code":"ORDLINE"}],"input": [{"id":"2","order":"","limit":20}]}


Any idea how to get the right format given properly?
Thanks

Answer

You have to have two variables on while loop, then make your variables json_encode with your structure accordingly. This is the way you can get your work done