Received: from localhost (unknown [200.46.204.183]) by mail.postgresql.org (Postfix) with ESMTP id 68AC8632EAA for ; Sat, 18 Apr 2009 18:31:30 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by localhost (mx1.hub.org [200.46.204.183]) (amavisd-maia, port 10024) with ESMTP id 55797-02 for ; Sat, 18 Apr 2009 18:31:28 -0300 (ADT) X-Greylist: from auto-whitelisted by SQLgrey-1.7.6 Received: from mail.morphoss.com (hoppy.mcmillan.net.nz [202.78.240.82]) by mail.postgresql.org (Postfix) with ESMTP id 889F9632559 for ; Sat, 18 Apr 2009 18:31:28 -0300 (ADT) Received: from localhost (localhost [127.0.0.1]) by mail.morphoss.com (Postfix) with ESMTP id AEFAA2312E; Sun, 19 Apr 2009 09:31:23 +1200 (NZST) X-Virus-Scanned: Debian amavisd-new at mail.morphoss.com Received: from mail.morphoss.com ([127.0.0.1]) by localhost (hoppy.mcmillan.net.nz [127.0.0.1]) (amavisd-new, port 10024) with LMTP id zZKyu4rPd7iu; Sun, 19 Apr 2009 09:31:19 +1200 (NZST) Received: from [192.168.55.193] (218-101-8-114.xtreme.net.nz [218.101.8.114]) (Authenticated sender: andrew@mail.morphoss.com) by mail.morphoss.com (Postfix) with ESMTPSA id 1FF492312C; Sun, 19 Apr 2009 09:31:19 +1200 (NZST) Subject: Re: sorting query results From: Andrew McMillan To: Piotr Mitoraj Cc: pgsql-php@postgresql.org In-Reply-To: <49E9CFBF.4050808@gmail.com> (sfid-20090419_010444_776307_BC32543F) References: <49E9CFBF.4050808@gmail.com> (sfid-20090419_010444_776307_BC32543F) Content-Type: text/plain Date: Sun, 19 Apr 2009 09:31:15 +1200 Message-Id: <1240090275.9028.948.camel@happy.mcmillan.net.nz> Mime-Version: 1.0 X-Mailer: Evolution 2.24.5 Content-Transfer-Encoding: 7bit X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=0 tagged_above=0 required=5 tests=none X-Spam-Level: X-Archive-Number: 200904/10 X-Sequence-Number: 4015 On Sat, 2009-04-18 at 16:03 +0300, Piotr Mitoraj wrote: > Hi, > failing to find the straight-forward answer on the web I hope to find > some advice in here. > I want to display the table fetched from the postgres database. The > headers of the columns are links used to sort the data. Like the > functionality you have on ebay when you sort according to price or time > left. I recognized two ways of doing that: > > 1. Issue a new query to the database each time the specific sorting was > requested. In other words, let the database do the sorting and fetch the > results. > > 2. Save the table into php array and and use php built-in functions to > sort and refresh the table. > > Which of those methods would be most desired in terms of performance and > according to "the best practices"? > What would be possible drawbacks/advantages of those methods. Personally I've found it easiest to implement the sorting in database. The reason this is easier is that the programming involved is just a small change to the 'ORDER BY' clause in the SQL, whereas the sorting in PHP would often change the whole way the query results are processed. Another approach I have seen used, which you don't mention, is to do the sorting in javascript, client side. This has the advantage that the database is only queried the first time and all re-sorting is done client-side. In this case sorting on things like time/date data can be more problematic, and of course you're depending client-side behaviour also. Cheers, Andrew. ------------------------------------------------------------------------ andrew (AT) morphoss (DOT) com +64(272)DEBIAN Powering the .NZ namespace with Open Source Software ------------------------------------------------------------------------