Undefined offset

1

Supposedly the counter ends in "2" but the error tells me that "3" is not defined.

<?php include_once 'includes/templates/header.php'; ?>


 <section class="seccion contenedor">
    <h2>Calendario de Eventos</h2>
            <?php
                    try {
                        require_once('includes/funciones/bd_conexion.php');
                        $sql = "SELECT 'evento_id', 'nombre_evento', 'fecha_evento', 'hora_evento', 'cat_evento', 'nombre_invitado', 'apellido_invitado' ";
          $sql .= "FROM 'eventos' ";
          $sql .= "INNER JOIN 'categoria_evento' ";
          $sql .= "ON eventos.id_cat_evento = categoria_evento.id_categoria ";
          $sql .= "INNER JOIN 'invitados' ";
          $sql .= "ON eventos.id_inv = invitados.invitado_id ";
          $sql .= "ORDER BY 'evento_id' ";
                        $resultado = $conn->query($sql);

                    } catch (Exception $e) {
                        $error = $e->getMessage();
                    }
             ?>
     <div class="calendario">

             <?php while($eventos = $resultado->fetch_all(MYSQLI_ASSOC) ) { ?>

       <?php $dias = array(); ?>
       <?php foreach($eventos as $evento) {
         $dias[] = $evento['fecha_evento'];
       } ?>

       <?php $dias = array_values(array_unique($dias)) ?>

       <?php $contador = 0; ?>
       <?php foreach($eventos as $evento): ?>

         <?php $dia_actual = $evento['fecha_evento']; ?>
         <?php if($dia_actual == $dias[$contador]): ?>
                <h3>
                    <i class="fa fa-calendar" aria-hidden="true"></i>
                    <?php echo $evento['fecha_evento']; ?>
                </h3>
                <?php $contador++; ?>
         <?php endif; ?>

         <div class="dia">
           <p class="titulo"><?php echo utf8_encode($evento['nombre_evento']); ?></p>
           <p class="hora"><i class="fa fa-clock-o" aria-hidden="true"></i><?php echo $evento['fecha_evento'] . " " . $evento['hora_evento'] . "Hs"; ?></p>
           <p>
                <?php $categoria_evento = $evento['cat_evento']; ?>
                <?php
                    switch ($categoria_evento) {
                      case 'Talleres':
                        echo '<i class="fa fa-code" aria-hidden="true"></i> Talleres';
                        break;
                      case 'Conferencia':
                        echo '<i class="fa fa-comment" aria-hidden="true"></i> Conferencia';
                        break;
                      case 'Seminario':
                        echo '<i class="fa fa-university" aria-hidden="true"></i> Seminario';
                        break;
                      default:
                        echo "";
                        break;
                    }
                 ?>
           </p>
           <p><i class="fa fa-user" aria-hidden="true"></i>
              <?php echo $evento['nombre_invitado'] . " " . $evento['apellido_invitado']; ?>
           </p>

         </div>
       <?php endforeach; ?>
       </div><!--.calendario-->

             <?php } ?>


 </section>
  

Notice: Undefined offset: 3 in D: \ xampp \ htdocs \ gdlwebcamp \ calendar.php on line 37

    
asked by Maxii Perez 05.10.2017 в 07:55
source

2 answers

0

In if you have $dias[$contador] and you're passing $dias[3] .

What the error indicates is that the array $dias does not have so many fields, it will be an array of three records ( $dias[2] ) and you are trying to access its fourth record ( $dias[3] ) that does not exist.

    
answered by 05.10.2017 в 08:45
0

I guess the error comes from this line:

<?php if($dia_actual == $dias[$contador]): ?>

The error tells you that your array has 3 positions and you are trying to access a fourth. And this comes from a logic error: $ counter gets its value by traversing the array of events, and in fact in your day array you are saving the dates of the events, but with the line:

<?php $dias = array_values(array_unique($dias)) ?>

You are eliminating positions from your array $ dias , as soon as two events occur on the same day. That is, before this line both your array $ dias and your array $ events have the same number of elements, and there is no error, but once filtered $ days , as I told you if two events occur on the same day, you have your error ...

Greetings.

    
answered by 28.10.2017 в 17:43