Jump to content

Displaying data vertically and horizontally


Shad

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!
Link to comment
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.
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.