RE: Two Column Sort

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On 04 March 2004 19:04, kc68@xxxxxxxxxxx wrote:

> On Thu, 4 Mar 2004 10:55:20 -0000, Ford, Mike               [LSS]
> <M.Ford@xxxxxxxxxxxxxx> wrote:
> 
> > On 03 March 2004 17:07, kc68@xxxxxxxxxxx wrote:
> > 
> > > Here's the php code for the sort command:
> > > 
> > > echo "<td><div align='center'><font size='3'><b>CBC <a
> > > href='contributions22504.php?sort_field=cbc,
> > > net_receipts&sort_order=desc'
> > > target='_self'><font size='2'>Sort</a></b></font></div></td>\n";
> > > 
> > > Here's the basic setup that begins the script:
> > > 
> > > # get sort order (if any) passed to script
> > > $sort_field = $_REQUEST['sort_field'];
> > > if (! $sort_field) {$sort_field = "State";}
> > > $sort_order = $_REQUEST['sort_order'];
> > > if (! $sort_order) {$sort_order = "asc";}
> > 
> > Well, you're going to need an order phrase that looks like this:
> > 
> >     ORDER BY CBC DESC, NET_RECEIPTS ASC
> > 
> > so you may need a little rethink about how you're passing those
> > arguments. 
> > 
> > Cheers!
> > 
> > Mike
> > 
> Thanks, but you're past my level.  Can you show me the exact coding I
> should use?

Well, not really, as there are several ways you could approach this and the one you choose will depend on your exact application, personal preference, and whether there's an "r" in the month ;)

>  Apparently I can't just replace SORT_FIELD=CBC,
> NET_RECEIPTS&SORT_ORDER=DESC with ORDER BY CBC DESC, NET_RECEIPTS ASC

Well, you can, but the question is more whether that's what you want to do.

My original point was that if your URL has a query string that looks like

    ?sort_field=cbc, net_receipts&sort_order=asc

then you're going to get your variables set like this:

    $sort_field = 'cbc, net_receipt';
    $sort_order = 'asc';

but you need to interpolate an additional 'desc' into the sort order string to get the CBC column to sort the way you want, so that the query ends up like:

    SELECT ... ORDER BY cbc DESC, net_receipts asc

Whilst this is possible using PHP string functions (or regexes), it doesn't seem like the easiest or most flexible method.  A couple of alternatives off the top of my head might be:

(i) just pass the full ORDER BY phrase (...?sort_order=cbc desc, net_receipts asc)

(ii) pass multiple parameters each with one column name and sort order (either ...?sort1=cbc desc&sort2=net_receipts asc, or ?sort[]=cbc desc&sort[]=net_receipts asc)

(iii) pass a parameter with a set of column names, and another with a matching set of sort orders (...?sort_field=cbc,net_receipts&sort_order=desc,asc).  This requires you to break each set apart (by explode()ing on the comma, say), then reassemble them into a suitable ORDER BY phrase.  This option probably also requires you to do the most validation, as you have to decide what happens when one set has more items in it than the other, or is misformed in some other way, and so on.

Of course, you might decide that column CBC always wants to be sorted descending regardless, in which case looking for it in the parameters and interpolating the required DESC keyword is probably the way to go after all...!!

At the end of the day, only you can decide which of these is appropriate (or if some other solution is), but I hope I've given you some pointers to help you make that decision.

Cheers!

Mike

---------------------------------------------------------------------
Mike Ford,  Electronic Information Services Adviser,
Learning Support Services, Learning & Information Services,
JG125, James Graham Building, Leeds Metropolitan University,
Beckett Park, LEEDS,  LS6 3QS,  United Kingdom
Email: m.ford@xxxxxxxxxxxxxx
Tel: +44 113 283 2600 extn 4730      Fax:  +44 113 283 3211 

-- 
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [PHP Users]     [Postgresql Discussion]     [Kernel Newbies]     [Postgresql]     [Yosemite News]

  Powered by Linux