tablesort_sql

5 tablesort.inc tablesort_sql($header, $before = '')
6 tablesort.inc tablesort_sql($header, $before = '')

Create an SQL sort clause.

This function produces the ORDER BY clause to insert in your SQL queries, assuring that the returned database table rows match the sort order chosen by the user.

Parameters

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

$before: An SQL string to insert after ORDER BY and before the table sorting code. Useful for sorting by important attributes like "sticky" first.

Return value

An SQL string to append to the end of a query.

Related topics

15 calls to tablesort_sql()

File

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

Code

function tablesort_sql($header, $before = '') {
  $ts = tablesort_init($header);
  if ($ts['sql']) {
    // Based on code from db_escape_table(), but this can also contain a dot.
    $field = preg_replace('/[^A-Za-z0-9_.]+/', '', $ts['sql']);

    // Sort order can only be ASC or DESC.
    $sort = drupal_strtoupper($ts['sort']);
    $sort = in_array($sort, array('ASC', 'DESC')) ? $sort : '';

    return " ORDER BY $before $field $sort";
  }
}

Comments

alternate sort field

Hello. Within the table I am working, I have a description column which if the description exists in the database it prints to screen 'update description' and if not 'add description'. Does anyone know how I can sort by the text visible onscreen (update description & add description) rather than the actual description returned by the database? Thank you.

Danilo

postfix your before with a comma

this seems apparent from looking at that code but not mentioned in the doco

Login or register to post comments