Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Render a Table the "wrong" way: transpose Columns and Rows?

I'm wondering if there is an easy way to draw a table using the standard while(mysql_fetch_assoc) where the left most column is the header like this:

Name      | Peter Jacksson    | Steven Spielberg | Martin Scorsese |
Birthyear | 1961              | 1946             | 1942 |
Movie     | Lord of the Rings | Jurassic Park    | Cape Fear |

(Rats! SO doesn't support the table-tag).

like image 295
Sandokan Avatar asked Jan 27 '26 10:01

Sandokan


1 Answers

I would do this by adding the elements to an array structure and then printing that structure:

$data = array('name' => array(), 'birth' => array(), 'movie' => array());
while($r = mysql_fetch_assoc(...)) {
  $data['name'][] = $r['name'];
  ...
}

echo '<th>Name</th>';
foreach($data['name'] as $n) {
  printf('<td>%s</td>', htmlspecialchars($n));
}
...
like image 76
Emil Vikström Avatar answered Jan 29 '26 03:01

Emil Vikström



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!