tablesort_get_order

Versions
4.6 – 7
tablesort_get_order($headers)

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.

Code

includes/tablesort.inc, line 236

<?php
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 the initial 'order by' field unless otherwise specified.
    $first = current($headers);
    if (is_array($first)) {
      $first += array('data' => NULL, 'field' => NULL);
      return array('name' => $first['data'], 'sql' => $first['field']);
    }
    else {
      return array('name' => $first, 'sql' => '');
    }
  }
}
?>
Login or register to post comments
 
 

All source code and documentation on this site is released under the terms of the GNU General Public License, version 2 and later. Drupal is a registered trademark of Dries Buytaert.