Jump to content


Photo

Displaying data vertically and horizontally


  • Please log in to reply
3 replies to this topic

#1 Shad

Shad
  • Members
  • PipPip
  • Member
  • 12 posts

Posted 23 October 2006 - 06:49 PM

Hey guys, first time posting in this forum.

I was wondering how i would go about displaying data from a table vertically and horizontally. allow me to explain:

i have a table named "table" and one field inside it named "name" with 9 rows
i make a query and i display all rows in this table
e.g.
echo "<TABLE>";
$query = mysql_query("SELECT * FROM table");
while ($qry = mysql_fetch_array($query)) {
echo "<TR><TD>$qry[name]</TD></TR>";
}
echo "</TABLE>";
naturally this will display the "names" vertically in the table with 9 rows.

I would like to know how i would be able to display them so that it would display 3 rows and 3 coloumns which display all 9 names in this format.

Hope you can help!

#2 Orio

Orio
  • Staff Alumni
  • Advanced Member
  • 2,491 posts

Posted 23 October 2006 - 07:03 PM

<?php

echo "<table>";
$query = mysql_query("SELECT * FROM table");

$results=array();
while ($qry = mysql_fetch_array($query))
 $results[]=$qry['name'];

$i=0;
foreach ($results as $name)
{
if($i%3 == 0) echo "<tr>";
echo "<td>".($i+1).") ".$name."</td>";
if($i%3 == 0) echo "</tr>";
$i++;
}

echo "</table>";

?>

This code is dynamic- It will work if there are more than 9 names.

Orio.
Think you're smarty?

(Gone until 20 to November)

#3 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,023 posts

Posted 23 October 2006 - 09:40 PM

Orio,

You need to reverse the order of these lines so the increment comes first
if($i%3 == 0) echo "</tr>";
$i++;

If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#4 Shad

Shad
  • Members
  • PipPip
  • Member
  • 12 posts

Posted 24 October 2006 - 02:19 PM

thanks a lot!




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users