Shad Posted October 23, 2006 Share Posted October 23, 2006 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 rowsi make a query and i display all rows in this tablee.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 https://forums.phpfreaks.com/topic/24864-displaying-data-vertically-and-horizontally/ Share on other sites More sharing options...
Orio Posted October 23, 2006 Share Posted October 23, 2006 [code]<?phpecho "<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 https://forums.phpfreaks.com/topic/24864-displaying-data-vertically-and-horizontally/#findComment-113319 Share on other sites More sharing options...
Barand Posted October 23, 2006 Share Posted October 23, 2006 Orio,You need to reverse the order of these lines so the increment comes first[code]if($i%3 == 0) echo "</tr>";$i++;[/code] Link to comment https://forums.phpfreaks.com/topic/24864-displaying-data-vertically-and-horizontally/#findComment-113381 Share on other sites More sharing options...
Shad Posted October 24, 2006 Author Share Posted October 24, 2006 thanks a lot! Link to comment https://forums.phpfreaks.com/topic/24864-displaying-data-vertically-and-horizontally/#findComment-113636 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.