2

I have a working block of php code that runs inside a Drupal 7 Page:

<?php
$my_database= array(
  'database' => 'mydb',
  'username' => 'xxxxxx', // assuming this is necessary
  'password' => 'xxxxxx', // assuming this is necessary
  'host' => 'localhost', // assumes localhost
  'driver' => 'mysql', // replace with your database driver
);
Database::addConnectionInfo('database_db', 'default', $my_database);
db_set_active('database_db');
$sql = "SELECT tcol1, tcol2 FROM mytable LIMIT 10";

$result = db_query($sql);
$header = array('Col1', 'Col2');
$rows = array();
if ($result) {
  while ($row = $result->fetchAssoc()) {
    $row= array($row['tcol1'],$row['tcol2']);
  $rows[] = $row;
  }
}
db_set_active();
print_r($rows);
$output= theme('table', $header, $rows);
?>

This piece of code works fine and print_r() prints all the data I asked for. The odd thing is that the theming fails totally, showing nothing. I have done some experimentation:

    $output= theme('table', $header, $rows);
    echo theme('table', $header, $rows);
    print theme('table', $header, $rows);

but all of them fail, showing nothing or disrupting drupal globally. How can I show my data in a nice table? I want to avoid writing html code and instead rely on Drupal theming.

1 Answer 1

6

The format of arguments to theme functions changed in Drupal 7, instead of

$output = theme('table', $header, $rows);

you need to use

$output = theme('table', array('header' => $header, 'rows' => $rows));

or, using a render array,

$build = array(
  '#theme' => 'table',
  '#header' => $header,
  '#rows' => $rows,
);
$output = drupal_render($build);
1
  • 1
    You are my hero!
    – Terix
    Commented Sep 25, 2013 at 17:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.