You are here

function tablesort_get_order in Drupal 6

Same name and namespace in other branches
  1. 8 core/includes/tablesort.inc \tablesort_get_order()
  2. 4 includes/tablesort.inc \tablesort_get_order()
  3. 5 includes/tablesort.inc \tablesort_get_order()
  4. 7 includes/tablesort.inc \tablesort_get_order()

Determine the current sort criterion.

Parameters

$headers: An array of column headers in the format described in theme_table().

Return value

An associative array describing the criterion, containing the keys:

  • "name": The localized title of the table column.
  • "sql": The name of the database field to sort on.
1 call to tablesort_get_order()
tablesort_init in includes/tablesort.inc
Initialize the table sort context.

File

includes/tablesort.inc, line 151
Functions to aid in the creation of sortable tables.

Code

function tablesort_get_order($headers) {
  $order = isset($_GET['order']) ? $_GET['order'] : '';
  foreach ($headers as $header) {
    if (isset($header['data']) && $order == $header['data']) {
      return array(
        'name' => $header['data'],
        'sql' => isset($header['field']) ? $header['field'] : '',
      );
    }
    if (isset($header['sort']) && ($header['sort'] == 'asc' || $header['sort'] == 'desc')) {
      $default = array(
        'name' => $header['data'],
        'sql' => isset($header['field']) ? $header['field'] : '',
      );
    }
  }
  if (isset($default)) {
    return $default;
  }
  else {

    // The first column specified is initial 'order by' field unless otherwise specified
    if (is_array($headers[0])) {
      $headers[0] += array(
        'data' => NULL,
        'field' => NULL,
      );
      return array(
        'name' => $headers[0]['data'],
        'sql' => $headers[0]['field'],
      );
    }
    else {
      return array(
        'name' => $headers[0],
      );
    }
  }
}