4.6.x theme.inc theme_table($header, $rows, $attributes = NULL)
4.7.x theme.inc theme_table($header, $rows, $attributes = array(), $caption = NULL)
5.x theme.inc theme_table($header, $rows, $attributes = array(), $caption = NULL)
6.x theme.inc theme_table($header, $rows, $attributes = array(), $caption = NULL)
7.x 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:

  $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

50 theme calls to theme_table()
aggregator_view in modules/aggregator/aggregator.admin.inc
Displays the aggregator administration page.
book_admin_overview in modules/book/book.admin.inc
Returns an administrative overview of all books.
contact_admin_categories in modules/contact/contact.admin.inc
Categories/list tab.
dblog_event in modules/dblog/dblog.admin.inc
Menu callback; displays details about a log message.
dblog_overview in modules/dblog/dblog.admin.inc
Menu callback; displays a listing of log messages.

... See full list

File

includes/theme.inc, line 1371
The theme system, which controls the output of Drupal.

Code

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;
}

Comments

Granjow’s picture

For a table having columns ID, Look, and Taste.

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;
}
armanschwarz’s picture

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):

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;
}
erlendstromsvik’s picture

Old comment, but still...

$i = 0;
  while ($row = db_fetch_array($query)) {
    $data[] = array($row[$i]);
    $i++;
  }

That is just REALLY bad coding!

db_fetch_array returns an array. When you write row[i] you actually fetch the first column/value from the first record on the first iteration, second column/valute on the second record, third column/valute on third record, and so on.

Just keep it simple:

  while ($row = db_fetch_array($query)) {
    $data[] = array_values($row);
  }
armanschwarz’s picture

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:

$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.

Cablestein’s picture

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 =).

feedbackloop’s picture

This seems to be excluded. To make a table sortable, append tablesort_sql($header) to the end of your query. Sample code:

$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).

dilipsingh02’s picture

There are an example for theme_table with page query.


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 = '';

    $username = $userdata->name;

    $commentsub = '' . $comment->subject . '';

    $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...

muhammad.tanweer’s picture

Just to add a note, might help someone. Attributes to the table tag are added like this:

$table_attributes = array('width' => '100%', 'border' => 0, 'cellspacing' => 5, cellpadding => 5);
theme('table', $header, $rows, $table_attributes);

Muhammad.
www.app-desk.com

ratneshnavlakhe’s picture

Hi all,

I want to provide a link in a cell how to do that waiting for the reply

guntervs’s picture

Where I define my cell content I just use the core l() function:

array('data' => l($row->title, 'node/' .$row->nid));
guntervs’s picture

I know that when you want a default sort, your header should be something like this:

array('data' => t('Institution'), 'field' => 'inst_title', 'sort' => 'asc');

Is it possible to combine sorts, eg. ORDER BY user.uid ASC, node.title ASC?

Jacob’s picture

Try http://tablesorter.com/docs/example-meta-sort-list.html - it's completely external, client side jQuery sorting plugin that allows multi column sorting

frankblaze’s picture

Please, I need a tutorial or code example of how to create a form with a textfield and a button to search for contents in a database and how to get the results displayed in a grid format and not a table.
What i mean is, the results do not display in tabular form rather each result in a rectangular box like a grid.
Thanks for your time

professor_b’s picture

or else you get a fatal "[] operator not supported for strings in /path/to/includes/theme.inc". in other words, even if you are thinking of a scalar 'myclass' that you want to add to a tr/array that you are attempting to add to your 'rows' within your $table, it has to be an array.

$rows = [];
foreach ($my_database_results as $record) {
    $rows[] = [
        'data' => [ /* ... */],
        'id' => 'some-id',
        // WRONG!
        'class' => 'my-class' ,
        // RIGHT :-)
        'class' => [ 'my-class'  ],
    ] 
}

One interesting/annoying fact here is that you can get away with a string for your class with a nested td array, but not for a tr.