Credo ti basti parametrizzare la funzione:
Codice PHP:
function get_table($q, $headers, $fields){
$testo = "...";
foreach($headers as $header){
$testo .= "<th>$header</th>";
}
$testo .= '...';
while($row = ...){
$testo .= '<tr>';
foreach($fields as $field){
$testo .= '<td>'.$row->$field .'</td>';
}
$testo .= '</tr>';
}
echo $testo;
}
Cmq, perchè non sfrutti l'essere un template engine di php ?
Es. potresti crearti un file "table.tpl" con dentro il codice di sopra, senza la variabile $testo:
codice:
<table...>
<tr>
<?php foreach($headers as $header): ?>
<th><?php echo $header ?></th>
<?php endforeach ?>
</tr>
...tutto il resto ..
</table>
nella tua funzione get_table
Codice PHP:
function get_table($q, $headers, $fields){
ob_start();
include('table.tpl');
return ob_get_clean(); // oppure echo
}
e se poi ci rifletti bene, potresti renderla generica (basta che passi i parametri in modo congruo) e renderizzare qualunque cosa perchè poi cosa e come visualizzare sta nel tpl