Page 488 - Beginning PHP 5.3
P. 488
Part III: Using PHP in Practice
Creating HTML Tables with the HTML_Table
Package
Now that you ’ ve installed and used a basic PEAR package, try something a little more involved. HTML_
Table is a package that lets you generate HTML tables programmatically, rather than directly outputting
HTML markup yourself. Not only does this result in neater PHP code, but it frees you up to concentrate
on the PHP side of things, without having to fiddle with HTML. It also makes it easy for your script to
go back and change the table at any time before it ’ s rendered; for example, you can add a new row or
column to the table at a later date.
The first thing to do is install the package. HTML_Table depends on another PEAR package, HTML_
Common , to do its work, so you ’ ll need to make sure HTML_Common is installed too. The easiest way to do
that is to include the - - alldeps option when installing HTML_Table .
Go ahead and install the HTML_Table and HTML_Common packages using the technique appropriate for
your setup, as described earlier in the chapter. For example:
$ pear install --alldeps HTML_Table
downloading HTML_Table-1.8.2.tgz ...
Starting to download HTML_Table-1.8.2.tgz (16,988 bytes)
......done: 16,988 bytes
downloading HTML_Common-1.2.4.tgz ...
Starting to download HTML_Common-1.2.4.tgz (4,519 bytes)
...done: 4,519 bytes
install ok: channel://pear.php.net/HTML_Common-1.2.4
install ok: channel://pear.php.net/HTML_Table-1.8.2
$
You can find documentation for HTML_Table on the PEAR Web site ( http://pear.php.net/package/
HTML_Table/docs ), and there should be a couple of example scripts that show how to use HTML_Table
in the doc/HTML_Table/docs folder in your PEAR path. Here ’ s a quick overview of HTML_Table ’ s
most important methods:
Method Description
HTML_Table( $ attrs , $ tabOffset , The HTML_Table constructor. All three arguments
$ useTGroups ) are optional.
$ attrs is an array of HTML attributes (as name/
value pairs) to add to the opening < table > tag.
$ tabOffset specifies how many tabs to indent the
markup for the table (the default is zero).
$ useTGroups specifies whether to use thead ,
tfoot , and tbody elements in the table (the default
is false , which means they ’ re not used).
450
9/21/09 9:14:49 AM
c15.indd 450 9/21/09 9:14:49 AM
c15.indd 450