# PHP Table generation

I have been using the following function (in the kohana html class in a table) to generate tables.

function table($columns,$reverse=false,$fill=false,array$attr=null){
$c = ''; //ugly hack, looking for fix$max=sizeof(max($columns)); for($i=0;$i<sizeof($columns);$i++){$column = $columns[$i];

if($fill==true){$l = ($max-sizeof($column));
for($ii=0;$ii<$l;$ii++){
array_push($column,''); } } if($reverse==true){
$columns[$i] = array_reverse($column); }else{$columns[$i] =$column;
}
}
for($i=0,$l=sizeof($columns);$i<$l;$i++){
$column =$columns[$i];$c.="<tr>";
for($ii=0,$ll=sizeof($column);$ii<$ll;$ii++){
$c.="<td>".$column[$ii]."</td>"; }$c.="</tr>";
}
return "<table".html::attributes($attr).">".$c."</table>";
}


And then I have been calling it like so:

echo html::table(array(
array('colunm 1 row 1','colunm 2 row 1'),
));


And a reversed table that auto fills the columns.

echo html::table(array(
array('colunm 1 row 1','colunm 2 row 1'),
array('column 1 row 2','column 2 row 2')
),true,true);

• There's also PEAR's HTML_Table package, if you're into that sort of thing. :-) May 31, 2011 at 0:06

Didn't test, but I would write it something like this:

function table($rows,$reverse = false, $fill = false, array$attr = null) {
$c = '';$max_cols = sizeof(max($columns)); foreach ($rows as $columns) { if ($fill && sizeof($columns) <$max_cols) {
$columns = array_merge($columns, array_fill(0, $max_cols - sizeof($columns), ''));
}
if ($reverse) {$columns = array_reverse($columns); }$c .= '<tr><td>' . implode('</td><td>', $columns) . '</td></tr>'; } return '<table>' .$c . '</table>';
}

• Sweet, I wasn't aware of array_fill or implode. Very convenient and it shortens the function by a lot. There was one bug $fill_to should be $max_cols. I decided to change some of the naming from $columns to $rows. Here is the edited version: codepad.org/UQyynzqW Thanks. May 6, 2011 at 21:36
• that's what i get for not testing. fixed the variable, and naming to make more sense. May 7, 2011 at 7:26
• Some of the variable names could benefit from being slightly less terse.
• Aren't you passing in an array of rows? Each element in the array is then an array of columns.
• What is max? Is it trying to find the row with the most columns? Either rename to something more self-documenting or add a comment.
• array_map may be worth a look and could feasibly allow for reverse to be replaced with a callback function.
• Consider changing the last lines of the function to:

return "<table".html::attributes($attr).">".$c."</table>";

• I'm not sure what I was thinking for the return statement. Or rather I wasn't thinking %). Also, yes I'm passing in an array of rows which contains an array of columns. May 6, 2011 at 2:39
• Yes max is attempting to find the max number of columns. Basically if you reverse a table with out all the rows having the same number of columns, the table will fill to the left. By passing in true to $fill it adds additional empty cells to the end of table so it doesn't fill to the left. What would you advise for a naming alternative to $max? That's the php function name so I thought it was well suited. May 6, 2011 at 2:42
• Perhaps $max could be called$num_tbl_columns since it's the number of columns in the table. May 6, 2011 at 12:51

replace for loops with foreach

• I would love to use foreach, but I am unsure how to use it in place of the first for loop. I am reassigning itself as a reversed array at the end of the loop. I don't believe php allows you to reassign the looping variable inside a foreach. May 6, 2011 at 2:52
• Actually, you can using foreach($array as$key => &\$value) May 6, 2011 at 17:03