-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
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?
– Brenofvs
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.
– Brenofvs
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...
– Brenofvs
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
– Gnomo Escalate