Strydaris Posted July 1, 2007 Share Posted July 1, 2007 Ok I am completely new to this whole PHP scripting stuff and really only try to deal with simple scripts and such for my website. It has done me fine until now. I know this is going to be a simple question to answer and probaly has been answerd before but I have spent an hour on this forum and could not find one yet. Anyways here it is... I have an array of lets say something like $my_array = array( 1=> 'Numbers', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine', 'ten', 'eleven', Now I have been searching for a way to put that array into an html table without any luck. Want I want to do is have lets say 5 columns and 3 rows where it would be laid out something like this in the end. Numbers|one |two |three|four | five |six |seven|eight|nine | ten |eleven| | | | Any help here would be much appiciated!! Cheers Quote Link to comment https://forums.phpfreaks.com/topic/57904-arrays-and-html-tables/ Share on other sites More sharing options...
pocobueno1388 Posted July 1, 2007 Share Posted July 1, 2007 Try this and see if it is what your looking for: <?php $my_array = array( 1=> 'Numbers', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine', 'ten', 'eleven'); $count = 1; foreach ($my_array as $num){ echo $num.' | '; if ($count == 5){ echo '<br>'; $count = 0; } $count++; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/57904-arrays-and-html-tables/#findComment-286942 Share on other sites More sharing options...
Strydaris Posted July 1, 2007 Author Share Posted July 1, 2007 its closer then I was... lol I think you misunderstood my little illustration up there but for the most part works. I did the little illustration of a table to show that I wanted to use <table></table> in the code. Thanks... now I need to figure out how to get the HTML code in there. I think I do the the variables for the amount of columns and rows I want to use. Something like $rows and $cols. lol i dont know for sure though. Quote Link to comment https://forums.phpfreaks.com/topic/57904-arrays-and-html-tables/#findComment-286947 Share on other sites More sharing options...
MadTechie Posted July 1, 2007 Share Posted July 1, 2007 try this <?php $my_array = array( 1=> 'Numbers', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine', 'ten', 'eleven'); $count = 1; echo "<table border='1'>"; echo "<tr>"; foreach ($my_array as $num){ echo "<td>$num</td>"; if ($count == 5){ echo "</tr><tr>"; $count = 0; } $count++; } //cleanup while ($count != 1){ echo "<td> </td>"; if ($count == 5){ echo "</tr><tr>"; $count = 0; } $count++; } echo "</tr>"; echo "</table>"; ?> EDIT: added a cleanup for the table Quote Link to comment https://forums.phpfreaks.com/topic/57904-arrays-and-html-tables/#findComment-286952 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.