7
\$\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);
asked Apr 30, 2011 at 2:44
\$\endgroup\$
1
  • \$\begingroup\$ There's also PEAR's HTML_Table package, if you're into that sort of thing. :-) \$\endgroup\$ Commented May 31, 2011 at 0:06

3 Answers 3

5
\$\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>';
}
answered May 6, 2011 at 17:09
\$\endgroup\$
2
  • \$\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\$ Commented 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\$ Commented May 7, 2011 at 7:26
4
\$\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>";
    
answered May 1, 2011 at 14:24
\$\endgroup\$
3
  • \$\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\$ Commented 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\$ Commented 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\$ Commented May 6, 2011 at 12:51
1
\$\begingroup\$

replace for loops with foreach

answered May 5, 2011 at 20:53
\$\endgroup\$
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\$ Commented May 6, 2011 at 2:52
  • \$\begingroup\$ Actually, you can using foreach($array as $key => &$value) \$\endgroup\$ Commented May 6, 2011 at 17:03

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.