How to create a table and add extra attributes...
$t = new cTable('table1','users');
$t->AddAttr('summary','Registered users');
echo $t->Start();
// Output:
// <table id="table1" class="users" summary="Registered users">
We define 3 columns...
$t = new cTable('table1','users');
$t->arrTH[0] = new cTableHead('Name', 'th0', 'head bold');
$t->arrTH[1] = new cTableHead('Given names','th1', 'head' );
$t->arrTH[2] = new cTableHead('Address', 'th2', 'head' );
echo $t->GetTHrow();
// Output:
// <tr>
// <th id="th0" class="head bold">Name</th>
// <th id="th1" class="head">Given names</th>
// <th id="th2" class="head">Address</th>
// </tr>
This is identical to the example #2, but using advanced methods to automate the creation of the columns and to change all attributes at once
$t = new cTable('table1','users');
$t->ChangeTHcontent(array(0=>'Name',1=>'Given names',2=>'Address')); // Here the column headers does not exist, nevertheless this method creates the missing columns (thus columns [0],[1] and [2] are created and the contents are added).
$t->ChangeTHattr('id', array(0=>'th0',1=>'th1',2=>'th2')); // Here the html 'id' attributes exist (columns are created in previous line) but values are empty. This populates the 'id' attributes with distinct values.
$t->ChangeTHattr('class', 'head'); // Here is the same as in previous line, but only one value is provided. In this case, this attribute value will be used in all column headers.
$t->arrTH[0]->AddAttrValue('class',' bold'); // Here we just add the ' bold' value into the existing html class attribute, only in the first column.
echo $t->GetTHrow();
// Output:
// <tr>
// <th id="th0" class="head bold">Name</th>
// <th id="th1" class="head">Given names</th>
// <th id="th2" class="head">Name</th>
// </tr>
With clickable column header to change sorting order
$t = new cTable('table1','users');
$t->rowcount = 5;
$t->activecol = $_GET['order'];
$t->activelink = '';
$t->arrTH['name'] = new cTableHead('Name', 'th0', 'head', '<a href="thispage.php?order=name">%s</a>');
$t->arrTH['firstname'] = new cTableHead('Given names','th1', 'head', '<a href="thispage.php?order=firstname">%s</a>');
$t->arrTH['address'] = new cTableHead('Address', 'th2', 'head', '<a href="thispage.php?order=address">%s</a>');
echo $t->GetTHrow();
// Here the column headers have link property.
// These patterns are applied to the column headers, except for the active column.
// When the active column is 'name', the output is:
// <tr>
// <th id="th0" class="head">Name</th>
// <th id="th1" class="head"><a href="thispage.php?order=firstname">Given names</a></th>
// <th id="th2" class="head"><a href="thispage.php?order=address">Address</a></th>
// </tr>
// Note:
// If rowcount is 0 or 1 (i.e. the dataset is empty or contains only 1 row),
// the link property is not added in the column headers
// and the output will be the same as in example #3.
With clickable column headers to change sorting order AND sorting direction (can be switched ascending/descending)
$t = new cTable('table1','users');
$t->rowcount = 5;
$t->activecol = $_GET['order'];
$t->activelink = '<a href="thispage.php?order='.$_GET['order'].'&direction='.($_GET['direction']=='asc' ? 'desc' : 'asc').'">%s</a>';
$t->arrTH['name'] = new cTableHead('Name', 'th0', 'head', '<a href="thispage.php?order=name&direction=asc">%s</a>');
$t->arrTH['firstname'] = new cTableHead('Given names','th1', 'head', '<a href="thispage.php?order=firstname&direction=asc">%s</a>');
$t->arrTH['address'] = new cTableHead('Address', 'th2', 'head', '<a href="thispage.php?order=address&direction=asc">%s</a>');
echo $t->GetTHrow();
// Here the column headers have a link property.
// These patterns are applied to the column headers.
// For the active column header an other url link is used:
// an url link with a reversed direction (from 'asc' to 'desc')
// When the active column is 'name', the output is:
// <tr>
// <th id="th0" class="head"><a href="thispage.php?order=name&direction=desc">Name</a></th>
// <th id="th1" class="head"><a href="thispage.php?order=firstname&direction=asc">Given names</a></th>
// <th id="th2" class="head"><a href="thispage.php?order=address&direction=asc">Address</a></th>
// </tr>
// Note:
// If rowcount is 0 or 1 (i.e. the dataset is empty or contains only 1 row),
// the link property is not added in the column headers and
// the output will be the same as in example #3.
Show how to have a specific a style added in the dataset row.
$t = new cTable('table1','users');
$t->arrTH['name'] = new cTableHead('Name');
$t->arrTH['bill'] = new cTableHead('Dollars');
$t->arrTD['name'] = new cTableData(); // We create an empty <td> object. It will be filled when looping in the dataset
$t->arrTD['bill'] = new cTableData();
$t->arrTD['bill']->dynamicValues = array('positive'=>'background-color:green','negative'=>'background-color:red');
echo $t->Start();
echo $t->GetTHrow();
// We loop in the dataset (e.g. a row fetched from a sql query)
while($row = mysql_fetch_assoc($result))
{
$t->arrTD['name']->content = $row['name'];
$t->arrTD['bill']->content = $row['bill'];
$t->arrTD['bill']->AddDynamicAttr('style', ($row['bill']<0 ? 'negative' : 'positive'));
echo $t->GetTDrow();
}
echo $t->End();
// Note, if the arrTD are designed with the same key as the database fieldname,
// instead of adding the row content cell by cell,
// we can populate all cells of the row with the method $t->ChangeTDcontent($row);
Using advanced methods. Here we create columns without instantiating each column object.
$t = new cTable('table1','users');
$t->ChangeTHcontent(array('Name','Given name','Address')); // create column headers (because not yet existing) and fill content
$t->ChangeTHattr('class','head'); // add a 'class' attribute with value 'head' to each column
echo $t->Start();
echo $t->GetTHrow();
$t->ChangeTDcontent(array('Smith','John','Wallstreet')); echo $t->GetTDrow();
$t->ChangeTDcontent(array('White','Bob','Downstreet')); echo $t->GetTDrow();
$t->ChangeTDcontent(array('Brown','Bill','Mainstreet')); echo $t->GetTDrow();
echo $t->End();
// Output:
// <table id="table1" class="users">
// <tr>
// <th class="head">Name</th><th class="head">Given name</th><th class="head">Address</th></tr>
// <tr><td>Smith</td><td>John</td><td>Wallstreet</td></tr>
// <tr><td>White</td><td>Bob</td><td>Downstreet</td></tr>
// <tr><td>Brown</td><td>Bill</td><td>Mainstreet</td></tr>
// </table>