jump to navigation

Creating Tables with Pear’s HTML_Table Package December 20, 2011

Posted by Tournas Dimitrios in PHP.

Thankfully PEAR provides several different packages that take care of different aspects of data presentation and allow programmers expand their scripts to support many formats they would not have been able to develop before . One  of these packages is the HTML_Table , it offers an interface for creating HTML tables . You can work with these tables like a spreadsheet . Instead of working with HTML code and linear adding of cells , you can address and fill cells independend of their position . There is no difference , whether you start with filling a cell at the beginning , in the middle or at the end of the table , a row or a column .

Populating HTML tables with dynamically generated content from a database query is done with looping through the result-set ( usually an array ) . Manually creating this HTML table can be very tedious and the code can become messy as we have to deal with the HTML-tags directly (calculate when to close tags etc ) . In these cases the HTML_Table package comes in very handy as an Object-Oriented wraper for the creation and manipulation of HTML tables .

This article will present a practical example how to populate an table with content from a database query . I assume that PEAR package-manager is already installed on your development/server environment and will go directly to the script . If you haven’t already setup your development environment , read my previous article ” Installing PEAR Package Manager on Wamp2 in Ten Simple Steps ” . Important to note is that you should already have a basic understanding of OOP consents (Object Oriented Programming ) as all PEAR packages are build with this programming paradigm ( style ) .

Installing HTML_Table is simple as executing  ” pear install  – -alldeps HTML_Table-1.8.3 ” from your terminal window . Verify that the installation is completed with ” pear list -a ” , if HTML_Table is on the list you are ready to go .

A basic example of using the HTML_Table package could be :

  require_once 'HTML/Table.php';
$table = new HTML_Table() ;
$table->addRow(array('one','two', 'three', 'four'), null , 'th') ;
$hrAttrs = array('bgcolor' => 'silver');
$table->setRowAttributes(0, $hrAttrs, true);
  for ($i = 0; $i < 8; $i++) {
 $table->addRow(array('one','two','three')) ;

echo $table->toHtml();
echo "<hr>";

The code will produce the following result :

one two three four
one two three
one two three
one two three
one two three
one two three
one two three
one two three
one two three

Now lets present the code for a real-world scenario , populating the HTML-table with content from an Mysql query .

require_once 'HTML/Table.php';
$attrs = array('width' => '450');
$table = new HTML_Table($attrs);
$mysqli = new mysqli("localhost", "root", "", "world");
/* check connection */
if (mysqli_connect_errno()) {
    printf("Connect failed: %s", mysqli_connect_error());
$query = "SELECT Name, Population , Continent , Code FROM Country LIMIT 15 ";
$result = mysqli_query($mysqli, $query);
$val = 0 ;
 while ($row = $result->fetch_object()) {
	//$table->addRow(array( $val , $row->Name , $row->CountryCode , $row->Population)) ;
	$table->addRow(array( $val , $row->Code , $row->Name ,$row->Continent ,  $row->Population)) ;
	$val++ ;
$altRow = array('bgcolor' => '#FBDFA6');
$table->altRowAttributes(1, null, $altRow);
$table->setHeaderContents(0, 0, '');
$table->setHeaderContents(0, 1, 'Code');
$table->setHeaderContents(0, 2, 'Country');
$table->setHeaderContents(0, 3, 'Continent');
$table->setHeaderContents(0, 4, 'Population');
$hrAttrs = array('bgcolor' => 'silver');
$table->setRowAttributes(0, $hrAttrs, true);
$table->setColAttributes(0, $hrAttrs);
echo $table->toHtml();
echo "<hr>";

The code will produce the following result :

n/a Code Country Continent Population
1 NLD Netherlands Europe 15864000
2 ANT Netherlands Antilles North America 217000
3 ALB Albania Europe 3401200
4 DZA Algeria Africa 31471000
5 ASM American Samoa Oceania 68000
6 AND Andorra Europe 78000
7 AGO Angola Africa 12878000
8 AIA Anguilla North America 8000
9 ATG Antigua and Barbuda North America 68000
10 ARE United Arab Emirates Asia 2441000
11 ARG Argentina South America 37032000
12 ARM Armenia Asia 3520000
13 ABW Aruba North America 103000
14 AUS Australia Oceania 18886000

The HTML_Table package is highly configurable , I demonstrated only the basic concepts . The on-line manual has an extensive API description .

Just for completeness : Compare the following code , it implements similar functionality as HTML_Table  but with hand-coded HTML-tags :

// connect to the database

// get the records from the database
if ($result = $mysqli->query("SELECT * FROM world ORDER BY id")){
// display records if there are records to display
if ($result->num_rows > 0){
// display records in a table
echo "<table border='1' cellpadding='10'>";

// set table headers
echo "<tr><th>ID</th><th>First Name</th><th>Last Name</th><th></th><th></th></tr>";

while ($row = $result->fetch_object()){
// set up a row for each record
echo "<tr>";
echo "<td>" . $row->id . "</td>";
echo "<td>" . $row->Country . "</td>";
echo "<td>" . $row->Population . "</td>";
echo "</tr>";

echo "</table>";
// if there are no records in the database, display an alert message
echo "No results to display!";
// show an error if there is an issue with the database query
		echo "Error: " . $mysqli->error;

// close database connection


1. Jopa - March 26, 2012

thank man it helped me a lot!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s