Andrew
Andrew

Reputation: 53

Render a Table the “wrong” way: transpose Columns and Rows

I found this code on here but I don't understand what the $n refers to. I tried it but it gives me data on a one line.

$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));
}
...

Upvotes: 0

Views: 58

Answers (1)

CodeMonkey
CodeMonkey

Reputation: 678

For your first question: "I found this code on here but I don't understand what the $n refers to"

$data['name'] is an array, which seems to be filled from your database query.

In

foreach($data['name'] as $n) {

it is just looping through the array of names, on each iteration $n is assigned the value of the element in the array of names for that iteration

For your second question "I tried it but it gives me data on a one line."

that is correct.

printf('<td>%s</td>', htmlspecialchars($n));

the <td>s in this line prints out table cells. If you want rows, you need to use <tr> as well

Upvotes: 1

Related Questions