1 tablesort_example.module tablesort_example_page()

Builds the table render array.

Return value

array: A render array set for theming by theme_table().

Related topics

File

modules/examples/tablesort_example/tablesort_example.module, line 38
Hook implementations for the Table Sort Example module.

Code

function tablesort_example_page() {
  // We are going to output the results in a table with a nice header.
  $header = array(
    // The header gives the table the information it needs in order to make
    // the query calls for ordering. TableSort uses the field information
    // to know what database column to sort by.
    array('data' => t('Numbers'), 'field' => 't.numbers'),
    array('data' => t('Letters'), 'field' => 't.alpha'),
    array('data' => t('Mixture'), 'field' => 't.random'),
  );

  // Using the TableSort Extender is what tells the the query object that we
  // are sorting.
  $query = db_select('tablesort_example', 't')
    ->extend('TableSort');
  $query->fields('t');

  // Don't forget to tell the query object how to find the header information.
  $result = $query
  ->orderByHeader($header)
    ->execute();

  $rows = array();
  foreach ($result as $row) {
    // Normally we would add some nice formatting to our rows
    // but for our purpose we are simply going to add our row
    // to the array.
    $rows[] = array('data' => (array) $row);
  }

  // Build the table for the nice output.
  $build['tablesort_table'] = array(
    '#theme' => 'table',
    '#header' => $header,
    '#rows' => $rows,
  );

  return $build;
}