Error Undefined offset in PHP (Create table with stored procedure result)

1

I need to print the sales of the operators of each day, we do not take into account the holidays on weekends. I get the following error

  

Notice (8): Undefined offset: 1 [APP / View / Reports / getventas_asistencia.ctp, line 63]

     

Notice (8): Undefined offset: 6 [APP / View / Reports / getventas_asistencia.ctp, line 63]

     

Notice (8): Undefined offset: 7 [APP / View / Reports / getventas_asistencia.ctp, line 63]

     

Notice (8): Undefined offset: 13 [APP / View / Reports / getventas_asistencia.ctp, line 63]

     

Notice (8): Undefined offset: 14 [APP / View / Reports / getventas_asistencia.ctp, line 63]

     

Notice (8): Undefined offset: 16 [APP / View / Reports / getventas_asistencia.ctp, line 63]

My code is as follows:

<div style="width: 100%; ">
    <legend style="color:grey" ><?php echo __('Resultados');?></legend>
</div>

<div class="table table-bordered" style="width=100%;"  >
    <table width="100%" border="1" style="text-align:center;">
        <?php
         // $i=0;
         // $j=0;
        $columnas = count($AsistenciaVentas[0]);
        //echo $columnas;

        $filas = count($AsistenciaVentas);  
        //echo "<br>".$filas."<br>";

        //Añadimos los titulos          
       for($i=0;$i<count($AsistenciaVentas[0]);$i=$i+1){
          next($AsistenciaVentas[0]);
          echo "<th><b>".key($AsistenciaVentas[0])."</b></th>";
        // next($AsistenciaVentas[0]);
        }    
        for($i=0;$i<$filas;$i++){
        echo "<tbody><tr>";
        for($j=1;$j<$columnas;$j++){
        echo "<td>".$AsistenciaVentas[$i][$j]."</td>";
        }
        echo "</tr></tbody> ";
        }
        ?>  
    </table>
</div>

The result of the array looks like this:

