0
I have an admin and I have serious doubts about how to generate tables in php normally what I do is paste html in a web editor but this solution does not seem to me the best suggestions?
0
I have an admin and I have serious doubts about how to generate tables in php normally what I do is paste html in a web editor but this solution does not seem to me the best suggestions?
1
Let’s say I have several tables in my database, each with a different structure. Whereas I have data(lines) in all of them, you can do the following:
<?php
$dados; // essa variável tem um array com cada linha da tabela
// vou usar o primeiro resultado para conseguir a lista de colunas.
// Você pode querer alguma coisa mais robusta, tipo passar uma
// lista de campos e usar eles na view;
$colunas = array_keys($dados[0]);
?>
<table>
<thead>
<tr>
<?php foreach ($colunas as $coluna): ?>
<th><?= $coluna ?></th>
<?php endforeach; ?>
</tr>
</thead>
<tbody>
<?php foreach ($dados as $linha): // para cada linha ?>
<tr>
<?php foreach ($linha as $key => $value): // para cada campo de cada linha ?>
<td><?= $value ?></td>
<?php endforeach ?>
</tr>
<?php endforeach; ?>
</tbody>
</table>
Good answer but how would I have to have different tds? Calculate tds for example if you had one with 4 columns and another with 3?
What are different tables? Give me a more concrete example in your question (edit it). If your structure is more complex than this, you will need to treat case-by-case, or count columns with each new row, but then it becomes a much more complex work, maybe it is better to use something ready, like a datatable of jQuery: https://datatables.net/manual/ajax
I say in the same table if a row has 3 columns and another 4 not to break the table. But it really gets more complex.
0
This function mounts a table from any select. $sql is the desired select. Ex. function usage:
mostrarTabela("select cd, nome from tabela");
I hope I can be of use to someone.
function mostrarTabela($sql){
//Mostra uma tabela com o resultado da select automaticamente
global $banco; //conexao com o banco desejado feita fora da funcao. Se quiser pode colocar aqui.
if (!$idSQLControle=mysql_query($sql,$banco))
echo "<b>ERRO SQL:</b>" . $sql_Controle;
$nrregSQLControle=mysql_num_rows($idSQLControle);
if ($nrregSQLControle > 0) {
$fields_num = mysql_num_fields($idSQLControle);
echo "<br><b>$sql_Controle</b><br/>";
echo "<table border='1'><tr>";
//cabecalho da tabela
for($i=0; $i<$fields_num; $i++)
{
$field = mysql_fetch_field($idSQLControle);
echo "<td><b>{$field->name}</b></td>";
}
echo "</tr>\n";
//dados da tabela
while($linha = mysql_fetch_row($idSQLControle))
{
echo "<tr>";
foreach($linha as $colunas)
echo "<td>$colunas</td>";
echo "</tr>\n";
}
echo "</table>";
} else {
echo "Registros não encontrados!";
}
}
Browser other questions tagged php
You are not signed in. Login or sign up in order to post.
How hard is it to write code? If you want to type less, you can use Emmet in your favorite code editor: https://emmet.io/
– Daniel
The problem is editing these tables later because you have to redo the html
– Vagner Franco
Man, you need more detail. In the comment below you said that the columns change too, now I understand what you meant: you want to pass an object and exit a table formatted according to the fields of this object, right?
– Daniel
That exact would like a kind of a template where add a line with a text field something like that and generate a field in the table wanted to know how best to do this.
– Vagner Franco