Sorting by multiple columns


/ Published in: PHP
Save to your folder(s)

Use instead of standard "sql\_tablesort()" function. Handles multiple "sort" in $header, but the arrow will be only on last sorted column. This can be handled with rewriting theme_table, for example.


Copy this code and paste it in your HTML
  1. function multi_tablesort($header = array()) {
  2. $sorts = array('asc', 'desc');
  3. $sorter = '';
  4. $tablesort = array();
  5.  
  6. foreach($header AS $field) {
  7. if (in_array($field['sort'],$sorts)) {
  8. $tablesort[] = db_escape_string($field['field']) . ' ' . drupal_strtoupper(db_escape_string($field['sort']));
  9. }
  10.  
  11. }
  12.  
  13. if (count($tablesort) > 0) {
  14. $sorter = 'ORDER BY ' . implode(', ', $tablesort);
  15. }
  16.  
  17. return $sorter;
  18. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.