1
I have the following table:
<table class="table">
<thead class="thead-dark">
<tr>
<th scope="col">Nome</th>
<th scope="col">Descrição</th>
<th scope="col">Valor</th>
<th scope="col">Valor Desconto</th>
</tr>
</thead>
<tbody>
<?php
$tabela = 'produtos';
$colunas = 4;
$limite = 0;
echo $metodos->listarDados($tabela,$colunas,$limite);
?>
</tbody>
</table>
I want to create a generic method where through the settings below, define the amount of columns of the table above:
$tabela = 'produtos';
$colunas = 4;
$limite = 0;
Notice that you have the variable $columns = 4, it corresponds to the number of columns that the HTML table should have, that is, referring to Name, Description, Value and Discounted Value.
With this, I call the method as below:
echo $metodos->listarDados($tabela,$colunas,$limite);
And the method lies that way:
public function listarDados($tabela,$colunas,$limite)
{
$limite = ($limite == 0)?null:"LIMIT".$limite;
$sql = mysqli_query($this->conexao,"SELECT * FROM ".$tabela." ".$limite.";");
$visualizar = '';
for($c = 0; $c < $colunas; $c++)
{
while($pe = mysqli_fetch_object($sql))
{
$visualizar .= '<tr>';
// aqui incluir os valores referentes as colunas da tabela
$visualizar .= '</tr>';
}
}
return $visualizar;
}
I thought I’d put the for()
, but I saw that this would limit the rows and not the columns. Anyway, I would like to make it as it was defined the amount of columns in the $colunas
, appear the columns of the tables corresponding to the value included in the variable.
The way to do for the columns is to use a
for
on the outcome ofmysqli_query
, but that means accessing by position instead of column name, which is not good because it forces the order of the columns in the bank, and makes it difficult in the future.– Isac
Hello Isac. Could you show me an example? The order of the columns I can also put in the table thead.
– user24136
I thought the order was from the database table, but then the order has to be what’s inside the
<thead>
is this ? Assuming there are columns with the same name discarding the Casing ?– Isac
In fact the
<thead>
will follow the database order.– user24136