HTML_Table offers an interface for create a HTML table. You can work with the table like a spreadsheet. Instead of working with HTML code and linear adding of cells, you can address and fill cells independend of there position. There is no different, whether you start with fill a cell at the beginning, in the middle or at the end of the table, a row or a column.
autoGrow flagNormaly, you would define a table with a constant number of rows and columns. But sometimes, you does not know, how many rows or columns you need: ie. transforming user input or the result of a database query to an HTML table.
In this case, you should
enable
the autoGrow feature.
In this mode, HTML_Table adds new
rows or columns automatically, if you use a cell address located in a
not existing row or column.
autoFill value
If you create a table of data, sometimes you have not to fill
all cells with different values. Perhaps you do not know the
value for a cell, or you want to insert a default value - ie. retrieving
data about users. Not every user has a mobile, a email address etc.,
in this case, an "n/a" should be inserted into that
specific cell.
So, simply
define "n/a" as autoFill value and fill only
the cells where data exist. You need not to fill every cell; unfilled
cells contain automatically an "n/a".
Our HTML table to create should contain the following data:
<?php
$data = array(
'0' => array('Bakken', 'Stig', '', 'stig@example.com'),
'1' => array('Merz', 'Alexander', 'alex.example.com', 'alex@example.com'),
'2' => array('Daniel', 'Adam', '', '')
);
?>
Let us now start by creating a new instance of
HTML_Table. The table
should be 600 pixel wide. We do not know
the quantity of the data to insert into the table -
so we enable the autoGrow feature.
Unfilled cells should contain an "n/a".
<?php
require_once 'HTML/Table.php';
$attrs = array('width' => '600');
$table = new HTML_Table($attrs);
$table->setAutoGrow(true);
$table->setAutoFill('n/a');
?>
Setting table attributes is also possible by using the setAttributes() method. Therefore, the example from above can also be written as:
<?php
require_once 'HTML/Table.php';
$attrs = array('width' => '600');
$table = new HTML_Table();
$table->setAttributes($attrs);
// [...]
?>
Now process every data entry. Here we use also the alternate feature of HTML_Table. Every second row will be colored red.
<?php
for ($nr = 0; $nr < count($data); $nr++) {
$table->setHeaderContents($nr+1, 0, (string)$nr);
for ($i = 0; $i < 4; $i++) {
if ('' != $data[$nr][$i]) {
$table->setCellContents($nr+1, $i+1, $data[$nr][$i]);
}
}
}
$altRow = array('bgcolor' => 'red');
$table->altRowAttributes(1, null, $altRow);
?>
Now we want to define the cells in the first row and column as header cells. It should looks like a spreadsheet application, so we want to use the color "silver" as the background colour for each header cell. The first row contains a column headline, the first column the number of the data set row.
<?php
$table->setHeaderContents(0, 0, '');
$table->setHeaderContents(0, 1, 'Surname');
$table->setHeaderContents(0, 2, 'Name');
$table->setHeaderContents(0, 3, 'Website');
$table->setHeaderContents(0, 4, 'EMail');
$hrAttrs = array('bgcolor' => 'silver');
$table->setRowAttributes(0, $hrAttrs, true);
$table->setColAttributes(0, $hrAttrs);
?>
It is done! Our table is finished, now we can output the table as HTML code.
<?php
echo $table->toHtml();
?>
The output will look like this:
If you want to divide your tables into thead,
tfoot and tbody groups, you need to
get table objects using
getHeader(),
getFooter(),
and getBody(),
which you can then use like the normal table object.
<?php
$table = new HTML_Table();
$head =& $table->getHeader();
$foot =& $table->getFooter();
$body =& $table->getBody();
$head->setCellContents(...);
$body->setCellContents(...);
echo $table->toHtml();
?>
In this example, there is no content set for the tfoot
group. Therefore, only thead and tbody
will be rendered.
The rendering order is
thead, thentfootand as the last grouptbody. This is not a bug but intended behaviour because that's the way it is defined in the (X)HTML Standard.
Since release 1.8.0
getBody()
and several other methods like
setCellAttributes()
accept an optional numeric parameter $body that allows
you to generate multiple tbody groups in your table. A
new group can be generated by using
addBody()
or, if the
autoGrow
feature is enabled, by using a new number in one of the mentioned method
calls.