6
\$\begingroup\$

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);
\$\endgroup\$
  • \$\begingroup\$ There's also PEAR's HTML_Table package, if you're into that sort of thing. :-) \$\endgroup\$ – Sam Wilson May 31 '11 at 0:06
4
\$\begingroup\$

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>';
}
\$\endgroup\$
  • \$\begingroup\$ 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. \$\endgroup\$ – William May 6 '11 at 21:36
  • \$\begingroup\$ that's what i get for not testing. fixed the variable, and naming to make more sense. \$\endgroup\$ – h0tw1r3 May 7 '11 at 7:26
3
\$\begingroup\$
  • 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>";
    
\$\endgroup\$
  • \$\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 May 6 '11 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 May 6 '11 at 2:42
  • \$\begingroup\$ Perhaps $max could be called $num_tbl_columns since it's the number of columns in the table. \$\endgroup\$ – ramblex May 6 '11 at 12:51
1
\$\begingroup\$

replace for loops with foreach

\$\endgroup\$
  • \$\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 May 6 '11 at 2:52
  • \$\begingroup\$ Actually, you can using foreach($array as $key => &$value) \$\endgroup\$ – h0tw1r3 May 6 '11 at 17:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.