Sorting rows from a database in reverse order
I have a question about sorting rows returned by an SQL query. I'm running Zope 2.0 with a Z MySQL DB connection. display_news is a Z SQL Method that contains the following query: SELECT DATE_FORMAT(date, '%b %d, %Y') as date, headline, url FROM news I use display_news in the following DTML fragment: <dtml-in display_news sort=date> <a href="<dtml-var url>"><dtml-var headline></a><br> <dtml-var date> <br><br> </dtml-in> When I view the page containing the above DTML, the news headlines are displayed such that the oldest news is at the top of the page and the newest is at the bottom. Now, I know that I can use "ORDER BY date DESC" in the SQL query and remove the "sort=date" dtml-in parameter to accomplish the task, but I'm not sure that it's really the most efficient way, nor that it will always work. So here are the questions whose answers will settle the matter for me: 1. Will dtml-in always iterate over the rows display_news returns in order? 2. If so, is there a way for dtml-in to reverse the sort order? 3. If so, is it more efficient to let Zope or MySQL sort the data? Thanks, Ben
At 22:43 22/09/99 , Ben Glazer wrote:
1. Will dtml-in always iterate over the rows display_news returns in order?
It will iterate over the list in the order it received it. If your wuery returns them in order, they will be displayed in order.
2. If so, is there a way for dtml-in to reverse the sort order?
Yes, just add the 'reverse' attribute to your in tag: <dtml-in display_news sort=date reverse> <a href="<dtml-var url>"><dtml-var headline></a><br> <dtml-var date> <br><br> </dtml-in>
3. If so, is it more efficient to let Zope or MySQL sort the data?
Depends on the length of the returned set, but if you indexed your tables properly, the DB will be faster. -- Martijn Pieters, Web Developer | Antraciet http://www.antraciet.nl | Tel: +31-35-7502100 Fax: +31-35-7502111 | mailto:mj@antraciet.nl http://www.antraciet.nl/~mj | PGP: http://wwwkeys.nl.pgp.net:11371/pks/lookup?op=get&search=0xA8A32149 ------------------------------------------
participants (2)
-
Ben Glazer -
Martijn Pieters