Вот функция, которая принимает mysqli
результат запроса и возвращает таблицу HTML с заголовками и данными.
<?php
function mysqli_result_2_table( $query_result, $width = '100%' ) {
$nrows = mysqli_num_rows($query_result);
if( $nrows > 0 ) {
$table = '<table style="width:' . $width . '"><thead><tr style="background-color:#ccc;color:#000;">';
$nfields = mysqli_num_fields($query_result);
while( $field = mysqli_fetch_field( $query_result ) ) {
$table .= '<th>' . ucfirst($field->name) . '</th>';
}
$table .= '</tr></thead><tbody>';
$even = true;
while( $row = mysqli_fetch_assoc($query_result) ) {
$table .= '<tr style="' . ($even ? 'background-color:#eee' : 'background-color:#ddd') . '">';
$even = !$even;
foreach($row as $field => $value) {
$table .= '<td>' . $value . '</td>';
}
$table .= '</tr>';
}
$table .= '</tbody><tfoot><tr style="background-color:#ccc;color:#000"><td colspan="' . $nfields . '">Query returned ' . $nrows . ' records.</td></tr></tfoot></table>';
}
else { echo '<p>Query returned an empty result (no rows).</p>'; }
return $table;
}
?>
Я довольно сильно изменил его, но вот исходный источник для справки:
sql query -> html table?!