How to list values of multiple queries in a single table?

Asked

Viewed 61 times

0

I have a table with the columns attendant, total/day and total/month.

I listed on tr/td the names of people through the first MYSQL query. I need through the second query to sum up the records of these people and print on the next td. The problem is that the queries are from different tables and do not know how to print inside a tr through the While query values from different MYSQL tables.

$nome = $_SESSION['usuarioNome'];
$nomes = mysql_query("SELECT nome FROM usuarios"); //primeira consulta
$consulta = mysql_query("SELECT COUNT(*) FROM atendimento WHERE nome = '$nome' AND DAY(data)=DAY(NOW())"); // segunda consulta

Follows HTML:

            <table style="text-align: center;width: 100%;" class="table table-striped table-bordered" id="table_id">
            <thead>
                <tr style="background-color: #007bff;">

                  <td style="width: 5%;color: white;font-size: 16px;">Atendente:</td>
                  <td style="width: 4%;color: white;font-size: 16px;">Total/Dia:</td>
                  <td style="width: 4%;color: white;font-size: 16px;">Total/Mês:</td>

                </tr>
            </thead>
              <tbody>

                <?php  while($prod = mysql_fetch_array($nomes)) { ?>

                  <tr data-id="<?php echo $prod['id'] ?>">
                    <td style="width: 5%;color: black;font-size: 16px;"><?php echo $prod['nome'] ?></td>
                    <td style="width: 5%;color: black;font-size: 16px;"><?php  ?></td>
                    <td style="width: 5%;color: black;font-size: 16px;"><?php  ?></td>
                  </tr>

                <?php } ?>

              </tbody>
        </table>

inserir a descrição da imagem aqui

inserir a descrição da imagem aqui

1 answer

0


And if we unite the querys it would not be better ?

Would something like this:

SELECT 
    nome,
    (SELECT COUNT(*) FROM ATENDIMENTO WHERE NOME = '$nome' AND DAY(data)=DAY(NOW())) AS TOTAL_DIA
FROM USUARIOS

This way you can iterate normally:

<?php  while($prod = mysql_fetch_array($nomes)) { ?>
  <tr data-id="<?php echo $prod['id'] ?>">
    <td style="width: 5%;color: black;font-size: 16px;"><?php echo $prod['nome'] ?></td>
    <td style="width: 5%;color: black;font-size: 16px;"><?php echo $prod['TOTAL_DIA'] ?></td>
    <td style="width: 5%;color: black;font-size: 16px;"><?php  ?></td>
  </tr>
<?php } ?>
  • It worked! Thank you very much

  • @Well that it worked. The best way to thank you is to put the answer as correct. I am grateful to help!

Browser other questions tagged

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