4.6.x tablesort.inc tablesort_sql($header, $before = '')
4.7.x tablesort.inc tablesort_sql($header, $before = '')
5.x tablesort.inc tablesort_sql($header, $before = '')
6.x 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.


$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

14 calls to tablesort_sql()
comment_admin_overview in modules/comment/comment.module
forum_get_topics in modules/forum/forum.module
path_overview in modules/path/path.module
Return a listing of all defined URL aliases.
poll_votes in modules/poll/poll.module
Callback for the 'votes' tab for polls you can see other votes on
statistics_node_tracker in modules/statistics/statistics.module

... See full list


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


function tablesort_sql($header, $before = '') {
  $ts = tablesort_init($header);
  if ($ts['sql']) {
    $sql = db_escape_string($ts['sql']);
    $sort = drupal_strtoupper(db_escape_string($ts['sort']));
    return " ORDER BY $before $sql $sort";