Skip to content

Instantly share code, notes, and snippets.

@jonhattan
Last active December 18, 2015 09:49
Show Gist options
  • Save jonhattan/5764326 to your computer and use it in GitHub Desktop.
Save jonhattan/5764326 to your computer and use it in GitHub Desktop.
Example of building a table with rows from a db table in Drupal
<?php
/**
* Menu callback.
*/
function this_is_a_menu_callback() {
$header = array('nid' => 'ID', 'title' => 'Title');
$query = db_select('node', 'n', array('fetch' => PDO::FETCH_ASSOC))
->fields('n', array('nid', 'title'))
->extend('PagerDefault')
->limit(10)
->extend('TableSort')
->orderByHeader($header);
$rows =$query
->execute()
->fetchAllAssoc('nid');
$build = array();
$build['nodes'] = array(
'#theme' => 'table',
'#header' => $header,
'#rows' => $rows,
'#empty' => t('No nodes available.'),
);
$build['pager'] = array(
'#theme' => 'pager',
);
//print render($build);
return $build;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment