PHP Table Class Documentation

This page provides instructions for the PHP Table Class and shows how to use its methods for generating HTML tables. An example demonstrates, showing both the source code and the resulting table.

To use the PHP Table Class, you include or require the class file in your documents as follows:


Next, create an instance of the class. The arguments passed to the constructor are optional. They include id and class with any additional attributes passed in an associative array. Here is an example of how you might begin to create a table using the PHP Table Class:

// arguments: id, class
$tbl = new HTML_Table('', 'demoTbl');

Or, for an old-school HTML4 table with border, cellpadding, and cellspacing attributes:

// arguments: id, class,
// associative array of additional attributes
$tbl = new HTML_Table('', 'demoTbl', array('border'=>1, 'cellpadding'=>6, 'cellspacing'=>0) );

Now you can begin to add table elements. We will start by describing how you add table cells and rows since that may be all that most users of the code will need. Then we will describe and demonstrate methods for including less common table elements.

Adding Table Rows and Cells

The addRow method is used to add a row to the table, typically with no arguments:

// optional arguments: class,
// associative array of additional attributes

The addCell method is used to add td and th elements to the row just added. For example, the following adds a header cell (th):

// arguments: cell content, class (optional),
// type (default is 'data' for td, pass 'header' for th)
// associative array of additional attributes (optional)
$tbl->addCell('Product', 'first', 'header');

The following adds a data cell (td) with a colspan attribute included using the optional associative array argument:

$tbl->addCell('All so very yummy!', 'foot', 'data', array('colspan'=>3) );


The PHP Table Class provides a method for placing your rows and cells in thead, tfoot and tbody sections rather than just in the table element itself. An addSection method is provided for this purpose. Pass 'thead', 'tfoot', or 'tbody' to the method:

// thead
    $tbl->addCell('Product', 'first', 'header');
    // ... rest of row's cells

Then add rows and cells to the section just added. The table class collects these elements in arrays as you add them and then builds your table when you are ready to display it. An example in the download file demonstrates with thead, tfoot, and tbody sections. ^


You can add a caption to your table as follows:

// arguments: caption text, class
// associative array of additional attributes (optional)
$tbl->addCaption('Dessert Favorites', 'cap');

COLGROUP and COL Elements

COLGROUP and/or COL elements can be added too, for example:

// optional arguments (for both methods):
// span, class, associative array of additional attributes
$tbl->addCol(1, 'title');
$tbl->addCol(2, 'prices');

Displaying the Table

Once you have added all the elements to your table, call the display method which will return the resulting table in a string:

echo $tbl->display();

Note: The table class includes only very minimal error checking. For example, the table class will not verify that your elements are in the correct order, or that you have consistent numbers of columns, or that your attributes are valid for your document type. So for best results, validate your resulting documents when using the table class.

Demo, Download, and More

View the PHP Table Class file as well as an example demonstrating its use. The download file provides the PHP Table Class file and example documents.

See the PHP Form Class which is well suited for use with the PHP Table Class. See also the PHP Order Form for a demonstration of the PHP Table Class used in combination with the PHP Form Class.

Back to top