dbHtmlTable examples

This page demonstrates how to use the dbHtmlTable PHP class. The examples use the following CSS:

caption { text-align:center; color:#00a; background:#eef; font-weight:bold; padding:10px; border:1px solid #aaa; }
th { background:#ddf; text-align:left; }
th,td { padding:2px 5px; }
th.rn,td.rn { color:#888 !important; background:#fff; border:none; border-right:1px solid #aaa; }
tr.odd { background:#f8f8f8; }
tr.even { background:#f0f0f0; }
tr.odd td { color:#444; }
tr.even td { color:#222; }
tr.divider td { border-bottom:1px solid #aaa; }
.grp { font-size:16px; font-weight:bold; color:#060; border-bottom:1px solid #ddd; padding-top:10px; }

Simple table

First we create an instance of the class giving it an SQL query string, then call createTable() to create and display the table:

require_once('dbHtmlTable.class.php');
$HtmlTable = new dbHtmlTable('select Id, Title, StartDate, EndDate, ImageFilename, Tmst from mytable order by Title');
$HtmlTable->createTable();
Id Title StartDate EndDate ImageFilename Tmst
5 Amazing waterfall 2010-03-13 18:05:22 2010-09-13 18:05:22   2010-03-13 18:05:22
3 Another one 2010-03-10 16:53:00 2010-09-10 16:51:00 checkershadow.jpg 2010-03-17 17:27:25
2 Digital camera 2010-03-10 16:53:00 2010-09-10 16:19:00 still.gif 2010-03-19 00:18:35
4 Permanent item 2010-03-12 01:34:00     2010-03-17 16:47:26
8 Rose Tyler 2010-03-16 19:53:40 2010-09-16 18:53:40 avi.gif 2010-03-16 18:53:40
1 Striped pyjamas 2010-03-10 11:51:00 2010-03-11 01:00:00 banana.jpg 2010-03-17 22:16:53
6 This is (not really) interesting! 2010-03-17 00:00:00 2010-09-16 18:49:00   2010-03-16 22:16:56
7 Zoological anomaly 2020-03-16 19:51:00 2010-09-16 18:51:00 girant.gif 2010-03-16 19:12:57

Simple as that! You'll notice that the headings are already styled; this is due to the th declarations in our CSS.

Add a caption

$HtmlTable->setOption('Caption', 'This is my data');
$HtmlTable->createTable();
This is my data
Id Title StartDate EndDate ImageFilename Tmst
5 Amazing waterfall 2010-03-13 18:05:22 2010-09-13 18:05:22   2010-03-13 18:05:22
3 Another one 2010-03-10 16:53:00 2010-09-10 16:51:00 checkershadow.jpg 2010-03-17 17:27:25
2 Digital camera 2010-03-10 16:53:00 2010-09-10 16:19:00 still.gif 2010-03-19 00:18:35
4 Permanent item 2010-03-12 01:34:00     2010-03-17 16:47:26
8 Rose Tyler 2010-03-16 19:53:40 2010-09-16 18:53:40 avi.gif 2010-03-16 18:53:40
1 Striped pyjamas 2010-03-10 11:51:00 2010-03-11 01:00:00 banana.jpg 2010-03-17 22:16:53
6 This is (not really) interesting! 2010-03-17 00:00:00 2010-09-16 18:49:00   2010-03-16 22:16:56
7 Zoological anomaly 2020-03-16 19:51:00 2010-09-16 18:51:00 girant.gif 2010-03-16 19:12:57

Alternating background colours

Now we'll get rid of the cell borders, and add alternating background colours:

$HtmlTable->setOptions(array(
    'OddRowClass'=>'odd', 'EvenRowClass'=>'even', 'TableAttr'=>'border="0" cellspacing="0"'
    ));
$HtmlTable->createTable();
echo '<h3>'.$HtmlTable->getRowCount().' rows listed</h3>';
This is my data
Id Title StartDate EndDate ImageFilename Tmst
5 Amazing waterfall 2010-03-13 18:05:22 2010-09-13 18:05:22   2010-03-13 18:05:22
3 Another one 2010-03-10 16:53:00 2010-09-10 16:51:00 checkershadow.jpg 2010-03-17 17:27:25
2 Digital camera 2010-03-10 16:53:00 2010-09-10 16:19:00 still.gif 2010-03-19 00:18:35
4 Permanent item 2010-03-12 01:34:00     2010-03-17 16:47:26
8 Rose Tyler 2010-03-16 19:53:40 2010-09-16 18:53:40 avi.gif 2010-03-16 18:53:40
1 Striped pyjamas 2010-03-10 11:51:00 2010-03-11 01:00:00 banana.jpg 2010-03-17 22:16:53
6 This is (not really) interesting! 2010-03-17 00:00:00 2010-09-16 18:49:00   2010-03-16 22:16:56
7 Zoological anomaly 2020-03-16 19:51:00 2010-09-16 18:51:00 girant.gif 2010-03-16 19:12:57

8 rows listed


More formatting and row numbers

Next we remove the caption, set some date formats and add row numbering:

$HtmlTable->setOption('Caption', '');
$HtmlTable->setOption('FormatAsDate', 'StartDate,Tmst,EndDate=j M Y H:i');
$HtmlTable->setOption('center', 'Id');
$HtmlTable->setOption('rownumber', '&nbsp;');
$HtmlTable->createTable();
  Id Title StartDate EndDate ImageFilename Tmst
1 5 Amazing waterfall 13 Mar 2010 13 Sep 2010 18:05   13 Mar 2010
2 3 Another one 10 Mar 2010 10 Sep 2010 16:51 checkershadow.jpg 17 Mar 2010
3 2 Digital camera 10 Mar 2010 10 Sep 2010 16:19 still.gif 19 Mar 2010
4 4 Permanent item 12 Mar 2010     17 Mar 2010
5 8 Rose Tyler 16 Mar 2010 16 Sep 2010 18:53 avi.gif 16 Mar 2010
6 1 Striped pyjamas 10 Mar 2010 11 Mar 2010 01:00 banana.jpg 17 Mar 2010
7 6 This is (not really) interesting! 17 Mar 2010 16 Sep 2010 18:49   16 Mar 2010
8 7 Zoological anomaly 16 Mar 2020 16 Sep 2010 18:51 girant.gif 16 Mar 2010

The row numbers look like they're outside the table, due to our CSS styling for classname rn

Empty and null fields

Now we'll identify empty and null values, add a dividing line every 5th row and include the field names at the bottom as well as the top of the table:

$HtmlTable->setOptions(array(
    'EmptyValue' => '<span style="color:#888;">(empty)</span>',
    'NullValue'  => '<span style="color:#888;">-null-</span>',
    'TrEvery'    => 'divider@5',
    'ShowFooter' => true
    ));

$HtmlTable->createTable();
  Id Title StartDate EndDate ImageFilename Tmst
1 5 Amazing waterfall 13 Mar 2010 13 Sep 2010 (empty) 13 Mar 2010
2 3 Another one 10 Mar 2010 10 Sep 2010 checkershadow.jpg 17 Mar 2010
3 2 Digital camera 10 Mar 2010 10 Sep 2010 still.gif 19 Mar 2010
4 4 Permanent item 12 Mar 2010 -null- (empty) 17 Mar 2010
5 8 Rose Tyler 16 Mar 2010 16 Sep 2010 avi.gif 16 Mar 2010
6 1 Striped pyjamas 10 Mar 2010 11 Mar 2010 banana.jpg 17 Mar 2010
7 6 This is (not really) interesting! 17 Mar 2010 16 Sep 2010 (empty) 16 Mar 2010
8 7 Zoological anomaly 16 Mar 2020 16 Sep 2010 girant.gif 16 Mar 2010
  Id Title StartDate EndDate ImageFilename Tmst

Re-using the class object

We can continue to use the same class object for a completely new database query:

$sql='select TypeDescn as Kind, Id, Title, ImageFilename as Image, Tmst from mytable'
    .' inner join myreftable on mytable.TypeCd=myreftable.TypeCd'
    .' order by mytable.TypeCd';

$HtmlTable->setQuery($sql);
$HtmlTable->setOption('emptyvalue', '[NONE]');
$HtmlTable->createTable();

When you change the query by calling setQuery(), the options that apply to specific fields will revert to their defaults, while more general options will be retained.

  Kind Id Title Image Tmst
1 Normal type 5 Amazing waterfall [NONE] 2010-03-13 18:05:22
2 Normal type 6 This is (not really) interesting! [NONE] 2010-03-16 22:16:56
3 Normal type 3 Another one checkershadow.jpg 2010-03-17 17:27:25
4 Normal type 2 Digital camera still.gif 2010-03-19 00:18:35
5 Normal type 1 Striped pyjamas banana.jpg 2010-03-17 22:16:53
6 Normal type 8 Rose Tyler avi.gif 2010-03-16 18:53:40
7 Unusual type 7 Zoological anomaly girant.gif 2010-03-16 19:12:57
8 Rare type 4 Permanent item [NONE] 2010-03-17 16:47:26
  Kind Id Title Image Tmst

Grouping by first column

If the first column is repeating (as it is in the above example), we can make it appear only when it changes:

$HtmlTable->setOption('GroupFirst', true);
$HtmlTable->setOption('GroupHeadCount', 3);
$HtmlTable->setOption('TrEvery', '');
$HtmlTable->createTable();
      Title Image Tmst
 Normal type
1   5 Amazing waterfall [NONE] 2010-03-13 18:05:22
2   6 This is (not really) interesting! [NONE] 2010-03-16 22:16:56
3   3 Another one checkershadow.jpg 2010-03-17 17:27:25
4   2 Digital camera still.gif 2010-03-19 00:18:35
5   1 Striped pyjamas banana.jpg 2010-03-17 22:16:53
6   8 Rose Tyler avi.gif 2010-03-16 18:53:40
 Unusual type
7   7 Zoological anomaly girant.gif 2010-03-16 19:12:57
 Rare type
8   4 Permanent item [NONE] 2010-03-17 16:47:26
      Title Image Tmst

Here, the repeating first column is styled using classname grp.