为 foreach() 和 index.php 提供的无效参数不显示任何内容

Invalid argument supplied for foreach() and index.php shows nothing

我尝试使用 PHP、HTML、CSS 编写网站程序,但我是初学者。起初,我只使用 CSS 和 HTML 对我的网站进行编程,并且效果很好。但是,当我尝试使用 PHP、HTML 对同一个网站进行编程时,我遇到了几个问题。 CSS 使用 MVC 模型。 When I execute my website this shows nothing: 另外,我还有这个问题:

Invalid argument supplied for foreach().

型号代码:

<?php

function muestra($DB)
{
    $categorias = NULL;
    $sql = 'SELECT id,`nombre`,`imagen` FROM categoria';
    $stmt = $DB->prepare($sql);
    $stmt->execute();
    $categorias = $stmt->fetchAll();
    return $categorias;
}
?>

控制器代码:

<?php
    require_once(__DIR__.'/../model/conexioBD.php');
    require_once(__DIR__ . '/../model/modelo_categorias.php');

    $DB = conexio();
    //$DB = conecta();
    $categorias = muestra($DB);

    include(__DIR__.'/../views/vista_categorias.php');

?>

查看代码:

    <!DOCTYPE html>
    
    <html lang="es">
    <head>
        <meta charset="UTF-8">
        <title>Categorias</title>
        <link rel="stylesheet" type="text/css" href="css/categorias.css">
    </head>
    <body>
    <div class="navigation">
        <ul>
            <li><a href="#">Principal</a></li>
            <li><a href="#">Categorias</a></li>
            <li><a href="#">Productos</a></li>
            <li><a href="#">Carrito</a></li>
            <li><a href="#">Perfil</a></li>
            <li><a href="#">Contacto</a></li>
    
        </ul>
    </div>
    
    <div class="paginaCategorias">
    
        <div class="grid-contenidor">
            <?php foreach ($categorias as $categoria) :?>
                <article class="categorias-llista" id=<?php echo $categoria['id'];?>>
                    <a class="categorias-titulo" href="#"><?php echo $categoria['nombre'];?></a>
                    <div class="categorias-imagen">
                        <a href="#">
                            <img width="300" height="169" alt="categoria" src= <?php echo $categoria['imagen']; ?>/></a>
                    </div>
                </article>
                <?php endforeach; ?>
        </div>
    </div>
    </body>
    </html>

index.php代码:

<?php


    if(!isset($_GET['accio'])){
        error_log("error");
    }else{
        $accio = $_GET['accio'];
        switch ($accio)
        {

            default:
                include __DIR__.'/controllers/control_categorias.php';
                break;
        }
    }

?>

您需要将 ?accio=val 添加到您的 URL。

在您的索引文件中,查看 if/else 块。它正在检查 $_GET 中的内容,如果未设置 accio,则不会显示您的页面。我不知道那个参数是干什么用的,而且它似乎没有在您的代码的其他地方使用。

<?php
    if(!isset($_GET['accio'])){
        error_log("error");
    }else{
        $accio = $_GET['accio'];
        switch ($accio)
        {
            default:
                include __DIR__.'/controllers/control_categorias.php';
                break;
        }
    }
?>

由于 $accio 似乎没有被使用,您可能可以将其用作索引。

<?php
    include __DIR__.'/controllers/control_categorias.php';
?>