|  | Posted by Richard Levasseur on 02/13/06 21:10 
I often have this requested as a feature of the things i write.I've yet to come up with a simple elegant solution.
 
 Most often i simply hack it in.  Pass a ?sort=name&dir=asc from a link,
 store that in a session, and append a ORDER BY clause to all the
 relevent queries.
 It is a pain, but it gets the job done.
 
 Other solutions:
 Create a SortBy array and have each element an array of column name and
 direction.
 $sort[0] = array('col'=>'first_name', 'dir'=>'ASC');
 Then implode it and concatenate it.  Messy, but a quick fix if you have
 a lot of subsorts needed.
 Extending that, create a SortBy class, and append that into all the
 relevent queries, propagating it through $SESSION.
 $sort = new SortBy($col, $dir);  $sort->addCol($col2, $dir2);
 $query = "select bla bla $sort->toString()
 This lets you easily manipulate the sorted direction and allows more
 control over the sort.  Basically the previous thing but with a
 friendly interface you can reuse in other projects.
 
 My only other solution is to abstract the query so that you can do
 whatever the hell you want to it.
 $query = new Query();
 $query->Select($col1, $col2);
 $query->SortBy($col1, $dir);
 $query->toString();
 Granted, that requires a lot of work, but is the easiest one once its
 all said and done.
 In fact, i think with the PDO objects you can do just that.  I'm not
 sure, though, I haven't really looked at those.
 
 Finally, often enough they say they want sort, but they will never use
 it.  Really they just want to see specific results in specific ways.
 I would say wringing as many details for reports as possible out of
 them is a better way to go.  Its easier for you to do, and they'll like
 the final results better.
  Navigation: [Reply to this message] |