Avevo l'esigenza di creare una tabella HTML ed inserire dati provenienti da una tabella mysql (proprio come nel tuo caso), ho elaborato un codice generico che può essere adattato a qualsiasi tabella, in esso c'è molto di PHP compreso il metodo per tenere ordinato il codice.
Codice PHP:
$Resource_ID = mysql_query("SELECT * FROM My_TABLE") or die("<p>Errore! in <b>".__file__."</b> on line <b>".__line__."</b></p>");
$css_property_table = "border: 1px solid grey; background-color: gray;";
$css_property_tr = null;
$css_property_td = "background-color: white; padding: 2px;";
$css_property_th = "font-weight: bold; text-align: center; padding: 10px; color: white;";
define('OPEN_TABLE', "<table cellspacing=\"1\" style=\"".$css_property_table."\">");
define('OPEN_TR', "<tr style=\"".$css_property_tr."\">");
define('OPEN_TD', "<td style=\"".$css_property_td."\">");
define('CLOSE_TR', "</tr>");
define('CLOSE_TD', "</td>");
define('CLOSE_TABLE', "</table>");
define('OPEN_TH', "<th style=\"".$css_property_th."\">");
define('CLOSE_TH', "</th>");
print OPEN_TABLE;
while($Row = mysql_fetch_row($Resource_ID)) {
if(!isset($num_fileds)) {
$num_fileds = mysql_num_fields($Resource_ID);
}
if(!isset($th_table)) {
print OPEN_TR;
for($i = 0; $i < $num_fileds; $i++) {
print OPEN_TH . "".str_replace('_', ' ', mysql_field_name($Resource_ID, $i))."" . CLOSE_TH;
}
print CLOSE_TR;
$th_table = 1;
}
print OPEN_TR;
for($r = 0; $r < $num_fileds; $r++) {
printf(OPEN_TD . "%s" . CLOSE_TD, htmlspecialchars($Row[$r]));
}
print CLOSE_TR;
}
print CLOSE_TABLE;
mysql_free_result($Resource_ID);
mysql_close($Connection_DB);
unset($Connection_DB);
In questo codice vengono richiamate diverse funzioni di PHP fondamentali per il primo approccio a questo linguaggio. Buon lavoro!