Array
(
    [0] => Array
        (
            [usuario_id] => 1055
            [nombre_usuario] => Alejandro Espinoza Salinas
            [2] => 2
            [3] => 2
            [4] => 2
            [5] => 3
            [8] => 3
            [9] => 
            [10] => 2
            [11] => 
            [12] => 
            [15] => 
            [17] => 
            [18] => 
            [19] => 
            [22] => 
            [23] => 
            [24] => 
            [25] => 
            [26] => 
            [29] => 
            [30] => 
            [31] => 
        )

    [1] => Array
        (
            [usuario_id] => 1248
            [nombre_usuario] => Alex Rodrigo Azocar Bernal
            [2] => 
            [3] => 
            [4] => 
            [5] => 
            [8] => 
            [9] => 
            [10] => 
            [11] => 
            [12] => 
            [15] => 0
            [17] => 
            [18] => 
            [19] => 
            [22] => 
            [23] => 
            [24] => 
            [25] => 
            [26] => 
            [29] => 
            [30] => 
            [31] => 
        )

    [2] => Array
        (
            [usuario_id] => 377
            [nombre_usuario] => Alicia Escobar Cuadra
            [2] => 5
            [3] => 
            [4] => 9
            [5] => 5
            [8] => 7
            [9] => 
            [10] => 6
            [11] => 4
            [12] => 4
            [15] => 2
            [17] => 
            [18] => 
            [19] => 
            [22] => 
            [23] => 
            [24] => 
            [25] => 
            [26] => 
            [29] => 
            [30] => 
            [31] => 
        )

    [3] => Array
        (
            [usuario_id] => 1243
            [nombre_usuario] => Barbara Andrea Soto Ramos
            [2] => 
            [3] => 
            [4] => 
            [5] => 
            [8] => 
            [9] => 
            [10] => 
            [11] => 3
            [12] => 
            [15] => 1
            [17] => 
            [18] => 
            [19] => 
            [22] => 
            [23] => 
            [24] => 
            [25] => 
            [26] => 
            [29] => 
            [30] => 
            [31] => 
        )

    [4] => Array
        (
            [usuario_id] => 1200
            [nombre_usuario] => Claudio Esteban Nunez Gajardo
            [2] => 
            [3] => 
            [4] => 
            [5] => 3
            [8] => 6
            [9] => 6
            [10] => 6
            [11] => 
            [12] => 3
            [15] => 
            [17] => 
            [18] => 
            [19] => 
            [22] => 
            [23] => 
            [24] => 
            [25] => 
            [26] => 
            [29] => 
            [30] => 
            [31] => 
        )

I do not know what to do to delete the messages and to print the user names and id

Thank you very much Jorge, I tried in the following way:

if (array_key_exists(0, $AsistenciaVentas)) {for($i=0;$i<count($AsistenciaVentas[0]);$i=$i+1){
      if(!empty($AsistenciaVentas[0]))
      {
        next($AsistenciaVentas[0]);
        echo "<th><b>".key($AsistenciaVentas[0])."</b></th>";
        // next($AsistenciaVentas[0]);
      }
    }

and it did not work for me.

Result of what I want to show

what my pag shows including the offset

    
asked by Alexandra Teran 18.01.2018 в 21:42
source

2 answers

1
  

EDITED: GENERATING AN AUTOMATIC TABLE

With this code you can browse the array and generate an automatic table with its headers.

Keep in mind that I did not put all your array for brevity reasons, but if you implement it with the full array it will generate the table as you present it in the image.

VIEW DEMO IN REXTESTER

<?php
/* PHP: tabla automática a partir de un array asociativo - https://es.stackoverflow.com/q/132129/29967  */
    $arrUsuarios=array(
                    array("usuario_id" => 1055, "nombre_usuario" => "Alejandro Espinoza Salinas", "2" => "2", "3" => "2", "4" => "2" ),    
                    array("usuario_id" => 1248, "nombre_usuario" => "Alex Rodrigo Azocar Bernal", "2" => "" , "3" => "" , "4" => "" ) 
                    );

    $strHTML= "<table>";
    /*Encabezados de la tabla*/
    foreach (array_keys($arrUsuarios[0]) as $th) {
        $strHTML .= '<th>' . $th . '</th>';
    }
    /*Filas y celdas*/
    foreach($arrUsuarios as $fila) {
        $strHTML.= "<tr>";
        foreach($fila as $celda){
            /*Si la celda está vacía, cambiamos su valor por NULL*/    
            $td=($celda) ? $celda : "NULL";
            $strHTML.= "<td>" . $td . "</td>";
        }
        $strHTML.= "</tr>";
    }
    $strHTML.="</table>";
    echo $strHTML;
?>

Result:

Here is an example of a limited result, I did not reproduce the whole array for brevity reasons:

table, th, td {
   border: 1px solid black;
   border-collapse: collapse;
   text-align:center;
   padding:5px 5px 5px 5px;
}
<table>
  <th>usuario_id</th>
  <th>nombre_usuario</th>
  <th>2</th>
  <th>3</th>
  <th>4</th>
  <tr>
    <td>1055</td>
    <td>Alejandro Espinoza Salinas</td>
    <td>2</td>
    <td>2</td>
    <td>2</td>
  </tr>
  <tr>
    <td>1248</td>
    <td>Alex Rodrigo Azocar Bernal</td>
    <td>NULL</td>
    <td>NULL</td>
    <td>NULL</td>
  </tr>
</table>
    
answered by 18.01.2018 / 22:32
source
0

Greetings

why not valid using a if (! empty ($ value))? for example something in your code

for($i=0;$i<count($AsistenciaVentas[0]);$i=$i+1){
      if(!empty($AsistenciaVentas[0]))
      {
        next($AsistenciaVentas[0]);
        echo "<th><b>".key($AsistenciaVentas[0])."</b></th>";
        // next($AsistenciaVentas[0]);
      }
    }  
    
answered by 18.01.2018 в 22:18