0
I’m having trouble properly displaying in JSON
, a result originated from two tables in my database.
The situation is as follows: I have orders in a table (A) and the products in that order 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 (Attention to "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;
The same that I started, in this case is what I refer to at the beginning of the answer, I’m really sorry I read it here and it got a little vague anyway. But I mean the beginning when I say "if the budget table only holds the headings..."
– Ronaldo Filho
As soon as I wrote, I understood what I meant rs. Thank you very much for your help.
– Rene Sá
Thanks even, even I after you spoke did not understand well what I wrote, but it is so living and learning. I hope to have helped. Good morning
– Ronaldo Filho
I did INNER JOIN, now the result shows me all the products, but not in the format I needed... Now it’s "repeating the header" of the pain, every time you have a product... Can you help me?
– Rene Sá
Great, in case what you can do is: loop on the results that were collected mounted the JSON header and then add the products, it would be more or less like this Budget 1: Header : id = 1 client = 1 value = 10,00 products: id = 1 value = 5,00 id = 2 value = 5,00 in the case how the repeat header would have you mount the loop by checking if each record belongs to the header of the previous record, if it belongs you launch the product in the same header as a new JSON object
– Ronaldo Filho