How to include all my pages within one?

Asked

Viewed 52 times

-4

I am developing a system where I want the "menu" page to include all my categories, which already has a layout of its own on your page, for this I am trying to use the while loop this way:

<section class="pages">
    
    <?php 
        $stmt = $conn->prepare("SELECT * FROM dados_categoria WHERE ativo='S'");
        $stmt->execute();
        $resultado = $stmt->get_result();
            while($rows = $resultado->fetch_assoc()){
    
        echo '<div class="page-card">
                <h1 class="title">'.$rows['nome_categoria'].'</h1>
                '.include('pages/'.$rows['categoria'].'.php').'
            </div>';
    
        }
    ?>
    </section>

Unfortunately this way the site does not load the pages with the "include" and is presenting the following:

Warning: include(pages/pizza.php </div>): failed to open stream: No such file or directory in C:\xampp\htdocs\foodOrderSystem\pages\cardapio.php on line 11

Warning: include(): Failed opening 'pages/pizza.php </div>' for inclusion (include_path='C:\xampp\php\PEAR') in C:\xampp\htdocs\foodOrderSystem\pages\cardapio.php on line 11

2 answers

0

The friend’s response solved my problem, however as I commented include was generating conflict by ending my while loop. After analyzing I found that the problem was two variables equal in my menu and include page with different values.

Code before:

<?php 
$stmt = $conn->prepare("SELECT * FROM dados_categoria WHERE ativo='S'");
$stmt->execute();
$resultado = $stmt->get_result();
while($rows = $resultado->fetch_assoc()){
    echo '<div class="page-card">';
    echo '<h1 class="title">'.$rows['nome_categoria'].'</h1>';
    include('pages/'.$rows['categoria'].'.php');
    echo '</div>';
}
?>

Code after:

<?php 
$stmt = $conn->prepare("SELECT * FROM dados_categoria WHERE ativo='S'");
$stmt->execute();
$resultante = $stmt->get_result();
while($cat = $resultante->fetch_assoc()){
    echo '<div class="page-card">';
    echo '<h1 class="title">'.$cat['nome_categoria'].'</h1>';
    include('pages/'.$cat['categoria'].'.php');
    echo '</div>';
}
?>

0


I may be wrong but I think your mistake is here:

echo '<div class="page-card">
   <h1 class="title">'.$rows['nome_categoria'].'</h1>
   '.include('pages/'.$rows['categoria'].'.php').'
</div>';

Try to switch to:

echo '<div class="page-card">';
   echo '<h1 class="title">'.$rows['nome_categoria'].'</h1>';
   include('pages/'.$rows['categoria'].'.php');
echo '</div>';

Make sure that files with the same category name appear inside the folder.

  • It worked, so not the most error, however it was only calling 1 time, when it was to call 3 Divs with 3 different includes, as if the loop was not working, can imagine what is the cause?

  • detail when I take the include from the middle of the code it correctly calls the 3 div’s I checked and the directory and file name are correct, because I use this include method on another page of that same project based on database information, however it is a single include, ie just a page without having a loop calling several pages in one.

  • After taking a look at the code I soon realized the reason to run include only once... In my "pizza" page for example, there is a query to get the data from the database, with this when I call the include it includes this data from the database overwriting my previous query of the cardapio page, because of that the loop ends there...

  • In the case of include(), among other native PHP functions it is not possible to concatenate with them, as was in your question. About breaking your loop I was going to answer before but it didn’t, you managed to find the problem. I usually name queries with unique variables, something like this $sel_categoria $sel_cardapio

Browser other questions tagged

You are not signed in. Login or sign up in order to post.