Skip to content

Instantly share code, notes, and snippets.

@simme
Created August 23, 2012 11:51
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save simme/3435931 to your computer and use it in GitHub Desktop.
Save simme/3435931 to your computer and use it in GitHub Desktop.
Drupal table with pager from db_select()
<?php
/**
* Display point award report.
*
* Generates a list of awarded points.
*
* @return array $content
*/
function game_report_page_points() {
// For some mysterious reason only parts of the db_select API is chainable.
// Which means that sometimes you have to split it up like I've done below.
$query = db_select('game_points', 'p')
->orderBy('timestamp', 'DESC') // Default sort, not reflected in table. :/
->extend('PagerDefault') // Automagical pager
->limit(50); // Items per page
$query->join('users', 'u', 'p.uid = u.uid');
$query->fields('p', array(
'id',
'points',
'point_name',
'point_category',
'timestamp',
'meta',
));
$query->fields('u', array('name'));
$result = $query->execute()->fetchAllAssoc('id');
$render = array(
'table' => array(
'#theme' => 'table',
'#header' => array(
'id' => array('data' => t('ID'), 'field' => 'id'),
'points' => array('data' => t('Points'), 'field' => 'points'),
'point_name' => array('data' => t('Type'), 'field' => 'point_name'),
'point_category' => array('data' => t('Category'), 'field' => 'point_category'),
'timestamp' => array('data' => t('Date'), 'field' => 'timestamp'),
'meta' => array('data' => t('Meta'), 'field' => 'meta'),
'name' => array('data' => t('User'), 'field' => 'name'),
),
'#rows' => array_map(function ($row) {
return (array)$row;
}, $result),
'#sticky' => TRUE,
'#empty' => t('No points recorded yet.'),
),
'pager' => array(
'#theme' => 'pager',
),
);
_game_preprocess_report($render);
return $render;
}
/**
* Preproccsing of report tables.
*
* @param array $report
*
* @return void
*/
function _game_preprocess_report(&$report) {
// Format dates, can someone tell me how to do this directly in the
// query?
foreach ($report['table']['#rows'] as $key => $row) {
$time = $row['timestamp'];
$row['timestamp'] = date('Y-m-d H:i:s', $time);
$report['table']['#rows'][$key] = $row;
}
}
@simme
Copy link
Author

simme commented Aug 23, 2012

Seems like the table won't sort. So I guess the documentation is kinda wrong. Hmm.

@zsnagy73
Copy link

About formatting dates in the query:

$entries = $query->execute()->fetchAll(PDO::FETCH_ASSOC);

  if (!empty($entries)) {
    $rows = array();
    foreach ($entries as $entry) {
      $entry['timestamp'] = format_date($entry['timestamp']);
      // Sanitize the data before handing it off to the theme layer.
      $rows[] = array_map('check_plain', $entry);
    }

    // Make a table for them.
    $header = array(t('Id'), t('Message'), t('Time'));
    $output = theme('table', array('header' => $header, 'rows' => $rows));
    $output .= theme('pager', array('tags' => array()));
  }

@nkorojoseph
Copy link

how do i print the table

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment