PHP Class for Generating HTML Tables

The HTML_Table class is a basic PHP class for generating html tables. It is useful for separating logic from markup and keeping code cleaner when outputing mysql query results or displaying elements of arrays. It works nicely in combination with the Form Class.

The HTML_Table class makes use of two additional very small classes to create table rows and cells. These are included in the html_table.class.php file. View code for the class and an example.

Setup

To use the table class you include or require it's file in your documents as follows:

require('includes/html_table.class.php');

With the file in place, the next step is to create an instance of the class:

$myTbl = new HTML_Table();

When you create an instance you begin to generate a table. Some of the common attributes of tables are optional arguments of the constructor.

HTML_Table Arguments:

$id (string):
Unique id to be assigned to the table element (optional)
$klass (string):
Class name to be attached to the table (optional)
$border (integer):
Border attribute of table element (optional, default is 0)
$cellspacing (integer):
Table's cellspacing attribute - amount of spacing between table cells (optional, default is 2)
$cellpadding (integer):
Table's cellpadding attribute - amount of padding around table cell content (optional, default is 0)
$attr_ar (array):
Associative array of additional attributes (optional)

More information about the $attr_ar argument is available below.

Adding Table Rows and Cells

The table class contains methods for adding rows and cells to the table, including header cells (<th>). The class does not currently support addition of lesser used elements of tables such as thead, tfoot, caption, colgroup, etc.

Add a row to the table you have created using:

$myTbl->addRow();

addRow Arguments:

$klass (string):
Class name to be attached to the table row (optional)
$attr_ar (array):
Associative array of additional attributes (optional)

The addCell method is used to add td and th elements to the current row.

$myTbl->addCell('Content for the table cell');

addCell Arguments:

$data (string):
Content to be displayed in table cell.
$klass (string):
Class name to be attached to the table cell (optional)
$type (string):
Type of table cell, data (<td>) or header (<th>). Default: 'data'
$attr_ar (array):
Associative array of additional attributes (optional)

addAttributes Method

The optional $attr_ar argument specified for the HTML_Table constructor and the addRow and addCell methods all use the addAttributes method to provide the capability to add attributes other than those passed in named arguments. This provides increased flexibility to the code without the necessity for a long list of named arguments most of which would not generally be used.

For example, if you have a table cell which you wish to span more than one column you can specify the colspan attribute using this approach:

$tbl->addCell('Content for wide cell', 'cellClass', 'data', array('colspan'=>3) 

The Form Class also provides an addAttributes method which you can see demonstrated there and in the Order Form code.

Displaying the Table

Unlike the form class, methods for adding rows and cells to the table do not generate strings. The markup for the table is built and output using the display method. This seeming inconsistency is the means by which the table and form classes can be used in combination.

The display method has no arguments. It outputs a string which can be immediately echoed or assigned to a string and returned from a function.

echo $myTbl->display();

An Example Table

See an example demonstrating use of the table class to generate a table for display of PHP array content.

Back to top