HTML Table Class

The Table Class provides methods that enable you to auto-generate HTMLtables from arrays or database result sets.

Using the Table Class

Initializing the Class

The Table class is not provided as a service, and should be instantiated“normally”, for instance:

  1. $table = new \CodeIgniter\View\Table();

Examples

Here is an example showing how you can create a table from amulti-dimensional array. Note that the first array index will become thetable heading (or you can set your own headings using the setHeading()method described in the function reference below).

  1. $table = new \CodeIgniter\View\Table();
  2.  
  3. $data = array(
  4. array('Name', 'Color', 'Size'),
  5. array('Fred', 'Blue', 'Small'),
  6. array('Mary', 'Red', 'Large'),
  7. array('John', 'Green', 'Medium')
  8. );
  9.  
  10. echo $table->generate($data);

Here is an example of a table created from a database query result. Thetable class will automatically generate the headings based on the tablenames (or you can set your own headings using the setHeading()method described in the class reference below).

  1. $table = new \CodeIgniter\View\Table();
  2.  
  3. $query = $db->query('SELECT * FROM my_table');
  4.  
  5. echo $table->generate($query);

Here is an example showing how you might create a table using discreteparameters:

  1. $table = new \CodeIgniter\View\Table();
  2.  
  3. $table->setHeading('Name', 'Color', 'Size');
  4.  
  5. $table->addRow('Fred', 'Blue', 'Small');
  6. $table->addRow('Mary', 'Red', 'Large');
  7. $table->addRow('John', 'Green', 'Medium');
  8.  
  9. echo $table->generate();

Here is the same example, except instead of individual parameters,arrays are used:

  1. $table = new \CodeIgniter\View\Table();
  2.  
  3. $table->setHeading(array('Name', 'Color', 'Size'));
  4.  
  5. $table->addRow(['Fred', 'Blue', 'Small']);
  6. $table->addRow(['Mary', 'Red', 'Large']);
  7. $table->addRow(['John', 'Green', 'Medium']);
  8.  
  9. echo $table->generate();

Changing the Look of Your Table

The Table Class permits you to set a table template with which you canspecify the design of your layout. Here is the template prototype:

  1. $template = [
  2. 'table_open' => '<table border="0" cellpadding="4" cellspacing="0">',
  3.  
  4. 'thead_open' => '<thead>',
  5. 'thead_close' => '</thead>',
  6.  
  7. 'heading_row_start' => '<tr>',
  8. 'heading_row_end' => '</tr>',
  9. 'heading_cell_start' => '<th>',
  10. 'heading_cell_end' => '</th>',
  11.  
  12. 'tfoot_open' => '<tfoot>',
  13. 'tfoot_close' => '</tfoot>',
  14.  
  15. 'footing_row_start' => '<tr>',
  16. 'footing_row_end' => '</tr>',
  17. 'footing_cell_start' => '<td>',
  18. 'footing_cell_end' => '</td>',
  19.  
  20. 'tbody_open' => '<tbody>',
  21. 'tbody_close' => '</tbody>',
  22.  
  23. 'row_start' => '<tr>',
  24. 'row_end' => '</tr>',
  25. 'cell_start' => '<td>',
  26. 'cell_end' => '</td>',
  27.  
  28. 'row_alt_start' => '<tr>',
  29. 'row_alt_end' => '</tr>',
  30. 'cell_alt_start' => '<td>',
  31. 'cell_alt_end' => '</td>',
  32.  
  33. 'table_close' => '</table>'
  34. ];
  35.  
  36. $table->setTemplate($template);

Note

You’ll notice there are two sets of “row” blocks in thetemplate. These permit you to create alternating row colors or designelements that alternate with each iteration of the row data.

You are NOT required to submit a complete template. If you only need tochange parts of the layout you can simply submit those elements. In thisexample, only the table opening tag is being changed:

  1. $template = [
  2. 'table_open' => '<table border="1" cellpadding="2" cellspacing="1" class="mytable">'
  3. ];
  4.  
  5. $table->setTemplate($template);

You can also set defaults for these by passing an array of template settingsto the Table constructor.:

  1. $customSettings = [
  2. 'table_open' => '<table border="1" cellpadding="2" cellspacing="1" class="mytable">'
  3. ];
  4.  
  5. $table = new \CodeIgniter\View\Table($customSettings);

Class Reference

  • class Table
    • $function = NULL
    • Allows you to specify a native PHP function or a valid function array object to be applied to all cell data.
  1. $table = new \CodeIgniter\View\Table();
  2.  
  3. $table->setHeading('Name', 'Color', 'Size');
  4. $table->addRow('Fred', '<strong>Blue</strong>', 'Small');
  5.  
  6. $table->function = 'htmlspecialchars';
  7. echo $table->generate();

In the above example, all cell data would be run through PHP’s htmlspecialchars() function, resulting in:

  1. <td>Fred</td><td>&lt;strong&gt;Blue&lt;/strong&gt;</td><td>Small</td>
  • generate([$tableData = NULL])

