Jump to content

Archived

This topic is now archived and is closed to further replies.

ericbangug

displaying the column horizontally

Recommended Posts

i am having a hard time solving this

i have a query result like this

[img]http://i29.photobucket.com/albums/c298/nejie/horizontal.jpg[/img]

but i want to display it like this:

[img]http://i29.photobucket.com/albums/c298/nejie/vertical.jpg[/img]

i am still new to php that's why forgive me for my ignorance... thanks alot

Share this post


Link to post
Share on other sites
Not exactly a MySQL question, but I'm not sure how you're generating this, so I don't even know what forum to move it to...

Share this post


Link to post
Share on other sites
A very quick and dirty way of doing this:

[code=php:0]
$rows = $db->query("my query here");

while ($r = $rows->fetch_assoc()){
$names[] = $r['name'];
$ages[] = $r['age'];
$sexes[] = $r['sex'];
$addresses[] = $r['address'];
}

$out = "<tr><td>name</td>";
foreach ($names as $n)
$out = "<td>".$n[$i]."</td>";
$out .= "</tr>";

$out .= "<tr><td>age</td>";
foreach ($ages as $a)
$out = "<td>".$a[$i]."</td>";
$out .= "</tr>";

$out .= "<tr><td>sex</td>";
foreach ($sexes as $s)
$out = "<td>".$s[$i]."</td>";
$out .= "</tr>";

$out .= "<tr><td>address</td>";
foreach ($addresses as $a)
$out = "<td>".$a[$i]."</td>";
$out .= "</tr>";

echo "<table>{$out}</table>";
[/code]


I bet this can be written in a better way, as this is not very efficient. And I haven't run this, maybe there are typos. And the first 2-3 lines assume you are using OO mysqli, you can easily change the way you fetch your rows.

Hope this helps ;)

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.