#native_company# #native_desc#

Using PEAR’s HTML_Table Class

By Andy Nortrup
on August 18, 2003

A Simpler Approach to Tables

Within the current incarnation of (X)HTML tables are the building blocks to well organized and attractive designs. They allow the developer an incredible amount of control over the visual representation of information and graphics. However they come with a catch, they are an absolute headache to write into your PHP code. The simple process of querying a database and showing the results in a table becomes a disorganized and ungainly process looking something like this:

//get our query
$connection mysql_connect("localhost""me""password");
$sql "SELECT * FROM users";
$result mysql_query($sql$connection);

//echo our table
echo "<table width="100%" border="1">";
        <th>Phone Number</th>

while($row mysql_fetch_array($result) {
                <td bgcolor="#COCOCO">" 
$row['name'] . "</td>
                <td bgcolor="#COCOCO">" 
$row['email'] . "</td>
                <td bgcolor="#COCOCO">" 
$row['phone'] . "</td>

Clearly this is not an eloquent solution. We either need to use multiple echo statements or as we use the above string one together to make one long statement.

Thankfully there is a solution: the PEAR HTML_Table class.

What is PEAR HTML_Table?

(PHP Extension and Application Repository) (PEAR) is a large body of PHP classes developed to make your life as a PHP programmer easier, by providing a library of prewritten code while also establishing a series of coding standards.

The HTML_Table class makes building tables much simpler, helps you maintain cleaner more readable code and brings the advantages of object oriented code to your work. If you are unfamiliar with working with objects in PHP, I recommend that you read either the relevant section of the PHP documentation or this article will make very little sense. You will also need to make sure that you download and install a copy of the code as well as it’s dependencies, all of which can be found here. Instructions for installing PEAR and it’s various classes is beyond the scope of this document but the PEAR documentation is very good and should be able to answer your questions.