I'm having trouble displaying correctly in JSON
, a result from two tables in my database.
The situation is as follows: I have requests in a table (A) and the products of that request in another table (B).
The JSON result I have is like this:
{
"pedidos": [
{
"numOrc": "1",
"nomeclie": "CONSUMIDOR",
"valortotal": "2.077,20",
"formapagto": "2",
"emissao": "2013-02-15 16:09:11",
"codprod": "4775",
"qtdade": "1",
"valorunit": "500,00",
"tipopreco": "B"
},
{
"numOrc": "2",
"nomeclie": "MARCELO AUGUSTO BOTURA",
"valortotal": "2.077,20",
"formapagto": "2",
"emissao": "2013-02-15 16:21:56",
"codprod": "4775",
"qtdade": "1",
"valorunit": "500,00",
"tipopreco": "B"
}
]
}
As you can see, the result I have is only one product in each order. I needed the result to be as below (Warning for "tag Detalhes
"):
{
"pedidos": [
{
"numOrc": "2",
"nomeclie": "MARCELO AUGUSTO BOTURA",
"valortotal": "2.077,20",
"formapagto": "2",
"emissao": "2013-02-15 16:21:56",
"Detalhes":
[
{
"codprod": "4775",
"qtdade": "1",
"valorunit": "500,00",
"tipopreco": "B"
},
{
"codprod": "5555",
"qtdade": "3",
"valorunit": "800,00",
"tipopreco": "A"
}
]
}
]
}
My PHP code looks like this:
$sqlcode2 = mysql_query("Select a.numero as numOrc, a.nomeclie, a.valortotal, a.formapagto, a.emissao, b.codprod, b.qtdade, b.valorunit, b.tipopreco from orcamento a, prodorc b");
$jsonObj= array();
if($something == 'all')
{
while($result=mysql_fetch_object($sqlcode2))
{
$jsonObj[] = $result;
$teste= array('pedidos' => $jsonObj);
}
}
$final_res =json_encode($teste);
echo $final_res;