Show Json in php with codeigniter

0

How can I access calidades to display them in my table? my exit shows the following

{
"separaciones": [
    {
        "id": "92",
        "idcuenta": "1",
        "idusuario": "1",
        "identrada": "185",
        "tcajas": "350",
        "restante": "350",
        "tpeso": "0",
        "dcajas": "0",
        "dpeso": "0",
        "ctotal": "63",
        "status": "1",
        "hora": "20:04:14",
        "fecha": "2018-04-21",
        "calidades": [
            {
                "peso": "100",
                "cajas": "50",
                "precio": "14",
                "nombre_calidad": "CUARTA"
            },
            {
                "peso": "100",
                "cajas": "50",
                "precio": "15",
                "nombre_calidad": "CALIBRE 12?S"
            },

I am currently using the following:

<table class="table table-striped">
                         <?php foreach ($separaciones as $value): ?>
                            <thead>
                                  <tr>
                                    <th>Lote #<?php echo $value->id; ?></th>
                                  </tr>    
                                  <tr>
                                    <th>Calidad</th>
                                    <th>Cajas</th>
                                    <th>Peso</th>
                                    <th>Precio</th>
                                  </tr>
                            </thead>
                        <tbody>
                            <?php foreach ($value->calidades as $key): ?>
                              <tr>
                                <td><?php echo $key->nombre_calidad; ?></td>
                                <td><?php echo $key->cajas; ?></td>
                                <td><?php echo $key->peso; ?></td>
                                <td><?php echo $key->precio; ?></td>
                              </tr>
                            <?php endforeach; ?>
                        </tbody>
                    <?php endforeach; ?>
                      </table>

Codeigniter errors:

  

Trying to get property of non-object

     

Invalid argument supplied for foreach ()

Someone to tell me how I show this information

    
asked by DoubleM 22.04.2018 в 04:14
source

1 answer

0

Suppose that your json is in a variable called $datos .

You should convert it to a valid object by using json_decode , and then access its properties.

I have allowed myself to write a much clearer code, concatenating a single variable that is printed at the end. You will be able to appreciate the difference ...

/*Variable con los datos del JSON*/
$datos='{
    "separaciones": [{
        "id": "92",
        "idcuenta": "1",
        "idusuario": "1",
        "identrada": "185",
        "tcajas": "350",
        "restante": "350",
        "tpeso": "0",
        "dcajas": "0",
        "dpeso": "0",
        "ctotal": "63",
        "status": "1",
        "hora": "20:04:14",
        "fecha": "2018-04-21",
        "calidades": [{
                "peso": "100",
                "cajas": "50",
                "precio": "14",
                "nombre_calidad": "CUARTA"
            },
            {
                "peso": "100",
                "cajas": "50",
                "precio": "15",
                "nombre_calidad": "CALIBRE 12S"
            }
        ]
    }]
}';

/*La convertimos a un JSON válido*/
$json=json_decode($datos);

/*Obtenemos el valor de la propiedad separaciones*/
$arrSeparaciones=$json->separaciones;

/*Variable que irá recogiendo los datos*/
$tablaHTML='<table class="table table-striped">';
foreach ($arrSeparaciones as $value) {
    $tablaHTML.="
                 <thead>
                    <tr>
                        <th>Lote # $value->id</th>
                    </tr>
                    <tr>
                        <th>Calidad</th>
                        <th>Cajas</th>
                        <th>Peso</th>
                        <th>Precio</th>
                    </tr>
                </thead>
                <tbody>";
    foreach ($value->calidades as $key) {
        $tablaHTML.="
                    <tr>
                        <td>$key->nombre_calidad</td>
                        <td>$key->cajas</td>
                        <td>$key->peso</td>
                        <td>$key->precio</td>
                    </tr>";
    }
    $tablaHTML.="
                </tbody>";
    }
$tablaHTML.="</table>";

/*Imprimimos*/
echo $tablaHTML;

Exit

<table class="table table-striped">
  <thead>
    <tr>
      <th>Lote # 92</th>
    </tr>
    <tr>
      <th>Calidad</th>
      <th>Cajas</th>
      <th>Peso</th>
      <th>Precio</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>CUARTA</td>
      <td>50</td>
      <td>100</td>
      <td>14</td>
    </tr>
    <tr>
      <td>CALIBRE 12S</td>
      <td>50</td>
      <td>100</td>
      <td>15</td>
    </tr>
  </tbody>
</table>
    
answered by 22.04.2018 в 05:30