stephdumais Posted April 23, 2007 Share Posted April 23, 2007 Hello, I'm currently looking for a way to populate some html tables of varying lenghts and widths, pulling the data from arrays. I want to achieve this with a function in order to make this as dynamic as possible. example: function build_table($data){ ... } $data would be an array containing a varying number of sub-arrays, the first array would populate the first row (column headings) and each subsequent array would be the data for each column rather than each row. Thus the table would be build as such: row 1 = array1[1],array2[1],array3[1]... row 2 = array1[2],array2[2],array3[2]... row 3 = array1[3],array2[3],array3[3]... In other words, I would like each specific key value from each array to be merged into new arrays that form the rows. Let me know if this is clear enough...if so, does anyone have any ideas? Quote Link to comment Share on other sites More sharing options...
stephdumais Posted April 24, 2007 Author Share Posted April 24, 2007 Here's a simpler way to explain what I'm trying to achieve: Here's my particular setup: $data = array( array('1','2','3','4'), array('5','6','7','8'), array('9','10','11','12'), ); Each sub-array in my $data should be a column, while each element in the sub-arrays should become a row, resulting in a table like this: 1 | 5 | 9 2 | 6 | 10 3 | 7 | 11 4 | 8 | 12 Now I'm not sure how to loop this... Any ideas? Quote Link to comment Share on other sites More sharing options...
Barand Posted April 24, 2007 Share Posted April 24, 2007 try <?php $data = array( array('1','2','3','4'), array('5','6','7','8'), array('9','10','11','12'), ); // Simplest way echo "<table><tr>"; foreach ($data as $col) { echo '<td>' . join ('<br>', $col) . '<td>'; } echo "</tr></table>"; ?> Quote Link to comment Share on other sites More sharing options...
stephdumais Posted April 24, 2007 Author Share Posted April 24, 2007 Ah, that's good...but what if I need my rows to be actual table rows? I need real rows because some table elements might take up multiple lines, so I would need all columns on that row to be the same height. Quote Link to comment Share on other sites More sharing options...
Barand Posted April 24, 2007 Share Posted April 24, 2007 <?php $data = array( array('1','2','3','4'), array('5','6','7','8'), array('9','10','11','12'), ); echo "<table border='1'>"; for ($r = 0; $r < 4; $r++) { echo '<tr>'; for ($c = 0; $c < 3; $c++) { echo '<td>', $data[$c][$r], '</td>'; } echo '</tr>'; } echo "</table>"; ?> Quote Link to comment Share on other sites More sharing options...
stephdumais Posted April 24, 2007 Author Share Posted April 24, 2007 Superb! Thank you very much Quote Link to comment Share on other sites More sharing options...
taith Posted April 24, 2007 Share Posted April 24, 2007 just as an idea... <? $query=mysql_query("SELECT * FROM `accounts`"); while($row=mysql_fetch_assoc($query)){ $arr[]=$row; } echo '<table>'; $k=0; for($i=0; $i<=count($arr)/3; $i++){ if($k==0) echo '<tr>'; echo '<td>'; print_r($arr[$i]); echo '</td>'; echo '<td>'; print_r($arr[$i+3]); echo '</td>'; echo '<td>'; print_r($arr[$i+6]); echo '</td>'; $k++; echo '</tr>'; } echo '</table>'; ?> math's prolly off tho... 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.