Problems with a cycle that gives the error "Notice: Undefined variable"

1

I'm doing a dynamic gallery with PHP and MySQL, but when it comes to making a cycle it gives me these two errors

  

Notice: Undefined variable: photos in   C: \ xammp2 \ htdocs \ course_PHP \ practices \ gallery_dynamics \ views \ index.view.php   on line 18

     

Warning: Invalid argument supplied for foreach () in   C: \ xammp2 \ htdocs \ course_PHP \ practices \ gallery_dynamics \ views \ index.view.php   on line 18 "

Here is the code of the cycle:

<?php foreach($fotos as $foto): ?>
           <div class="thumb">
             <a href="foto.php?id=<?php echo $foto['id']; ?>">
               <img src="fotos/<?php echo $foto['imagen'] ?>" alt="">
             </a>
           </div>
         <?php endforeach;?>

I already declared the variable in the document index.php, and I had always done the cycles in this way

    
asked by Enmanuel De Los Santos Cruz 09.02.2017 в 23:04
source

1 answer

1

The message indicates the problem:

1) the variable $ photos is not defined:

  

Notice: Undefined variable: photos in

2) since $ photos is not defined on foreach () send this message:

  

Warning: Invalid argument supplied for foreach () in

This is because foreach () requires an array and an object.

Make sure you define $ photos and that it is an array.

You can validate before performing the foreach () that the values you need are correct through :

if (is_array($fotos) || is_object($foto)){
 //Realiza foreach()
}
    
answered by 10.02.2017 в 00:46