how to show the result of an sql in different <Section> files?

Asked

Viewed 42 times

0

How do I make projects belonging to the same department stay in the same department?

    $sql = "SELECT d.nome, p.titulo FROM projeto p, departamento d WHERE p.dpto_id=d.id ORDER BY d.nome, p.titulo";

    $dpto=null;
    foreach ($rows as $reg){

    $departamento=$reg["nome"];
        $titulo=$reg["titulo"];

        if($departamento!==$dpto){
    echo  "<section>";
        echo "<h1>". $departamento . "</h1>";
        echo "<p>". $titulo ."</p>";
      } elseif ($departamento===$dpto) {
        echo "<p>". $titulo ."</p>";
      }

        $dpto=$departamento;
     }
     
    //Resultdo final deveria ser assim
    <section>
      <h1>nome do departamento</h1>
      <p>lista de projetos</p> 
    </section>

    <section>
      <h1>outro departamento</h1>
      <p>sua de projetos</p>
    </section>

I tried like that, but I can’t close the Section tag.

2 answers

1


Do so:

$dpto=null;
foreach ($rows as $reg){

    $departamento=$reg["nome"];
    $titulo=$reg["titulo"];

    if($departamento!==$dpto){
echo  "<section>";
    echo "<h1>". $departamento . "</h1>";
    echo "<p>". $titulo ."</p>";
echo  "</section>";
  } elseif ($departamento===$dpto) {
echo  "<section>";
    echo "<p>". $titulo ."</p>";
echo  "</section>";
  }
    $dpto=$departamento;
 }
  • It did not work. FV and DS projects belong to Dpto1, but remained outside the section. The same happened with the ABGO and GT projects of Dpto2. Section H1 dto1 H1 XTZ Section FV DS Section H1 dto2 H1 RT Section ABGO GT Section H1 dto3 H1 YWZ Section Section Dto4 H1

  • I updated the program, try this new way

0

You’ll have to have a loop nestled inside another, something like:

foreach($departamentos as $departamento):
 $produtos = SQL ///SELECT * FROM PRODUTOS WHERE idDepartamento = $departamento->id // sua consulta aqui.
 foreach($produtos as $produto):
  echo $produto->nome."<br>";
 endforeach;
endforeach;

This way you can select all products in each department interaction.

Browser other questions tagged

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