doddsey_65 Posted December 12, 2010 Share Posted December 12, 2010 Im messing around with functions and arrays but cant seem to get this to work. It basically creates a simple table with the parameters you specify. The array however doesnt go into the table properly. function asf_create_table($rows, $cols, $border=1, $padding=5, $td_border=1, $contents) { $table = "<table style=\"border: {$border}px solid; padding:{$padding}px;\">"; for ($t_rows=0; $t_rows<$rows; $t_rows++) { $table .= "<tr>"; } for ($t_cols=0; $t_cols<$cols; $t_cols++) { for ($i=0; $i<$cols; $i++) { $table .= "<td style=\"border: {$td_border}px solid;\">"; $table .= $contents[$i]; $table .= "</td>"; } } for ($t_rows=0; $t_rows<$rows; $t_rows++) { $table .= "</tr>"; } $table .= "</table>"; echo $table; } $t_contents = array("Cell 1", "Cell 2", "Cell 3", "Cell 4"); asf_create_table("4", "4", "1", "5", "1", $t_contents); instead of 4 cells each with Cell # in them i get 16 cells with the cell #. the 4 displayed 4 times. Quote Link to comment Share on other sites More sharing options...
BlueSkyIS Posted December 12, 2010 Share Posted December 12, 2010 table structure logic appears to be off. this might be closer: for ($t_rows=0; $t_rows<$rows; $t_rows++) { $table .= "<tr>"; for ($t_cols=0; $t_cols<$cols; $t_cols++) { for ($i=0; $i<$cols; $i++) { $table .= "<td style=\"border: {$td_border}px solid;\">"; $table .= $contents[$i]; $table .= "</td>"; } } $table .= "</tr>"; } Quote Link to comment Share on other sites More sharing options...
doddsey_65 Posted December 12, 2010 Author Share Posted December 12, 2010 yeh i realised that when i posted it so i changed it to that, but i still get 16 columns of data and 4 rows. Quote Link to comment Share on other sites More sharing options...
OOP Posted December 12, 2010 Share Posted December 12, 2010 Try this <?php function asf_create_table($rows, $cols, $border = 1, $padding = 5, $td_border = 1, $contents) { $table = '<table style="border:'.$border.'px solid; padding:{'.$padding.'}px;">'; for($t_rows = 0; $t_rows < $rows; $t_rows ++) { $table .= '<tr>'; for($i = 0; $i < $cols; $i ++) { $table .= '<td style="border: '.$td_border.'px solid;">'; $table .= $contents [$i]; $table .= '</td>'; } $table .= '</tr>'; } $table .= '</table>'; echo $table; } $t_contents = array ('Cell 1', 'Cell 2', 'Cell 3', 'Cell 4' ); asf_create_table ( '4', '4', '1', '5', '1', $t_contents ); ?> 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.