patternphpMinor
PHP Table generation
Viewed 0 times
phpgenerationtable
Problem
I have been using the following function (in the kohana html class in a table) to generate tables.
And then I have been calling it like so:
And a reversed table that auto fills the columns.
function table($columns,$reverse=false,$fill=false,array $attr=null){
$c = '';
//ugly hack, looking for fix
$max=sizeof(max($columns));
for($i=0;$i";
for($ii=0,$ll=sizeof($column);$ii".$column[$ii]."";
}
$c.="";
}
return "".$c."";
}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);Solution
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) ' . implode('', $columns) . '';
}
return '' . $c . '';
}Code Snippets
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>';
}Context
StackExchange Code Review Q#2171, answer score: 4
Revisions (0)
No revisions yet.