Sortable Drupal Pagination: pager_query

Drupal pagination can be achieved using pager_query API function and tablesort.

I am here mentioning the code snippet that would help create drupal pagination of the output that is sortable with fields.

<php>
$sql = ‘SELECT cid, first_name, last_name, company, city FROM {clients}’;
$limit = 20;
$header = array(
array(‘data’ => t(‘Name’), ‘field’ => ‘last_name’, ‘sort’ => ‘asc’),
array(‘data’ => t(‘Company’), ‘field’ => ‘company’),
array(‘data’ => t(‘City’), ‘field’ => ‘city’)
);
$tablesort = tablesort_sql($header);
$result = pager_query($sql . $tablesort, $limit);
$rows = array();
while ($client = db_fetch_object($result)) {
$rows[] = array(l($client->last_name.’, ‘.$client->first_name, ‘client/’.$client->cid), $client->company, $client->city);
}
if (!$rows) {
$rows[] = array(array(‘data’ => t(‘No client accounts created yet.’), ‘colspan’ => 3));
}
$output .= theme(‘table’, $header, $rows);
$output .= theme(‘pager’, NULL, $limit, 0);

print $output;
</php>

Comments on this entry are closed.

css.php