Skip to content

Instantly share code, notes, and snippets.

@timonweb
Created August 5, 2011 08:35
Show Gist options
  • Save timonweb/1127138 to your computer and use it in GitHub Desktop.
Save timonweb/1127138 to your computer and use it in GitHub Desktop.
Sortable Table with Pager in Drupal 7
<?php
function folks_db_report() {
$header = array(
'id' => array('data' => t('Id'), 'field' => 'u.id'),
'first_name' => array('data' => t('First Name'), 'field' => 'u.first_name'),
'last_name' => array('data' => t('Last Name'), 'field' => 'u.last_name'),
'color' => array('data' => t('Favorite Color'), 'field' => 'u.color'),
);
$query = db_select('folk', 'u');
$count_query = clone $query;
$count_query->addExpression('COUNT(u.id)');
$query = $query->extend('PagerDefault')->extend('TableSort');
$query
->fields('u', array('id', 'first_name', 'last_name', 'color'))
->limit(10)
->orderByHeader($header)
->setCountQuery($count_query);
$result = $query->execute();
$rows = array();
foreach ($result as $folk) {
$rows[$folk->id] = array(
'id' => toHtml('span', $folk->id),
'first_name' => toHtml('span', $folk->first_name),
'last_name' => toHtml('span', $folk->last_name),
'color' => toHtml('span', $folk->color),
);
}
$vars['header'] = $header;
$vars['empty'] = 'Empty List - No Entries';
$vars['rows'] = $rows;
$render_array['table'] = array(
'#theme' => 'table',
'#rows' => $rows,
'#empty' => 'Empty List - No Entries',
'#header' => $header,
);
$render_array['pager'] = array(
'#theme' => 'pager',
);
return $render_array;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment