theme_table
Definition
theme_table($header, $rows, $attributes = NULL)
includes/theme.inc, line 630
Description
Return a themed table.
Each cell can be either a string or an associative array with the following keys:
- "data": The string to display in the table cell.
- Any HTML attributes, such as "colspan", to apply to the table cell.
Parameters
$header An array containing the table headers. Each element of the array can be either a localized string or an associative array with the following keys:
- "data": The localized title of the table column.
- "field": The database field represented in the table column (required if user is to be able to sort on this column).
- "sort": A default sort order for this column ("asc" or "desc").
- Any HTML attributes, such as "colspan", to apply to the column header cell.
- "data": an array of cells
- Any HTML attributes, such as "class", to apply to the table row.
Return value
An HTML string representing the table.
Related topics
| Name | Description |
|---|---|
| Themeable functions | Functions that display HTML, and which can be customized by themes. |
Code
<?php
function theme_table($header, $rows, $attributes = NULL) {
$output = '<table'. drupal_attributes($attributes) .">\n";
// Format the table header:
if (count($header)) {
$ts = tablesort_init($header);
$output .= ' <tr>';
foreach ($header as $cell) {
$cell = tablesort_header($cell, $header, $ts);
$output .= _theme_table_cell($cell, 1);
}
$output .= " </tr>\n";
}
// Format the table rows:
if (count($rows)) {
foreach ($rows as $number => $row) {
$attributes = array();
// Check if we're dealing with a simple or complex row
if (isset($row['data'])) {
foreach ($row as $key => $value) {
if ($key == 'data') {
$cells = $value;
}
else {
$attributes[$key] = $value;
}
}
}
else {
$cells = $row;
}
// Add light/dark class
$class = ($number % 2 == 1) ? 'light': 'dark';
if (isset($attributes['class'])) {
$attributes['class'] .= ' '. $class;
}
else {
$attributes['class'] = $class;
}
// Build row
$output .= ' <tr'. drupal_attributes($attributes) .'>';
$i = 0;
foreach ($cells as $cell) {
$cell = tablesort_cell($cell, $header, $ts, $i++);
$output .= _theme_table_cell($cell, 0);
}
$output .= " </tr>\n";
}
}
$output .= "</table>\n";
return $output;
}
?> 