Jump to content

displaying the column horizontally


ericbangug

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

Link to comment
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 ;)
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.