Location: PHPKode > scripts > dbHtmlTable > dbHtmlTable.class.extend.php
<?php
/*
    dbHtmlTable.class.extend
    
    This file is entirely optional. It shows how you could extend 
    the dbHtmlTable class to implement themes, or use a data 
    source other than MySQL.
*/

require_once('dbHtmlTable.class.php');

/*
    EXAMPLE 1
    This example adds a new method setTheme(), allowing 
    you to create tables with a predetermined set of options. 
    
    Sample usage:
        require_once('dbHtmlTable.class.extend.php');
    
        $HtmlTable = new MyDbHtmlTable('select * from mytable');
        $HtmlTable->setTheme('MyTheme');
        $HtmlTable->createTable();
*/

class MyDbHtmlTable extends dbHtmlTable {

public function setTheme($ThemeName)
    {
    switch(strtolower($ThemeName))
        {
        case 'mytheme':
            $this->setOptions(array(
                'TableAttr'   => 'border="0" cellspacing="0"',
                'emptyvalue'  => '<span style="color:#888;">(empty)</span>',
                'nullvalue'   => '<span style="color:#888;">-null-</span>',
                'OddRowClass' => 'odd',
                'EvenRowClass'=> 'even',
                'rownumber'   => '#',
                'ShowFooter'  => true
                ));
            break;
        case 'clean': default:
            $this->resetOptions();
        }
    }
}

/*
    EXAMPLE 2
    By default, dbHtmlTable uses MySQL as the data source. 
    This example shows how you can change the class to use a 
    different data source (in this case, PostgreSQL). 
    To do this you need to define three methods:

    _dbGetResultset()
        must set class properties dbRowCount, dbColumnCount and 
        return a query result resource, which will later be 
        passed to the other two methods.

    _dbGetFieldnames($res)
        given the resource returned by _dbGetResultset,
        must return an enumerated array of field names

    _dbGetRow($res)
        given the resource returned by _dbGetResultset,
        must return an array containing the first row of data, 
        and on subsequent calls the next row. The returned 
        array must be enumerated, not associative (ie indexed 
        by  field position not field name)
*/

class PostgreTable extends dbHtmlTable {

protected function _dbGetResultset()
    {
    $result = pg_query($this->dbSql);
	$this->dbRowCount = pg_num_rows($result);
	if(0==$this->dbRowCount) return false;
	$this->dbColumnCount = pg_num_fields($result);
    return $result;
    }

protected function _dbGetRow($resultset)
    { return pg_fetch_row($resultset); }

protected function _dbGetFieldnames($resultset)
    {
    $FieldName=array();
    for ($i = 0; $i < $this->dbColumnCount; $i++) 
        { $FieldName[] = pg_field_name($resultset, $i); }
    return $FieldName;
    }
}

/*
    EXAMPLE 3
    You could even amend example 2 to use a file, an array or 
    XML data as the data source. (I haven't actually tried this, 
    which is why there's no sample code!)
    
    The "sqlString" passed to the class could be a filename,
    an array, or any other data. You would just need a local 
    property to keep track of which row should be returned next
    by the _dbGetRow() method.
    
    The "query result resource" returned by _dbGetResultset() and
    used by _dbGetFieldnames() and _dbGetRow() is not checked in
    any way, so it could be anything.
*/

?>
Return current item: dbHtmlTable