Tie duplicating whole table

4

I can not make a loop in the table, it is looping every search in the database.

    $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>";
    
asked by anonymous 01.09.2016 / 16:46

1 answer

12

You have to remove the table and the header of 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>
   ...
  • You open the table, show the headings once.

  • Within while is that it creates multiple tr as it reads the data.

  • Finally, out of while , close the table.

  • Just to complete, I changed the td of the header by th , which is the most appropriate.

        
    01.09.2016 / 16:54