Loop duplicating entire table

Asked

Viewed 120 times

4

I’m not getting to make a loop in the table, it’s making loop to every bank search.

    $dbc = mysqli_connect('senha_adm');
    $query = "select carro, barco, aviao, moto, triciclo, velotrou, dataCadastro from agencia";
    $result = mysqli_query($dbc, $query);
    while ($row = mysqli_fetch_array($result)) {
    ?>      

            <table id="tabela">
                    <tr>
                        <td>Carros</td>
                        <td>Barcos</td>
                        <td>Avioes</td>
                        <td>Motos</td>
                        <td>triciclos</td>
                        <td>Velotroes</td>
                    </tr>

                    <tr>  
                        <td><?php echo $row['carro']; ?></td>
                        <td><?php echo $row['barco']; ?></td>
                        <td><?php echo $row['aviao']; ?></td>
                        <td><?php echo $row['moto']; ?></td>
                        <td><?php echo $row['triciclo']; ?></td>
                        <td><?php echo $row['velotrou']; ?></td>

                    </tr>

            <?php       

           }


    echo " </table>";
  • 3

    First try to elaborate the question better, instead of just pasting the code. And as a suggestion, use the title to briefly inform the problem. Otherwise it is difficult to help.

  • 1

    Generally the foreach is placed in the <tr>.

  • Better you review your code this while with a whole table, with ID still, inside and with a lot of html inside is not good.

1 answer

12


You have to remove the table and header from the loop:

<?php
   ...

   $dbc = mysqli_connect('senha_adm');
   $query = "select carro, barco, aviao, moto, triciclo, velotrou, dataCadastro from agencia";
   $result = mysqli_query($dbc, $query);
?>
   <table id="tabela">
      <tr>
         <th>Carros</th>
         <th>Barcos</th>
         <th>Avioes</th>
         <th>Motos</th>
         <th>triciclos</th>
         <th>Velotroes</th>
      </tr>
<?php while ($row = mysqli_fetch_array($result)) { ?>
      <tr>  
         <td><?php echo $row['carro']; ?></td>
         <td><?php echo $row['barco']; ?></td>
         <td><?php echo $row['aviao']; ?></td>
         <td><?php echo $row['moto']; ?></td>
         <td><?php echo $row['triciclo']; ?></td>
         <td><?php echo $row['velotrou']; ?></td>
      </tr>
<?php } ?>
   </table>
   ...
  1. You open the table, show the headers once.

  2. Within the while is that creates several tr, as you read the data.

  3. Finally, out of the while, closes the table.

Just to top it off, I switched out the td of the header by th, which is the most appropriate.

Browser other questions tagged

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