tags:

views:

51

answers:

1

I found this great example which takes an sql statement and dynamically adds the field names into a header row and the data into the following rows?

Heres the example http://www.weberdev.com/get_example-4249.html

A: 

Since HTML tables must be written out by row, you'll have to read all the data before you're able to write out your table.

$rows = array();
while ($row = mysql_fetch_assoc($result)) $rows[] = $row;

// We'll use the first row to get the field names:
foreach($rows[0] as $field => $throwaway) {
    echo '<tr>';
    echo '<td>' . htmlspecialchars($field) . '</td>';
    // And for each field iterate through ALL rows:
    foreach($rows as $row) {
        echo '<td>' . htmlspecialchars($row[$field]) . '</td>';
    }
    echo '</tr>';
}
VoteyDisciple
With the array-population code included in my example: `while ($row = mysql_fetch_assoc($result)) $rows[] = $row;`
VoteyDisciple
Ah ok, didn't realise $rows = array() was just a declaration. I'm now getting results, but the first row and second row are the same, I'm not getting any field names.
Jules
Whoops — should be outputting `$field` not `$throwaway`. Edited.
VoteyDisciple