Sort Columns in tables

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
facets
Forum Contributor
Posts: 273
Joined: Wed Apr 13, 2005 1:53 am
Location: Detroit

Sort Columns in tables

Post 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>";
Sphen001
Forum Contributor
Posts: 107
Joined: Thu Mar 10, 2005 12:24 pm
Location: Land of the Beaver

Post 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:
Last edited by Sphen001 on Sun Jul 03, 2005 6:35 am, edited 1 time in total.
User avatar
neophyte
DevNet Resident
Posts: 1537
Joined: Tue Jan 20, 2004 4:58 pm
Location: Minnesota

Post 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.
Post Reply