Format JSON

0

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.

    
asked by Carlos Roberto Luna Ochoa 20.06.2018 в 19:14
source

2 answers

2

If you want to give the specific format I recommend you change your array

    array($reg['id'],$reg['cliente'],$reg['metodo_pago'],$reg['hora'],$reg['fecha_entrega'], $reg['sem_entrega']));

for the following:

    array('id'=>$reg['id'],
       'cliente'=>$reg['cliente'],
       'metodo_pago'=>$reg['metodo_pago'],
       'hora'=>$reg['hora'],
       'fecha_entrega'=>$reg['fecha_entrega'], 
       'sem_entrega'=>$reg['sem_entrega'])
    );

And so you assign the identifiers you want, my friend, greetings. and I leave the php arrays management link: PHP ARRAYS MANUAL

    
answered by 20.06.2018 в 19:20
0

You are having as much trouble getting the data as you want because you are not combining the right methods for it:

  • get the data using an associative array
  • do not use array_push , but simply filling your array with each row
  • building a JSON without forcing it to object

Write the code like this and you'll get the object as you say you want it:

<?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, MYSQLI_ASSOC))
    {
        $c[]=$reg;

    }
    echo json_encode($c)
?>

You should have an exit like this:

[{
    "id": "100",
    "cliente": "Carlos Luna",
    "metodo_pago": "Credito 30D",
    "hora_entrega": "14:15:00",
    "fecha_entrega": "2018-05-09",
    "sem_entrega": "19"
}, {
    "id": "104",
    "cliente": "sadfsadf",
    "metodo_pago": "Contado",
    "hora_entrega": "12:32:00",
    "fecha_entrega": "2018-06-18",
    "sem_entrega": "25"
}]
    
answered by 20.06.2018 в 20:02