Jump to content


Photo

displaying the column horizontally


  • Please log in to reply
5 replies to this topic

#1 ericbangug

ericbangug
  • New Members
  • Pip
  • Newbie
  • 7 posts

Posted 12 September 2006 - 08:17 AM

i am having a hard time solving this

i have a query result like this

Posted Image

but i want to display it like this:

Posted Image

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



#2 fenway

fenway
  • Staff Alumni
  • MySQL Si-Fu / PHP Resident Alien
  • 16,199 posts
  • LocationToronto, ON

Posted 12 September 2006 - 01:37 PM

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...
Seriously... if people don't start reading this before posting, I'm going to consider not answering at all.

#3 ericbangug

ericbangug
  • New Members
  • Pip
  • Newbie
  • 7 posts

Posted 13 September 2006 - 01:21 AM

i am using php to generate this kind of table. thanks a lot for replying.  :)

#4 yaba

yaba
  • Members
  • PipPip
  • Member
  • 27 posts

Posted 13 September 2006 - 03:27 AM

A very quick and dirty way of doing this:

$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>";


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 ;)

#5 ericbangug

ericbangug
  • New Members
  • Pip
  • Newbie
  • 7 posts

Posted 13 September 2006 - 05:16 AM

i'll try it right now... message you back if it run smoothly. thanks mate ;)

#6 ericbangug

ericbangug
  • New Members
  • Pip
  • Newbie
  • 7 posts

Posted 13 September 2006 - 06:09 AM

it worked bro... thanks a lot...

;) ;) ;) ;)


Posted Image




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users