| 5 theme.inc | theme_table($header, $rows, $attributes = array(), $caption = NULL) |
| 6 theme.inc | theme_table( |
| 7 theme.inc | theme_table($variables) |
| 8 theme.inc | theme_table($variables) |
Return a themed table.
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.
$rows: An array of table rows. Every row is an array of cells, or an associative array with the following keys:
- "data": an array of cells
- Any HTML attributes, such as "class", to apply to the table row.
Each cell can be either a string or an associative array with the following keys:
- "data": The string to display in the table cell.
- "header": Indicates this cell is a header.
- Any HTML attributes, such as "colspan", to apply to the table cell.
Here's an example for $rows:
<?php
$rows = array(
// Simple row
array(
'Cell 1', 'Cell 2', 'Cell 3'
),
// Row with attributes on the row and some of its cells.
array(
'data' => array('Cell 1', array('data' => 'Cell 2', 'colspan' => 2)), 'class' => 'funky'
)
);
?>$attributes: An array of HTML attributes to apply to the table tag.
$caption: A localized string to use for the <caption> tag.
Return value
An HTML string representing the table.
Related topics
File
- includes/
theme.inc, line 1359 - The theme system, which controls the output of Drupal.
Code
<?php
function theme_table($header, $rows, $attributes = array(), $caption = NULL) {
// Add sticky headers, if applicable.
if (count($header)) {
drupal_add_js('misc/tableheader.js');
// Add 'sticky-enabled' class to the table to identify it for JS.
// This is needed to target tables constructed by this function.
$attributes['class'] = empty($attributes['class']) ? 'sticky-enabled' : ($attributes['class'] . ' sticky-enabled');
}
$output = '<table' . drupal_attributes($attributes) . ">\n";
if (isset($caption)) {
$output .= '<caption>' . $caption . "</caption>\n";
}
// Format the table header:
if (count($header)) {
$ts = tablesort_init($header);
// HTML requires that the thead tag has tr tags in it followed by tbody
// tags. Using ternary operator to check and see if we have any rows.
$output .= (count($rows) ? ' <thead><tr>' : ' <tr>');
foreach ($header as $cell) {
$cell = tablesort_header($cell, $header, $ts);
$output .= _theme_table_cell($cell, TRUE);
}
// Using ternary operator to close the tags based on whether or not there are rows
$output .= (count($rows) ? " </tr></thead>\n" : "</tr>\n");
}
else {
$ts = array();
}
// Format the table rows:
if (count($rows)) {
$output .= "<tbody>\n";
$flip = array(
'even' => 'odd',
'odd' => 'even',
);
$class = 'even';
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;
}
if (count($cells)) {
// Add odd/even class
$class = $flip[$class];
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);
}
$output .= " </tr>\n";
}
}
$output .= "</tbody>\n";
}
$output .= "</table>\n";
return $output;
}
?> Login or register to post comments
Comments
Example for fetching data from a DB table and displaying it
For a table having columns ID, Look, and Taste.
<?php
function _mymodule_somepage_callback($nid) {
$query = db_query("SELECT * FROM {mytablename}");
$data = array();
$i = 0;
while ($row = db_fetch_array($query)) {
$data[$i] = $row;
$i++;
}
$output = theme_table(array('ID', 'Look', 'Taste'), $data);
return $output;
}
?>
Correction
This didn't work for me, although it pointed me in the right direction. To make it work in Drupal 6.x I changed it to something like the following (going on the example used by Granjow):
<?php
function _mymodule_somepage_callback($nid) {
$query = db_query("SELECT * FROM {mytablename}");
$data = array();
$i = 0;
while ($row = db_fetch_array($query)) {
$data[] = array($row[$i]);
$i++;
}
$output = theme_table(array('ID', 'Look', 'Taste'), $data);
return $output;
}
?>
Granjow example works
Granjow example works perfectly fine on D6.19 here, but why using an incremential int as you are not performing anything with it?
<?phpfunction _mymodule_somepage_callback($nid) {
$query = db_query("SELECT id, look, taste FROM {mytablename}");
$header = array('ID', 'Look', 'Taste');
$data = array();
while ($row = db_fetch_array($query)) $data[] = $row;
$output = theme_table($header, $data);
return $output;
}
?>
one more thing
I should add that the most common pitfall here is that the number of columns returned in $query does not match with the array of titles given in theme_table. You can avoid this happening by changing your query to this:
<?php$query = db_query("SELECT id, look, taste FROM {mytablename}");
?>
Now if at some stage you decide to add a column to "mytablename" it won't break your callback.
Or you could chose not to display headers...
Just an addition of information...
I found that I don't have to worry about matching columns to headers if I don't have headers at all. So to not have headers, and make the function continue to work, I just placed an undefined "array()" in place of headers:
theme_table(array(), $data);This outputs a table and doesn't care how many columns there are.
Downside is of course, it won't work for you if you DO want table headers =).
theme_table
why use theme_table directly
$output = theme_table(array('ID', 'Look', 'Taste'), $data);and not using theme hook like
$output = theme('table', array('ID', 'Look', 'Taste'), $data);also in this theme_table there is no possibility for using colspan within header, does anyone also have the same problem like i do?
please do share the solution.
thanks
Table footer?
Isn't table footer supported by Drupal?
I can't find it in Drupal 6 or D7.
you don't need a footer
you just use a last row to add your footer data.
How to make table sortable
This seems to be excluded. To make a table sortable, append tablesort_sql($header) to the end of your query. Sample code:
<?php
$html = '';
$header = array(array('data' => 'Name', 'field' => 'name', 'sort' => 'asc'), // sort by name by default, a-z order
array('data' => 'Date of Birth', 'field' => 'date_of_birth'),
'Notes');
$res = pager_query("SELECT name, date_of_birth, notes FROM {people}" . tablesort_sql($header), $limit = 30);
$data = array();
while ($row = db_fetch_array($res)) {
$data[] = array($row['name'], $row['date_of_birth'], $row['notes']);
}
$html .= theme('table', $header, $data);
$html .= theme('pager');
?>
This will give you a paginated table, thirty entries per page, with two sortable columns (name and date of birth).
An example for theme_table with page query.
There are an example for theme_table with page query.
<?php
function sample_comment($nid) {
global $base_url;
$query = "SELECT uid, subject, comment, timestamp FROM {comments} WHERE nid={$nid} ORDER BY timestamp DESC";
$data = pager_query($query, 10, 0,$count);
while($comment = db_fetch_object($data)) {
$userdata = db_fetch_object(db_query("SELECT name, picture FROM {users} WHERE uid=%d", $comment->uid));
if($userdata->picture != '')
$pic = $base_url . '/' . $userdata->picture;
else
$pic = $base_url . '/sites/all/themes/cdi_theme/images/User-icon.png';
$picdata = '<img src=' . $pic . ' />';
$username = $userdata->name;
$commentsub = '<i>' . $comment->subject . '</i>';
$commentbody = $comment->comment;
$date = ago($comment->timestamp);
$rows[] = array($picdata, $username, $commentsub, $commentbody);
$rows[] = array(array('data' => $date, 'colspan' => 4));
}
$output = theme('table', $header, $rows);
$output .= theme('pager', NULL, 10, 0);
return $output;
}
?>
$header set for table header are leave blank .( Its take a array like $header = array(
array('data' => t('Title')),
array('data' => t('Date & Time')),
array('data' => t('Duration')),
array('data' => t('Action')),
);) etc...
How create dual <tr> header ?
Hi all,
I try to create an array like that
<table><thead><tr><th>...</th><th colspan=2>...</th></tr>
<tr><th>...</th><th>...</th><th>...</th></tr>
</thead>
<tbody>...</tbody>
</table>
but i don't know if it's possible with theme('table')
Thx for your help