Parameters:

  1. - **$tableData** (_mixed_) Data to populate the table rows withReturns:

HTML tableReturn type:string

Returns a string containing the generated table. Accepts an optional parameter which can be an array or a database result object.

  • setCaption($caption)

Parameters:

  1. - **$caption** (_string_) Table captionReturns:

Table instance (method chaining)Return type:Table

Permits you to add a caption to the table.

  1. $table->setCaption('Colors');
  • setHeading([$args = [][, ]])

Parameters:

  1. - **$args** (_mixed_) An array or multiple strings containing the table column titlesReturns:

Table instance (method chaining)Return type:Table

Permits you to set the table heading. You can submit an array or discrete params:

  1. $table->setHeading('Name', 'Color', 'Size'); // or
  2.  
  3. $table->setHeading(['Name', 'Color', 'Size']);
  • setFooting([$args = [][, ]])

Parameters:

  1. - **$args** (_mixed_) An array or multiple strings containing the table footing valuesReturns:

Table instance (method chaining)Return type:Table

Permits you to set the table footing. You can submit an array or discrete params:

  1. $table->setFooting('Subtotal', $subtotal, $notes); // or
  2.  
  3. $table->setFooting(['Subtotal', $subtotal, $notes]);
  • addRow([$args = array()[, ]])

Parameters:

  1. - **$args** (_mixed_) An array or multiple strings containing the row valuesReturns:

Table instance (method chaining)Return type:Table

Permits you to add a row to your table. You can submit an array or discrete params:

  1. $table->addRow('Blue', 'Red', 'Green'); // or
  2.  
  3. $table->addRow(['Blue', 'Red', 'Green']);

If you would like to set an individual cell’s tag attributes, you can use an associative array for that cell.The associative key data defines the cell’s data. Any other key => val pairs are added as key=’val’ attributes to the tag:

  1. $cell = ['data' => 'Blue', 'class' => 'highlight', 'colspan' => 2];
  2. $table->addRow($cell, 'Red', 'Green');
  3.  
  4. // generates
  5. // <td class='highlight' colspan='2'>Blue</td><td>Red</td><td>Green</td>
  • makeColumns([$array = [][, $columnLimit = 0]])

Parameters:

  1. - **$array** (_array_) An array containing multiple rows data
  2. - **$columnLimit** (_int_) Count of columns in the tableReturns:

An array of HTML table columnsReturn type:array

This method takes a one-dimensional array as input and creates a multi-dimensional array with a depth equal to the number of columns desired.This allows a single array with many elements to be displayed in a table that has a fixed column count. Consider this example:

  1. $list = ['one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine', 'ten', 'eleven', 'twelve'];
  2.  
  3. $newList = $table->makeColumns($list, 3);
  4.  
  5. $table->generate($newList);
  6.  
  7. // Generates a table with this prototype
  8.  
  9. <table border="0" cellpadding="4" cellspacing="0">
  10. <tr>
  11. <td>one</td><td>two</td><td>three</td>
  12. </tr><tr>
  13. <td>four</td><td>five</td><td>six</td>
  14. </tr><tr>
  15. <td>seven</td><td>eight</td><td>nine</td>
  16. </tr><tr>
  17. <td>ten</td><td>eleven</td><td>twelve</td></tr>
  18. </table>
  • setTemplate($template)

Parameters:

  1. - **$template** (_array_) An associative array containing template valuesReturns:

TRUE on success, FALSE on failureReturn type:bool

Permits you to set your template. You can submit a full or partial template.

  1. $template = [
  2. 'table_open' => '<table border="1" cellpadding="2" cellspacing="1" class="mytable">'
  3. ];
  4.  
  5. $table->setTemplate($template);
  • setEmpty($value)

Parameters:

  1. - **$value** (_mixed_) Value to put in empty cellsReturns:

Table instance (method chaining)Return type:Table

Lets you set a default value for use in any table cells that are empty.You might, for example, set a non-breaking space:

  1. $table->setEmpty("&nbsp;");
  • clear()

Returns:Table instance (method chaining)Return type:Table

Lets you clear the table heading, row data and caption. Ifyou need to show multiple tables with different data youshould to call this method after each table has beengenerated to clear the previous table information.

Example

  1. $table = new \CodeIgniter\View\Table();
  2.  
  3.  
  4. $table->setCaption('Preferences')
  5. ->setHeading('Name', 'Color', 'Size')
  6. ->addRow('Fred', 'Blue', 'Small')
  7. ->addRow('Mary', 'Red', 'Large')
  8. ->addRow('John', 'Green', 'Medium');
  9.  
  10. echo $table->generate();
  11.  
  12. $table->clear();
  13.  
  14. $table->setCaption('Shipping')
  15. ->setHeading('Name', 'Day', 'Delivery')
  16. ->addRow('Fred', 'Wednesday', 'Express')
  17. ->addRow('Mary', 'Monday', 'Air')
  18. ->addRow('John', 'Saturday', 'Overnight');
  19.  
  20. echo $table->generate();