No public Twitter messages.

Table helper

July 9th, 2009 by Ray in PHP/MySQL, Solar | Tags: , | No Comments

Thought I would give you all a sneak peek at the table helper class I am working on. I’m not a big fan of table helper classes but I kind of found a use for a helper such as this for one of my projects–plus it’s neat.

$table = new Table();

$table->setCssId('my_table');
$table->addCssClasses('default big_text');

$thead = $table->addGroup('thead')->addCssClass('thead_tag_css_class');

$row1 = $thead->addRow()->addCssClass('sample_css_heading');
$row1->addHeadingCell('Name');
$row1->addHeadingCell('Sex');
$row1->addHeadingCell('Position');
$row1->addHeadingCell('Top 2 favorite colors')->colSpan(2);

// rows that are not instantiated from addGroup() are added to tbody
$row2 = $table->addRow();
$row2->addDataCell('Adam Smith');
$row2->addDataCell('Male');
$row2->addDataCell('Economist');
$row2->addDataCell('Black');
$row2->addDataCell('White');

$tfoot = $table->addGroup('tfoot');

// empty data cells make sure we are compliant
// empty cells default to  
$row3 = $tfoot->addRow();
$row3->addDataCell('example footer data');
$row3->addDataCell();
$row3->addDataCell();
$row3->addDataCell();
$row3->addDataCell();

// we can also just call print $table;
print $table->display()

Produces…

<table id="my_table" class="default big_text">
    <thead class="thead_tag_css_class">
            <tr class="sample_css_heading">
        <th>
            Name
        </th>
        <th>
            Sex
        </th>
        <th>
            Position
        </th>
        <th colspan="2">
            Top 2 favorite colors
        </th>
    </tr>
    </thead>
    <tfoot>
            <tr>
        <td>
            example footer data
        </td>
        <td>
            &nbsp;
        </td>
        <td>
            &nbsp;
        </td>
        <td>
            &nbsp;
        </td>
        <td>
            &nbsp;
        </td>
    </tr>
    </tfoot>
    <tbody>
            <tr>
        <td>
            Adam Smith
        </td>
        <td>
            Male
        </td>
        <td>
            Economist
        </td>
        <td>
            Black
        </td>
        <td>
            White
        </td>
    </tr>
    </tbody>
</table>

I still have some refactoring/testing/fixing to do but I hope to release this package soon under the GPLv3. I have also been throwing the idea around of being able to have this class generate CSS tables–e.g. fetchCssTable() and displayCssTable() (something like that) that would return both a CSS stylesheet and the proper div tags for the table itself.

Once I’m happy with this package I will also convert it over to Solar since it lacks this type of helper.


Leave a Reply