Page 1 of 1

Sort Columns in tables

Posted: Sat Jun 25, 2005 7:19 pm
by facets
Hi Gang,

I'm trying to use the following code to sort columns generated by a mysql query.
It doesn't sort the fields by default as it should or when you click the sort links.
It just reloads the page with no errors.

Any obvious errors in the code that you can see?

tia, Will.

Code: Select all

// In Common.inc file
$default_sort_order = 'ASC';
$default_order_by = 'colloPaperName';

// Globals
global $default_sort_order, $default_order_by, $sort_order, $order_by;

// Sort Commands
if(empty($order_by)) {
      $order_by_str = "ORDER BY $default_order_by";
      $order_by = $default_order_by;
   }
   else $order_by_str = "ORDER BY $order_by";
   if(empty($sort_order)) {
      $sort_order_str = $org_sort_order = $default_sort_order;
      $sort_order = 'DESC';
   }
   else {
      $sort_order_str = $org_sort_order = $sort_order;
      if($sort_order == 'DESC') $sort_order = 'ASC';
      else $sort_order = 'DESC';
   }

// Table SetUp

echo "<th width=150px valign=top align=left nowrap>
<a href=\"".$_SERVER['PHP_SELF']."\"?action=fnSearchResults&sort_order=\".$sort_order.\"&order_by=paperCategory\"'>
Paper Category</a></th>";

Posted: Sat Jul 02, 2005 9:07 pm
by Sphen001
Hi,

The problem is most likely your link. Try this:

Code: Select all

echo '<th width="150px" valign="top" align="left nowrap"><a href="' . $_SERVER['PHP_SELF'] . '?action=fnSearchResults&sort_order=' . $sort_order . '&order_by=paperCategory">Paper Category</a></th>';
Hope this helps :D

Sphen001

EDIT: Fixed my own syntax :wink:

Posted: Sat Jul 02, 2005 9:31 pm
by neophyte
You have to $_GET['var']. Unless register globals is on, which is doubtful you need to get the variable. $_GET['query_string_var_name'] will retrieve the data for you. I always fall back on var_dump(). If you think a variable should be available, make sure it is by echoing variables and var_dumping them to the screen where you think they should be availble. It's a great way to find problems when there are no syntax errors.