\$\begingroup\$
\$\endgroup\$
1
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);
-
\$\begingroup\$ There's also PEAR's HTML_Table package, if you're into that sort of thing. :-) \$\endgroup\$Sam Wilson– Sam Wilson2011年05月31日 00:06:42 +00:00Commented May 31, 2011 at 0:06
3 Answers 3
\$\begingroup\$
\$\endgroup\$
2
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>';
}
-
\$\begingroup\$ Sweet, I wasn't aware of
array_fill
orimplode
. 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. \$\endgroup\$William– William2011年05月06日 21:36:23 +00:00Commented May 6, 2011 at 21:36 -
\$\begingroup\$ that's what i get for not testing. fixed the variable, and naming to make more sense. \$\endgroup\$h0tw1r3– h0tw1r32011年05月07日 07:26:40 +00:00Commented May 7, 2011 at 7:26
\$\begingroup\$
\$\endgroup\$
3
- 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>";
-
\$\begingroup\$ 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. \$\endgroup\$William– William2011年05月06日 02:39:19 +00:00Commented May 6, 2011 at 2:39
-
\$\begingroup\$ 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. \$\endgroup\$William– William2011年05月06日 02:42:17 +00:00Commented May 6, 2011 at 2:42 -
\$\begingroup\$ Perhaps $max could be called $num_tbl_columns since it's the number of columns in the table. \$\endgroup\$ramblex– ramblex2011年05月06日 12:51:46 +00:00Commented May 6, 2011 at 12:51
2
-
\$\begingroup\$ 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. \$\endgroup\$William– William2011年05月06日 02:52:28 +00:00Commented May 6, 2011 at 2:52
-
\$\begingroup\$ Actually, you can using foreach($array as $key => &$value) \$\endgroup\$h0tw1r3– h0tw1r32011年05月06日 17:03:32 +00:00Commented May 6, 2011 at 17:03
lang-php