0

In the example_page I believe it shows something like this:

function foo() {
    return array('#markup' => t('bar'));
}

And the function foo would be used for a page callback in the menu hook to create a page. However, In my module I plan on using a db_query to access a database table and with it display many rows of data.

Is there a better way to do this than stuffing all of my output into #markup? It could be done with lots of concatenation, but it would be rather messy and it seems like there should be a better way.

2

If you're looking to build an HTML table then the theme_table() function will help a lot. See the docs page for full details but this is a basic example:

function MYMODULE_page_callback() {
  // Header for the table
  $header = array('id', 'name');

  // Get your data
  $data = db_query('SELECT id, name FROM {my_table}')->fetchAllKeyed();

  // Loop through the data and add rows to the table
  $rows = array();
  foreach ($data as $id => $name) {
    $rows[] = array(
      $id,
      $name
    );
  }

  // Return the themed table
  return theme('table', array('header' => $header, 'rows' => $rows));

  // You could also use:
  return array(
    '#theme' => 'table',
    '#header' => $header,
    '#rows' => $rows
  );
}
  • Thank you very much again. Sorry for the simple questions, I really know PHP,SQL, & HTML, but learning how to program for drupal almost feels like another language lol. – Dave_R May 7 '12 at 0:02
  • 1
    Don't mention it :) Drupal is notorious for having a steep learning curve but trust me it's worth it if you persevere! – Clive May 7 '12 at 0:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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