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! Quote Link to comment 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. Quote Link to comment 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] Quote Link to comment Share on other sites More sharing options...
Shad Posted October 24, 2006 Author Share Posted October 24, 2006 thanks a lot! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.