Insert Database items into Tbody of an HTML table using a repeat loop

Asked

Viewed 98 times

1

Good morning, I’m doing the product listing of a database on a Html + PHP page, with the code I developed, using a While that runs until the database table still has lines, I can already get the database information and list on the page, jump line to each information and when you change item step a dash on the page and also made the pagination.

This listing correctly, however for aesthetic reasons I created a table and instead of listing the items in the page used the "echo" would like to add directly in the table. I’m not familiar with Javascript so I wonder if there is a way to do this with php and html.

Thanks in advance!

Code:

<!-- Tabela criada para tentar inserir os dados -->

<body>
  <h1>Lista de Produtos</h1>
  <table class="table table-hover" id="tabres">
    <thead>
      <tr>
        <td>Código</td>
        <td>Descrição</td>
        <td>Valor</td>
      </tr>
    </thead>
    <tbody id="lsprodutos">
    </tbody>
  </table>
  <?php
$pagina_atual = filter_input(INPUT_GET,'pagina',FILTER_SANITIZE_NUMBER_INT);
$pagina = (!empty($pagina_atual)) ? $pagina_atual : 1; 

$qnt_result_pg = 3;

$inicio = ($qnt_result_pg * $pagina) - $qnt_result_pg;

$sql="select codigo,descricao,valor from loja.produto order by codigo LIMIT $inicio, $qnt_result_pg";
$resultado_produtos = mysqli_query($conn, $sql);

#Repetiçao que uso para inserir os dados na pagina
#Aqui gostaria de subistituir o echo por algo que insira na tabela que criei la em cima

while($row_produto = mysqli_fetch_assoc($resultado_produtos)){
    echo "Codigo " . $row_produto['codigo'] . "<br>";
    echo "Descricao ". $row_produto['descricao'] . "<br>";
    echo "Valor " . $row_produto['valor'] . "<br><hr>";
}
$result_pg = "SELECT COUNT(codigo) AS num_result FROM produto";
$resultado_pg = mysqli_query($conn,$result_pg);
$row_pg = mysqli_fetch_assoc($resultado_pg);

$quantidade_pg = ceil($row_pg['num_result'] / $qnt_result_pg);

$max_links = 2;
echo "<a href='listaproduto.php?pagina=1'>Primeira"
?>
</body>

1 answer

2


Good morning. It is possible to place the echo within the tbody table. For that, put the code PHP that makes the query in the shop.product table at the top of the page. Soon, we will merge the excerpt from the while with the table. Inside <tbody id="lsprodutos"> put the code below. It should look like this:

<?php while($row_produto = mysqli_fetch_assoc($resultado_produtos)){ ?>
      <tr>
          <td><?php echo $row_produto['codigo']; ?></td>
          <td><?php echo $row_produto['descricao']; ?></td>
          <td><?php echo $row_produto['valor']; ?></td>
      </tr>
<?php } ?>

If you prefer, it is also possible to do as it is answered in this question https://stackoverflow.com/questions/17902483/show-values-from-a-mysql-database-table-inside-a-html-table-on-a-webpage

  • Leonardo most grateful for your attention, your code has solved my problem.

Browser other questions tagged

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