Error working with foreach

1

Good afternoon, when trying to bring images from the database the page sends me the following errors:

  

Notice: Undefined variable: photos in C: \ xampp \ htdocs \ projectCarrito2 \ views \ index.view.php on line 72   Warning: Invalid argument supplied for foreach () in   C: \ xampp \ htdocs \ projectCarrito2 \ views \ index.view.php on line 72

In the part of the index.php I have the following code:

    <?php


session_start();
if(empty($_SESSION['idUsuario']) && empty($_SESSION['Tipo'])){
    include 'conexion.php';
    include 'views/index.view.php';


    $fotos_por_pagina = 16;

    $pagina_actual = (isset($_GET['p']) ? (int)$_GET['p'] : 1);
    $inicio = ($pagina_actual > 1) ? $pagina_actual * $fotos_por_pagina - $fotos_por_pagina : 0;

    $conexion = conexion('proyecto', 'root', '');

    if (!$conexion) {
        die();
    }

    $statement = $conexion->prepare("
        SELECT SQL_CALC_FOUND_ROWS * FROM timagen LIMIT $inicio, $fotos_por_pagina
    ");

    $statement->execute();
    $fotos = $statement->fetchAll();

    if (!$fotos) {
        header('Location: index.php');
    }

    $statement = $conexion->prepare("SELECT FOUND_ROWS() as total_filas");
    $statement->execute();
    $total_post = $statement->fetch()['total_filas'];

    $total_paginas = ceil($total_post / $fotos_por_pagina);
}else{

header("Location: formularios/form_cliente.php");
}


?>

And in the part of the view that is index.view.php I have the following:

<article class="imagenes col-9 col-m-8">
    <?php foreach($fotos as $foto):?>
        <div class="col-4 col-m-6">
        <a href="fotos.php?id=<?php echo $foto['idImagen']; ?>">
            <img src="images/bd<?php echo $foto['Imagen'] ?>" alt="<?php echo $foto['Descripcion'] ?>">
        </a>
        </div>
    <?php endforeach;?>
</article>

I hope you can help me. Thanks.

    
asked by Guillermo Ricardo Spindola Bri 02.04.2017 в 03:07
source

2 answers

0

The only thing I did was put the include of the file, at the end of my code and it was solved, instead of the previous code I changed it for the following:

    <?php


session_start();
if(empty($_SESSION['idUsuario']) && empty($_SESSION['Tipo'])){
    include 'conexion.php';


    $fotos_por_pagina = 16;

    $pagina_actual = (isset($_GET['p']) ? (int)$_GET['p'] : 1);
    $inicio = ($pagina_actual > 1) ? $pagina_actual * $fotos_por_pagina - $fotos_por_pagina : 0;

    $conexion = conexion('proyecto', 'root', '');

    if (!$conexion) {
        die();
    }

    $statement = $conexion->prepare("
        SELECT SQL_CALC_FOUND_ROWS * FROM timagen LIMIT $inicio, $fotos_por_pagina
    ");

    $statement->execute();
    $fotos = $statement->fetchAll();

    if (!$fotos) {
        header('Location: index.php');
    }

    $statement = $conexion->prepare("SELECT FOUND_ROWS() as total_filas");
    $statement->execute();
    $total_post = $statement->fetch()['total_filas'];

    $total_paginas = ceil($total_post / $fotos_por_pagina);

include 'views/index.view.php';
}else{

header("Location: formularios/form_cliente.php");
}


?>
    
answered by 05.04.2017 / 17:18
source
0

Look in your HTML code at the sentence foreach you're wrong; for a single instruction:

foreach (expresión_array as $valor)
   sentencia

or

foreach (expresión_array as $clave => $valor)
   sentencia

for several instructions

foreach (expresión_array as $valor) {
   sentencia1
   sentencia2
   sentencia3
   ...
   sentenciaN
}

or

foreach (expresión_array as $clave => $valor) {
   sentencia1
   sentencia2
   sentencia3
   ...
   sentenciaN
}

For cases <?php echo CADENAoVALOR ?> and similar I suggest <?= CADENAoVALOR ?> ; it's a single statement that you use echo .

foreach in PHP

    
answered by 02.04.2017 в 03:25