Using PEAR's HTML_Table Class
Andy Nortrup
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:
<?php
//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\">";
echo "<tr>
<th>Name</th>
<th>E-mail</th>
<th>Phone Number</th>
<tr>";
while($row = mysql_fetch_array($result) {
echo "<tr>
<td bgcolor=\"#COCOCO\">" . $row['name'] . "</td>
<td bgcolor=\"#COCOCO\">" . $row['email'] . "</td>
<td bgcolor=\"#COCOCO\">" . $row['phone'] . "</td>
</tr>";
}
echo "</table>";
?>
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.
[ Next Page ]
| Comments: | ||
| RE: use tables for layout is completely wrong | theprogramer | 12/29/03 10:45 |
| HeadBreakers | dLogica | 12/20/03 21:39 |
| code / content | Mundi King | 10/25/03 00:23 |
| nothing wrong with this aproach | mike howell | 10/08/03 14:02 |
| Why not use PEAR::DB | Tjerk Valentijn | 10/08/03 11:02 |
| RE: use tables for layout is completely wrong | Gold | 10/08/03 02:43 |
| RE: why not just use \n? | Kenneth H Scott | 09/17/03 10:36 |
| use tables for layout is completely wrong | Vicente Reig | 09/01/03 10:25 |
| html tables. | Phillip H Paulson | 08/26/03 08:14 |
| why not just use \n? | Ben V. | 08/19/03 00:07 |
|
If you are looking for help, please post on the appropriate forum here. Your questions will be answered much more quickly. | ||

