Returns + column values match with PHP and Mysql

Asked

Viewed 36 times

0

Good afternoon,

I have a table that receives data from a mysql that works correctly. But it does not return the corresponding value of a column all within one , Example:

How does it look:

    PROFESSOR     |   ALUNOS

    CARLOS        |  LUIZ
    CARLOS        |  CAIO
    CARLOS        |  matheus
    CARLOS        |  ROBERTO


    JOSE         |  RODRIGO
    JOSE         |  LEANDRO
    JOSE         |  WAGNER
    JOSE         |  LUIZ


    ELDER         |  LUIZ
    ELDER         |  EDUARDO
    ELDER         |  VICTOR
    ELDER         |  RODRIGO

How I’d like you to stay

 PROFESSOR        |   ALUNOS


    CARLOS        |  LUIZ,caio,matheus,roberto


    JOSE          |  RODRIGO,LEANDRO,WAGNER,LUIZ


    ELDER         |  LUIZ,EDUARDO,VICTOR,RODRIGO

My code

<table class="table table-striped table-bordered table-hover table-filters">
        <thead>
        <tr>
            <th>Professor</th>
            <th>Aluno</th>
        </tr>
        </thead>
        <tbody>'
               foreach($analises as $key => $cada){

                <tr>
                    <td><?php echo $cada->['professor']; ?></td>
                    <td><?php echo $cada->['aluno']; ?></td>                 
                </tr>;

            }
        </tbody>
    </table>
  • 1

    post the query being made.

  • I’ll tell you right away that you need to know the function GROUP_CONCAT to find the answer you want. But as Lucas has already said, you haven’t posted the most important of your question, your query.

1 answer

1

In my understanding you should have two queries to the database. One only of teachers where you make one group by of teachers. When doing the loop to scroll through the records you make another query within the loop searching for the corresponding students to the loop teacher, filling another table Something more or less like this:

<table>
  <?php
foreach ($listaProfessores as $key => $professor) {
  ?>
  <tr>
    <td style="border:solid 1px;border-collapse: collapse;"><?=$professor['professor'];?>:</td>
    <td>
      <table style="border:solid 1px;border-collapse: collapse;">
        <tr>
        <?php
        $query = "SELECT aluno from aulas where professor = '{$professor['professor']}';";
        $resultado = $conexao->query($query);
        $listaAlunos = $resultado->fetchAll();
        foreach ($listaAlunos as $key => $aluno) {
        ?>
          <td>
            <?=$aluno['aluno'].' ';?>
          </td>
        <?php }?>
        </tr>
      </table>
    </td>
  </tr>
  <?php
}?>
</table>

Browser other questions tagged

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