|
 |
|
HTML_Table Module
Oracle Tips by Burleson Consulting |
HTML tables are the most natural choice for the
presentation of relational tables. Both types of objects have rows
and columns, both have headers and both are organized similarly. In
this book, there are several functions that perform this mapping
from the realm of relational databases into the realm of HTML. This
is the reason for a brief introduction to the PEAR HTML_Table
module, despite the fact that it does not have anything to do with
databases.
As the name suggests, the HTML_Table module is a
PEAR module that helps with HTML_Tables. This section is a brief
introduction to the HTML_Table module, describing only a few of the
most typical uses.
So, in order to make the HTML_Table module
available to scripts, the HTML/Table.php file must be included by a
command like the following:
require_once('HTML/Table.php');
The next thing to do is to create a table. All
PEAR modules are object oriented using classes, and the HTML_Table
module is no exception. A table is conveniently created by the
HTML_Table class constructor such as the following:
$tbl_attrs=array('rules'=>"rows,cols",
'border'=>3);
$table=new HTML_Table($tbl_attrs);
A table constructor creates the “<table>” tag
and needs the table tag attribute as a constructor argument. Tags
are added as an associative array. The table is now created.
A fixed dimensions table with a fixed number of
rows and columns can now be created, but for dealing with SQL objects
it is usually not desirable to do so. The table should be made dynamic
and enabled to grow as needed. This is accomplished by using the
following call:
$table->setAutoGrow(TRUE);
HTML_Table treats HTML tables like matrices,
addressing each cell by row and column numbers. Rows and columns are
numbered from 0 onward. Before proceeding any further, the array
from the section regarding multiple SQL executions should be viewed
again:
$data=array(
array(1,'a'),
array(2,'b'),
array(3,'c'),
array (4,'d'));
Speaking mathematically, the array $data
is a matrix 4x2, which means that it is ideal for demonstrating HTML
tables.
To look pretty, a table must have a header. The
following shows how to create one:
$table->setHeaderContents
(0,0,'NUMBER');
$table->setHeaderContents
(0,1,'CHARACTER');
The setHeaderContents($row,$col,
$CONTENTS) function sets the cell with coordinates ($row,$col)
to the value in the $CONTENTS variable and encloses with <th></th>
tags.
Now the attributes to the existing cells can be
applied:
$table->setCellAttributes(0,0,array('align'=>'center'));
$table->setCellAttributes(0,1,array('align'=>'center'));
The column titles are now located at the center
of each cell.
Since there is a table with a pretty header, data
can be added into the mix:
for ($rowNum=0;$rowNum<4;$rowNum++) {
for ($colNum=0;$colNum<2;$colNum++) {
$table->setCellContents($rowNum+1,$colNum,$data[$rowNum][$colNum]);
$table->setCellAttributes($rowNum+1,$colNum,array('align'=>'center'));
}
}
Data is added by the
setCellContents($row,$col,$CONTENTS) method. This method places
the value of the $CONTENTS variable into the cell with coordinates ($row,$col).
Please note that rows are being added, starting with row number 1,
because row number 0 is the header row, defined earlier. The time
has come to show this little masterpiece to the world, by using the
following command:
echo $table->toHTML();
The toHTML() method returns the HTML
table, formatted as a text string. Here is the full text of this
example:
<?php
require_once('HTML/Table.php');
$data=array(array(1,'a'),
array(2,'b'),
array(3,'c'),
array (4,'d'));
$tbl_attrs=array('rules'=>"rows,cols",
'border'=>3);
$table=new HTML_Table($tbl_attrs);
$table->setAutoGrow(TRUE);
$table->setHeaderContents (0,0,'NUMBER');
$table->setHeaderContents(0,1,'CHARACTER');
$table->setCellAttributes(0,0,array('align'=>'center'));
$table->setCellAttributes(0,1,array('align'=>'center'));
for ($i=0;$i<4;$i++) {
for ($j=0;$j<2;$j++) {
$table->setCellContents($i+1,$j,$data[$i][$j]);
$table-
>setCellAttributes($i+1,$j,array('align'=>'center'));
}
}
echo $table->toHTML();
?>
When displayed in a browser, this table looks like
the following:
Instead of setting each cell separately and then
setting the cells attribute, the whole row, together with the
attributes can be added. This is done by changing the nested loops
paragraph in the example like the following:
foreach ($data as $val) {
$table->addRow($val,array('align'=>'center'));
}
The addRow() method has the syntax as
follows:
addRow($val_array,$attr_array,$row_type)
The $val_array contains the array of
values that populates the row being added. The $attr_array
contains the attributes for all cells in the row being added, and
$row_type contains either 'TH' for header rows or 'TD' for data
rows. The default type is 'TD', the data row. Now the example can be
shorten even further:
<?php
require_once('HTML/Table.php');
$data=array(array(1,'a'),
array(2,'b'),
array(3,'c'),
array (4,'d'));
$tbl_attrs=array('rules'=>"rows,cols",
'border'=>3);
$table=new HTML_Table($tbl_attrs);
$table->setAutoGrow(TRUE);
$table->addRow(array('NUMBER','CHARACTER'),
array('align'=>'center'),
'TH');
foreach ($data as $val) {
$table->addRow($val,array('align'=>'center'));
}
echo $table->toHTML();
?>
In the table, there are no empty cells, so the
“auto fill” value for the empty cells does not need set, which is
usually needed when dealing with cursors and SQL. This is
accomplished by a command like the following:
$table->setAutoFill('N/A');
The above section was a brief introduction to
the HTML_Table PEAR module basics. Those interested in all other
methods and capabilities of the HTML_Table class can take a look in
the online manual which is much more comprehensive.
SEE CODE DEPOT FOR FULL SCRIPTS
http://www.rampant-books.com/book_2005_2_php_oracle.htm
 |
Expert Remote DBA
BC is America's oldest and largest Remote DBA Oracle support
provider. Get real Remote DBA experts, call
BC Remote DBA today. |
 |
|