The code: http://ezdispatch.net/view_table_class.php
One of the things that I did for my most recent project, was a Table class, for building HTML Tables. It eliminates the ugly echo's etc that are so common for php-built tables, and it also eliminates a couple of the more common errors. Tags will always be in the correct order, all rows are adjusted to have the same number of cells (taking into account rowspans and colspans), and empty cells are filled with  's. It is FAR from perfect, but I thought I'd post it here anyway. Feel free to use it, and better yet...post back with updates/corrections/upgrades. There are instructions in the comments of the class itself on changing it to work with PHP 4. The class is attached.
It would be used like this:

$table = new Table(array('border'=>1));
$cur_row =& $table->add_head_row(array('style'=>'font-weight:bold;'));
$cur_row->add_cell(array('data'=>'Header 1'));
$cur_row->add_cell(array('data'=>'Header 2'));
$cur_row->add_cell(array('data'=>'Header 3'));
$cur_row->add_cell(array('data'=>'Header 4'));
$cur_row =& $table->add_row();
$cur_row->add_cell(array('data'=>'Row 1 - Col 1','rowspan'=>3));
$cur_row->add_cell(array('data'=>'Row 1 - Col 2','colspan'=>2));
$cur_row->add_cell(array('data'=>'Row 1 - Col 4'));
$cur_row =& $table->add_row();
$cur_row->add_cell(array('data'=>'Row 2 - Col 2'));
$cur_row->add_cell(array('data'=>'Row 2 - Col 4','rowspan'=>2, 'colspan'=>2));
$cur_row =& $table->add_row();
$cur_row->add_cell(array('data'=>'Row 3 - Col 3'));
$cur_row =& $table->add_row();
$cur_row->add_cell(array('data'=>'Row 4 - Col 1','colspan'=>4));
echo $table->get_table();

To test the error corrections, just remove or empty any cell, and watch it compensate!
And you can get as fancy as you want. The functions that build the tables, rows, and cells all take an associative array as a parameter, and anything you want can be set in there. For example, if you pass array('style'=>'padding-top:50px;','onmouseover'=>'alert("mouse is over")') to add_cell(), you will get a cell with a 50px top padding, and it will trigger an alert onmouse over. You can read through the comments in the code, and if you have any questions, feel free to ask!
The code: http://ezdispatch.net/view_table_class.php
My research as far as WHAT options to allow for each tag came from these links:
http://www.w3.org/TR/html4/index/elements.html
http://www.w3.org/TR/html4/index/attributes.html

    This is really nice. Have you had a look at PEAR HTML_Table? From my brief glance your version seems alot more easy to use. Perhaps it would be worth comparing the actual code to see how they do things.

    There aren't many improvements I could suggest. Perhaps including a few more attributes to the cells, for example 'scope' and 'headers' - to facilitate more Accessible table structures.

    There seems to be some potential use of the Decorator design pattern here too, maybe for differentiating between header, body and footer rows.

    All in all though a nice effort 🙂

      I'm going to look into that, but in the mean time:
      I updated the class some, you can still view it here.
      You need to use it like this now:

      $table = new Table(array('border'=>1));
      $cur_row =& $table->add_head_row(array('style'=>'font-weight:bold;'));
      $cur_row->add_cell('Header 1');
      $cur_row->add_cell('Header 2');
      $cur_row->add_cell('Header 3');
      $cur_row->add_cell('Header 4');
      $cur_row =& $table->add_row();
      $cur_row->add_cell('Row 1 - Col 1',array('rowspan'=>3));
      $cur_row->add_cell('Row 1 - Col 2',array('colspan'=>2));
      $cur_row->add_cell('Row 1 - Col 4');
      $cur_row =& $table->add_row();
      $cur_row->add_cell('Row 2 - Col 2');
      $cur_row->add_cell('Row 2 - Col 4',array('rowspan'=>2, 'colspan'=>2));
      $cur_row =& $table->add_row();
      $cur_row->add_cell('Row 3 - Col 3');
      $cur_row =& $table->add_row();
      $cur_row->add_cell('Row 4 - Col 1',array('colspan'=>4));
      echo $table->get_table();
      

      The main reason for the update was to change the settings for tables, rows, and cells to be stored in a 'settings' array, rather than seperate variables. This makes the filling of such an array far more efficient, AND it makes it REALLY easy to add support for another setting. For example, if you wanted to add support for the align propery for the table (NOT w3 standards), all you have to do is add 'align'=>NULL to the $settings array in the Table class. Since the output has been re-worked into a foreach, it will automatically be added to the table tag.

      Because of this re-structuring, I moved the th vs td option OUTSIDE the $settings array, since it isn't really that sort of setting.

      This brings it down to 285 lines too!! Now if only I could clean up the prep_table function...it's kinda ugly.

        3 years later

        I looked over your code. (Great! BTW!) And I have a question:

        Would it be better if you used an array instead of having all of the various HTML commands in individual variables? I've been working on a table class myself and I came up with:

        [INDENT]

        var $html = array();

        [/INDENT]

        In this way you can still do the:

        [INDENT]

        if( isset($html['onkeydown']) ){ <Code> }

        [/INDENT]

        but you don't have to take up the memory space each of the variables takes up. When someone specifies the HTML command, you just make an entry in the array and set it to TRUE (or the value).

        Just a thought. Accessing the array might be a bit slower than accessing the variable, but you lower your overhead by doing the above because no entry is made into the array unless the user specifies it.

          Incidentally, in PHP 5 it's not necessary to use =& to assign objects; they're already being passed by reference, so

          $cur_row = $table->add_body_row();
          $cur_row->add_cell('Row 2 - Col 2'); 

          would do the right thing and the new cell "Row 2 - Col 2" to the new row in $table. Likewise, the & in, e.g.,

          public function &add_body_row() 

          isn't needed.

          One thing I'd change about the class is to have add_row() and the like finish by returning $this. Then I can write

          $table->add_body_row()->add_cell('Row 2 - Col 2')->add_cell('Row 2 - Col 4',array('rowspan'=>2, 'colspan'=>2)); 

          Oh, and for

          $settings = array_diff($settings,array('')); 

          it would appear that

          $settings = array_filter($settings,'strlen');

          says what you want a bit more directly.

          preg_match('/^#/',$value)

          I'd just look at $value[0], myself (doesn't that mean you can't have named colours?).

            Write a Reply...