#native_company# #native_desc#
#native_cta#

Using PEAR’s HTML_Table Class Page 4

By Andy Nortrup
on August 18, 2003

A Few Last Tricks
And before we close here are few last tricks that come along with this very useful class.

Alternating Rows

One of the most common tasks when creating tables containing database output is alternating colors to make it easier to read. The HTML_Table class makes this easy to do, using the altRowAttributes function. Using our example from before the function works like this:

<?php
//..database retrieval... 

while($row mysql_fetch_array($result)) {
        
$contents = array($row['name'], $row['email'], $row['phone']);
        
$table->addRow($contents$attributes);
}
//alternate between 2 colors
$attributes1 bgcolor #336699;
$attributes2 bgcolor #COCOCO;
$table->altRowAttributes(1$attributes1$attributes2);

$table->display();
?>

This will result in the rows switching colors with every row, starting with the second (as our first row is a header).
Outputting to HTML
Another useful feature is the toHtml() function which works much the same as the display function but instead returns the html rather than outputting directly. This is useful if you are building the table in a function or need to display the table in a place other than where you built it.
In parting, I hope that I have shown you the benefits of the HTML_Table class. There are several other functions which I felt were beyond the scope of this introduction but I very much encourage you to browse the code yourself and find them. I think you will find the documentation to be very well done.

1
|
2
|
3
|
4