I want to give a specific format to a JSON that I do from PHP to then receive it in a variable in JS and fill a table with it, but I do not know how to do it.
Here the JSON is created:
<?php
$conexion=mysqli_connect("localhost", "root", "", "pruebas_maps") or die ("Problemas con la conexion");
$consulta="SELECT * FROM registros2";
$registros=mysqli_query($conexion,$consulta) or die ("Problemas con la consulta");
$c = array();
while ($reg=mysqli_fetch_array($registros))
{
array_push($c,
array($reg['id'],$reg['cliente'],$reg['metodo_pago'],$reg['hora'],$reg['fecha_entrega'], $reg['sem_entrega']));
}
echo json_encode($c, JSON_FORCE_OBJECT)
?>
And print it with this structure:
{
0: {
0: "100",
1: "Carlos Luna",
2: "Credito 30D",
3: "14:15:00",
4: "2018-05-09",
5: "19"
},
1: {
0: "104",
1: "sadfsadf",
2: "Contado",
3: "12:32:00",
4: "2018-06-18",
5: "25"
}
}
And I need to print it like this:
[
0: "{"id":"100","cliente":"Carlos Luna","metodo_pago":"Credito 30D","hora_entrega":"14:15:00","fecha_entrega":"2018-05-09","sem_entrega":"19"}",
1: "{"id":"104","cliente":"sadfsadf","metodo_pago":"Contado","hora_entrega":"12:32:00","fecha_entrega":"2018-06-18","sem_entrega":"25"}"
]
The function where I will receive it is this:
<script>
$(document).ready(function(){
$('#ejemplo1').DataTable();
var res = <?php echo json_encode($c, JSON_FORCE_OBJECT) ?>;
MostrarRegistros();
});
function MostrarRegistros(){
$('#tblRegistros tr:not(:first)').remove();
for(var i in res)
{
var con = JSON.parse(res[i]);
$('#tblRegistros tr:last').after('<tr><td>'+con.id+'</td><td>'+con.cliente+'</td><td>'+con.metodo_pago+'</td><td>'+con.fecha_entrega+'</td><td>'+con.sem_entrega+'</td></tr>');
}
}
</script>
Thank you in advance for your answers.