Help with php and json

0

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;
    
asked by anonymous 13.10.2015 / 16:00

1 answer

1

Good morning, depending on the structure of your tables you can join the table of products using the budget table, if in the budget table you save only the budget headings and you have the products posted in the separate budgets in another table, you can use the top tip, if not, if there is no header and products separation, you will have to in the method that does the assembly of JSON read the header of the budget retrieved in the query and in a loop check if the record where the cursor is matched to that captured budget, if you match, you will need to add an object to JSON of budget with the product information.

    
13.10.2015 / 16:11