Multi-table Data Sum Problem using PHP and MYSQL

Asked

Viewed 44 times

1

Guys, I developed a call system, and now I need to do the report of students with the amount of absences and attendance.

I use 1 table(chamadas) to register some information of the call and when you register, the system creates a table(chamada_XXXXXXXXX) where it will have the data of this call, as: the student and the status (presence or lack) of the same.

As the report needs to display the total presence and lack of each student, it has to access each of the tables(chamada_XXXXXXXXX) already created (included in the table chamadas) and add up the final total of presence and absences, and that’s where I’m having trouble.

I will leave a database print for you to understand better and also the code I am trying to perform the query and return the total.

ALL TABLES inserir a descrição da imagem aqui CALL TABLE inserir a descrição da imagem aqui TABLE (chamada_xxxxxxxxx) WITH CALL DATA, STUDENT, PRESENCE AND MISSING inserir a descrição da imagem aqui

STUDENT SEARCH CODE AND ABSENCE AND ATTENDANCE COUNT This code is returning me only from 1 table, that is, it is not adding up when going through a new table (1+1+1+1+1....).

<?php 
$procura_alunos = $site->query("SELECT * FROM alunos WHERE curso = '$token' ORDER BY nome ASC");
while ($aluno = $procura_alunos->fetch(PDO::FETCH_OBJ)) {
    $procura_chamadas = $chamadas->query("SELECT * FROM chamadas WHERE curso = '$token'");
    while ($chamada = $procura_chamadas->fetch(PDO::FETCH_OBJ)) {
        $conta_faltas = $chamadas->query("SELECT * FROM $chamada->tabela WHERE turma = '$aluno->turma' AND alunos = '$aluno->chns' AND status = 'F'");
        $conta_presencas = $chamadas->query("SELECT * FROM $chamada->tabela WHERE turma = '$aluno->turma' AND alunos = '$aluno->chns' AND status = 'C'");
        $faltas = $conta_faltas->rowCount();
        $presencas = $conta_presencas->rowCount();
    }
    ?>
    <tr>
        <td>
            <?php echo $aluno->nome;?>
        </td>
        <td>
            <span style="color: #00ff4e"><strong id="presencas_<?php echo$aluno->id; ?>"><?php echo $presencas;?></strong></span>
        </td>
        <td>
            <span style="color: #ef0c0c"><strong id="faltas_<?php echo$aluno->id; ?>"><?php echo $faltas;?></strong></span>
        </td>
    </tr>
    <?
}
?>
  • Wouldn’t it be better to use JOIN to do this? JOIN

  • you can give me some example of how I would use to consult with JOIN? I never messed with him so I don’t really know how to use him in this case.

  • At this link and at this link has plenty of stuff.

No answers

Browser other questions tagged

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