order by tables question

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Neal

    order by tables question

    I just made a table sortable by column headers by reading a thread
    posted here a while ago. Here is the code simplified:

    $sort_order = 'id';
    if($order == 'jobtitle'){
    $sort_order = 'jobtitle';
    }else if($order == 'district'){
    $sort_order = 'district';
    }
    else if($order == 'date'){
    $sort_order = 'date';
    }

    "SELECT * FROM table ORDER BY $sort_order ASC";

    It works just as I thought it would. But, my question is how do I make
    it so 'id' (set to auto increment) or 'date' defaults to DESC (so the
    most recent post is always on top), while at the same time the links on
    the column headers sorts by ASC (like it currently does).
    Thanks.
  • Malcolm Dew-Jones

    #2
    Re: order by tables question

    Neal (pissed@comcast .net) wrote:
    : I just made a table sortable by column headers by reading a thread
    : posted here a while ago. Here is the code simplified:

    : $sort_order = 'id';
    : if($order == 'jobtitle'){
    : $sort_order = 'jobtitle';
    : }else if($order == 'district'){
    : $sort_order = 'district';
    : }
    : else if($order == 'date'){
    : $sort_order = 'date';
    : }

    : "SELECT * FROM table ORDER BY $sort_order ASC";

    : It works just as I thought it would. But, my question is how do I make
    : it so 'id' (set to auto increment) or 'date' defaults to DESC (so the
    : most recent post is always on top), while at the same time the links on
    : the column headers sorts by ASC (like it currently does).
    : Thanks.

    I'm not 100% clear on what you mean, but perhaps something like the
    following minor variation would work better for you

    $sort_order = 'id DESC';
    if($order == 'jobtitle'){
    $sort_order = 'jobtitle ASC';
    }else if($order == 'district'){
    $sort_order = 'district ASC';
    }
    else if($order == 'date'){
    $sort_order = 'date DESC';
    }

    "SELECT * FROM table ORDER BY $sort_order";


    --

    This space not for rent.

    Comment

    • Neal

      #3
      Re: order by tables question

      Malcolm Dew-Jones wrote:
      [color=blue]
      > I'm not 100% clear on what you mean, but perhaps something like the
      > following minor variation would work better for you
      >
      > $sort_order = 'id DESC';
      > if($order == 'jobtitle'){
      > $sort_order = 'jobtitle ASC';
      > }else if($order == 'district'){
      > $sort_order = 'district ASC';
      > }
      > else if($order == 'date'){
      > $sort_order = 'date DESC';
      > }
      >
      > "SELECT * FROM table ORDER BY $sort_order";
      >
      >
      > --
      >[/color]

      Exactly what I was looking for (I should've known).
      Thanks.

      Comment

      Working...