Jump to content

Archived

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

Shad

Displaying data vertically and horizontally

Recommended Posts

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.
[code=php:0]
echo "<TABLE>";
$query = mysql_query("SELECT * FROM table");
while ($qry = mysql_fetch_array($query)) {
echo "<TR><TD>$qry[name]</TD></TR>";
}
echo "</TABLE>";
[/code]
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!

Share this post


Link to post
Share on other sites
[code]<?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>";

?>[/code]

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

Orio.

Share this post


Link to post
Share on other sites
Orio,

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

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.