I have an admin and I have serious doubts about how to generate tables in php normally what I do is paste the html into a web editor but does not that seem like the best suggestions?
I have an admin and I have serious doubts about how to generate tables in php normally what I do is paste the html into a web editor but does not that seem like the best suggestions?
Let's say I have multiple tables in my database, each with a different structure. Since I have data (rows) 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>
This function assembles a table from any select. $ sql is the desired select. Function usage example:
mostrarTabela("select cd, nome from tabela");
I hope you can be useful 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!";
}
}
Will your table be dynamic? If so, you can use Jquery. Just in the HTML you only indicate the opening and closing tags of the table in the body of the page, give an explicit id for it and, in Jquery, fill it according to the structure and data coming from a database, for example.