cTableEntity is a base class included in all other classes.
It provides basic methods to design the html entities (only table, tr, th and td) and their attributes. Other entities can be inserted with conventional statement like echo '<caption>Registered users</caption>';
$this->AddAttr('title','Click this column name to change the sort order');$this->AddAttrValue('class','warning');id="table1" class="users" summary="Registered users"<table id="table1" class="users" summary="Registered users"></table>The cTableEntity is not instantiated directly. The code will rather instantiate other classes that extend this class (see classes documentation here after).
When creating a new entity (for example a <table> entity) you can insert directly the 'id' and 'class' attribute values. To add other attributes, use AddAttr() method.
Example: $t = new cTable('table1','users');
This creates a cTable object and will show <table id="table1" class="users"> when using the Start() method.
An instance of cTableRow is row object (i.e. a cTableEntity representing a <tr> entity).
See cTableEntity for the description of the properties and methods.
An instance of cTableHead is a column header object (i.e. a cTableEntity representing a <th> entity).
$this->link = '<a href="page.php?order=col1">%s</a>'; When the row is displayed, this column content will show a link allowing to click this column header (to change the sort order). An instance of cTableData is a column cell object (i.e. a cTableEntity representing a <td> entity).
This class also allows defining a list of attribute values that must be applied to certain cells content.
For example, in a column, we want a background color for some specific values:
Here after, we show how to get a green background for positive value, and a red background when the cell content is negative.
$t->arrTD['col3']->dynamicValues = array('positive'=>'background-color:green', 'negative'=>'background-color:red');$t->arrTD['col1']->AddDynamicAttr('style',($this->content<0 ? 'negative' : 'positive'));$t = new cTable('table1','class1');
When creating an instance of cTable we can assign directly two basic html attributes, the 'id' and the 'class'.
Other attributes can be inserted with the AddAttr() method.
This principle is also applicable for all other objects (like a <tr>, <td>, or <th>).
Examples of column headers:
The cTableHead object (described here after) has 3 basic properties:
a content, an id attribute and a class attribute
(other html attributes can be added with the AddAttr method)
$t->arrTH[0] = new cTableHead('Name','hd0','head');
$t->arrTH[1] = new cTableHead('Given names','hd1','head');
$t->arrTH[2] = new cTableHead('Address','hd2','head');
Here we store the column headers with a numerical key (the column order: 0,1,2...).
When working with a database, it's more easy to use the database fieldname as key.
For example:
$t->arrTH['name'] = new cTableHead('Name','hd0','head');
$t->arrTH['firstname'] = new cTableHead('Given names','hd1','head');
$t->arrTH['address'] = new cTableHead('Address','hd2','head');
When displaying these column headers, $t->GetTHrow() generates:
<tr>
<th id="hd0" class="head">Name</th>
<th id="hd1" class="head">Given name</th>
<th id="hd3" class="head">Address</th>
</tr>
Examples of column data:
The cTableData object (described here after) has 3 basic properties:
a content, an id attribute and a class attribute
(other html attributes can be added with the AddAttr method)
$t->arrTD[0] = new cTableData('Smith','','cell');
$t->arrTD[1] = new cTableData('John','','cell');
$t->arrTD[2] = new cTableData('Wallstreet','','cell');
When displaying these columns, this generates:
<tr>
<th class="cell">Name</th>
<th class="cell">Given name</th>
<th class="cell">Address</th>
</tr>
Note:
When working with database, we will not use these simple declarations.
We will rather used advanced methods allowing to automate columns creation or to fill the contents of several cells.
cTableEntity::End(). This method closes the table entity (i.e. returns the </table> tag). The options allow removing the columns, column headers or default row:<td> stored in $this->arrTD)<th> stored in $this->arrTH)<tr> stored in $this->row)GetTHrow($id,$class) This method returns the formatted column headers as one string. (e.g. <tr><th>...</th><th>...</th></tr>) If the $row property is not defined, this method creates the cTableRow object (with attributes $id and $class). If the $row property is already defined, this method will use the existing cTableRow object (arguments $id and $class are useless)
GetTDrow($id,$class) This method returns the formatted column row as one string. (e.g. <tr><td>...</td><td>...</td></tr>) If the $row property is not defined, this method creates the cTableRow object (with attributes $id and $class). If the $row property is already definied, this method will use the existing cTableRow object (arguments $id and $class are useless)
GetTHnames() This returns the column header names in an array, where the array-key is the column key (i.e. the $arrTH key) and the array-value is the column header name (i.e. the <th> content).
The following advanced methods allows changing all columns at once (you can change the entity, the content or an attribute).
<td> instead of the default <th> as column headers.ChangeTDattr($arr,$bCreateColumn,$bNamedColumn): Idem for the column cells.
About these advanced methods They will apply the values provided by $arr, but two options are available: