I hope you can help me, I'm retrieving data from MySQL through PHP to jquery, currently, I'm getting this:
In this case, Jane Doe has two orders, which means two objects in the jQuery iteration. I'm not sure if it can be solved at the DB level, I think that would be great.
[
{
"nombre": "Jane Doe",
"telefono": "5512345678",
"calle": "Far far away",
"no_ext": "113",
"colonia": "Cumbres",
"cp": "12345",
"id_orden": "1",
"articulos": "2",
"total": "120",
"estado": "1"
},
{
"nombre": "Jane Doe",
"telefono": "5512345678",
"calle": "Far far away",
"no_ext": "113",
"colonia": "Cumbres",
"cp": "12345",
"id_orden": "2",
"articulos": "1",
"total": "90",
"estado": "3"
}
]
but how can I get this:
[
"usuario":
{
"nombre": "Jane Doe",
"telefono": "5512345678",
"calle": "Far far away",
"no_ext": "113",
"colonia": "Cumbres",
"cp": "12345",
"ordenes":{
{
"id_orden": "1",
"articulos": "2",
"total": "120",
"estado": "1"
}
{
"id_orden": "2",
"articulos": "1",
"total": "90",
"estado": "3"
}
}
}
]
it's an output in PHP, this is the code:
$sql = "SELECT c.nombre, c.telefono, c.calle, c.no_ext, c.colonia, c.cp, o.id_orden, o.articulos, o.total, o.estado, o.reg_fecha from CLIENTES c inner join ORDENES o ON c.telefono = o.id_cliente ORDER BY o.id_cliente, o.id_orden ASC";
$result = mysqli_query($connection, $sql) or die("Error en la ejecución de la consulta " . mysqli_error($connection));
$emparray = array();
while($row =mysqli_fetch_assoc($result)){
$emparray[] = $row;
}
echo json_encode($emparray);
Here is fiddle for Database schema