rvdb86 Posted February 2, 2009 Share Posted February 2, 2009 hey, im sure this is prety simple, i have just never done it before so I was hoping someone can help me out. i want to display the results of my database table in a html table that has three columns and if there are not enough results to complete the final row it shouw echo   like this: -------------------------------- | $val1 | $val2 | $val3 | -------------------------------- | $val4 | $val5 | $val6 | -------------------------------- | $val7 |   |   | -------------------------------- I hope someone has any ideas that could help me! Quote Link to comment https://forums.phpfreaks.com/topic/143498-solved-displaying-table-results-in-multi-column-table/ Share on other sites More sharing options...
premiso Posted February 2, 2009 Share Posted February 2, 2009 Post the code and we probably can help you. Quote Link to comment https://forums.phpfreaks.com/topic/143498-solved-displaying-table-results-in-multi-column-table/#findComment-752781 Share on other sites More sharing options...
sasa Posted February 2, 2009 Share Posted February 2, 2009 some like this <?php $count = 0; echo '<table border="3">'; for ($i=0;$i<10;$i++){ //start loop if ($count == 0) echo '<tr>'; $count++; echo '<td>',$i,'</td>';// echo some data in td tag if ($count == 3){ echo '</tr>'; $count=0; } } //end loop if ($count>0){ for (;$count<3;$count++) echo '<td> </td>'; echo '</tr>'; } echo '</table>'; ?> Quote Link to comment https://forums.phpfreaks.com/topic/143498-solved-displaying-table-results-in-multi-column-table/#findComment-752791 Share on other sites More sharing options...
rvdb86 Posted February 2, 2009 Author Share Posted February 2, 2009 Hey thanks for the quick replies! i really appreciate it i user a mysql query to get the results of the databse table and thanks to sasa i can echo the results in a table with 3 columns and x number of rows. the problem is if the number of results is not divisible by 3 i will have some cells empty. how can i make it so that the script counts the number of results, checks to see if it is divisible by 3, and if not then insert   to complete the table. for example if there are 4 results i will need the first row with the first 3 results and the second row with the 4th result and two   $query = "SELECT * FROM tbl_products"; $result = mysql_query($query) or die ("Couldn't execute query"); $counter = 0; while ($row = mysql_fetch_array($result)) { extract($row); for ($i=0;$i<10;$i++){ //start loop if ($count == 0) echo '<tr>'; $count++; echo '<td>',$product_name,'</td>';// echo some data in td tag if ($count == 3){ echo '</tr>'; $count=0; } } //end loop Quote Link to comment https://forums.phpfreaks.com/topic/143498-solved-displaying-table-results-in-multi-column-table/#findComment-752825 Share on other sites More sharing options...
rvdb86 Posted February 2, 2009 Author Share Posted February 2, 2009 sorry here is my updated code: <?php $query = "SELECT * FROM tbl_products"; $result = mysql_query($query) or die ("Couldn't execute query"); $row = mysql_fetch_array($result); $num_rows = mysql_num_rows($result); for ($i=0;$i<$num_rows;$i++){ //start loop if ($count == 0) echo '<tr>'; extract($row[$i]); $count++; echo '<td>',$product_name,'</td>';// echo some data in td tag if ($count == 3){ echo '</tr>'; $count=0; } } //end loop ?> Quote Link to comment https://forums.phpfreaks.com/topic/143498-solved-displaying-table-results-in-multi-column-table/#findComment-752845 Share on other sites More sharing options...
trq Posted February 2, 2009 Share Posted February 2, 2009 have you looked at this thread in the FAQ/Code Snippet Repository board? Quote Link to comment https://forums.phpfreaks.com/topic/143498-solved-displaying-table-results-in-multi-column-table/#findComment-752852 Share on other sites More sharing options...
rvdb86 Posted February 2, 2009 Author Share Posted February 2, 2009 thorpe thank you soooo much that is exactly what i needed! thanks to every one who took the spare time to help me, know that it has not gone unappreciated! Quote Link to comment https://forums.phpfreaks.com/topic/143498-solved-displaying-table-results-in-multi-column-table/#findComment-752897 Share on other sites More sharing options...
sasa Posted February 3, 2009 Share Posted February 3, 2009 have you looked at this thread in the FAQ/Code Snippet Repository board? clean up table part must be // clean up table - makes your code valid! if($i > 0) { for($j=$i; $j<$max_columns;$j++) echo "<td> </td>"; echo "</tr>"; } ?> </table> Quote Link to comment https://forums.phpfreaks.com/topic/143498-solved-displaying-table-results-in-multi-column-table/#findComment-753320 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.