TUTORIAL

Table.php


 by Bertrand Mansion
 http://www.mamasam.com/index3.html
This class was created by :
Adam Daniel and Bertrand Mansion in March 2001 for PEAR (PHP Extension and Add-ons Repository) and its source code can be found at http://cvs.php.net or directly in your PHP distribution.

Table.php allows you to generate HTML table without the need to include HTML tags in your PHP code.
  • PHP code is easier to read.
  • PHP code is shorter.
  • PHP code is easier to maintain.
  • Its usage is very easy.
  • Tables can be modified at any time.
  • The logic is the same as standard HTML editors.


Getting started


Before we start, you need to be sure that HTML/Common.php and HTML/Table.php can be reach by your script (using include paths to PEAR library or a direct require for example). Your script will use Table.php which also uses itself Common.php

With the help of two examples, we are going to explore the functionnalities of this class. At first, we will generate a Table from hard coded values, then with data taken from a database using PEAR DB wrapper.

Table.php allows you to define your own styles for layout. This is what we are going to do now :

  $tableStyle = "bgcolor=#FFFFFF border=1 width=150 cellpadding=1 cellspacing=2 ";
 or
$tableStyle = array("bgcolor"=>"#FFFFFF", "border"=>1, "width"=>150, "cellpadding"=>1, "cellspacing"=>2);

Attributes definition can be made with an array or a string. This functionnality is provided by HTML/Common.php which has a powerful HTML parser.


First Example

To start with, we will define the data that will compose our table :

  $colA = array("field 1", "field 2", "field 3", "", "<b>TOTAL</b>");
$a = 10;
$b = 15;
$c = 8;
$total = $a+$b+$c;
$colB = array($a, $b, $c, "", "<b>".$total."</b>");

You can notice that the values composing our columns are put into arrays. Indeed, lthe addCol() method accepts only arrays of any length. The table size will be automatically modified in consequence.

Finally, let's create the table :

  require_once ("/HTML/Table.php");

$table = new HTML_Table($tableStyle);
$table->addCol($colA);
$table->addCol($colB);
$table->display();

outputs :

field 1 10
field 2 15
field 3 8
   
TOTAL 33

To modify all the cells in a row or in a column, just use setColAttributes or setRowAttributes methods. These methods will overwrite all attributes even the ones already defined :

  $table->setColAttributes(1,"bgcolor=#99CCCC align=right");
$table->setRowAttributes(4,"bgcolor=#669933");

field 1 10
field 2 15
field 3 8
   
TOTAL 33

You will notice that rows and columns are defined by pointers starting from 0 and not 1. This is to be compliant with PHP arrays.

Now, we want to modify the empty cell, span it and fill it with an horizontal rule. This is how we can do it :

  $table->setCellAttributes(3,0,"colspan=2");
$table->setCellContents(3,0,"<hr noshade size=\"1\">");

field 1 10
field 2 15
field 3 8

TOTAL 33

Instead of using the display() method, we will use the toHTML() method which will allow us to keep rendered HTML code in a string variable for later use :

  $tableA = $table->toHTML();

We have seen how to generate a table using hard coded values. We have also seen that it was possible to change the layout and content of a table row, column or cell even if these were already defined before.

When we add a row, a column or a cell which does not exist, the table is resized accordingly.

• This behavior is managed with the autoGrow variable.

When a cell is empty, it can be automatically filled with a content of your choice, nobackspace for example (&nbsp;).

• This behavior is managed with the autoFill variable.


Second Example

In this second example, we will obtain our data directly from the database. This is how to do it quickly with PEAR DB layer :

  $db = DB::connect("mysql://$dbuser:$dbpass@$dbhost/$dbname");
$sql = "SELECT NAME, FIRSTNAME FROM EMPLOYEES";
$result = $db->query($sql);

With this query, we get all NAME, FIRSTNAME from the EMPLOYEES table. We will now display these values in a HTML table using HTML/Table.php. First, let's define our columns headers :

  $colHeaders = array("Name", "Firstname", "Extra");

$tableB = new HTML_Table("border=0 cellpadding=1 cellspacing=2 width=300");
$tableB->addRow($colHeaders);

Last minute tip
: To create a header row quickly, just use...
  $headerStyle = "bgcolor=blue nowrap";
$colHeaders = array("Name", "Firstname", "Extra");

$tableB->addRow($colHeaders, $headerStyle, "TH");

Then, we will add rows to our table, one by one, with no care for layout and styles:

  while ($row = $result->fetchrow(DB_FETCHMODE_ASSOC)) {
    $tableB->addRow(array($row["NAME"],$row["FIRSTNAME"]));
}
$db->disconnect();

We want to add a last row to this table with today's date in the last cell :

  $tableB->addRow(array("","",date('Ymd'));

Name Firstname Extra
Picsou Uncle  
Harry Inspector  
Mouse Mickey  
Duck Donald  
Gates William  
Disney Walter  
Smith John  
Bush Georgie  
McDonald Ronald  
    20010315

Now we can add layout and styles to our table. We will make our headers look different from the other rows and we will make each row look different from its predecessor.

 

$headerStyle = "bgcolor=blue nowrap";
$row1Style = "bgcolor=#FFFFCC align=left";
$row2Style = array ("bgcolor" => "#FFFF99", "align"=>"left");

$tableB->setRowType(0,"TH");
$tableB->altRowAttributes(1,$row1Style,$row2Style);
$tableB->updateColAttributes(2,"align=right");



Name Firstname Extra
Picsou Uncle  
Harry Inspector  
Mouse Mickey  
Duck Donald  
Gates William  
Disney Walter  
Smith John  
Bush Georgie  
McDonald Ronald  
    20010315

You have certainly noticed that we used updateColAttributes instead of setColAttributes. Indeed, 'update' won't overwrite attributes already set if they are not defined in the array or string arguments.

Now, we would like to change the attributes of the last row. To acheive this, we need to know how many rows compose our table :

  $rowCount = $tableB->getRowCount();
$tableB->updateRowAttributes($rowCount-1, "bgcolor=#CCCCCC");

which outputs :

Name Firstname Extra
Picsou Uncle  
Harry Inspector  
Mouse Mickey  
Duck Donald  
Gates William  
Disney Walter  
Smith John  
Bush Georgie  
McDonald Ronald  
    20010315


And last but not least, it is also possible to add a table in a table cell, like this:

  $tableB->setCellContents($rowCount-1,2,$tableA);

Name Firstname Extra
Picsou Uncle  
Harry Inspector  
Mouse Mickey  
Duck Donald  
Gates William  
Disney Walter  
Smith John  
Bush Georgie  
McDonald Ronald  
   
field 1 10
field 2 15
field 3 8

TOTAL 33
Voilà ! You seen most of the class functionnalities. Now we encourage you to have a look at the source code in order to learn about the other ones.