pg.ddx.io  pgsql-php@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: L. Franklin <lfranklin@lumatila.com>
To: 'Piotr Mitoraj' <piotr.mitoraj@gmail.com>
To: pgsql-php@postgresql.org
Subject: Re: sorting query results
Date: Sat, 18 Apr 2009 09:40:34 -0500
Message-ID: <55C5FCC7F739418594160863C4FB589E@lukasswork> (raw)
In-Reply-To: <49E9CFBF.4050808@gmail.com>
References: <49E9CFBF.4050808@gmail.com>

Piotr, 

-----Original Message-----
From: Piotr Mitoraj [mailto:piotr.mitoraj@gmail.com] 
Sent: Saturday, April 18, 2009 8:04 AM
To: pgsql-php@postgresql.org
Subject: [PHP] sorting query results

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.
-----------------------------------------

Assuming that you are displaying the entire result set in the browser at
once, I would strongly recommend doing the sorting in javascript in the
browser.  My personal favorite way of doing this is to use the jquery
javascript library coupled with the tablersorter plugin.  With these 2
libraries you can add sorting to your HTML table with a single line of
javascript code.

If you need to provide pagination through the resultset in your web page
then you need to use one of the 2 approaches you listed.  Using arrays and
keeping the data in php will be faster but you will be limited by php's
available memory as to the size of resultset that you can support this way,
so you will need to add some size checking to your resultset and disable the
php array method if it's too big.  The database query method will always
work regardless of result set size but will generally provide the slowest
results.

Lukass





view thread (7+ messages)  latest in thread

Message-ID: <55C5FCC7F739418594160863C4FB589E@lukasswork>
Permalink:  ../55C5FCC7F739418594160863C4FB589E@lukasswork/
Also on:    postgresql.org/message-id/55C5FCC7F739418594160863C4FB589E@lukasswork

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-php@postgresql.org
  Cc: lfranklin@lumatila.com, piotr.mitoraj@gmail.com
  Subject: Re: sorting query results
  In-Reply-To: <55C5FCC7F739418594160863C4FB589E@lukasswork>
  References: <49E9CFBF.4050808@gmail.com> <55C5FCC7F739418594160863C4FB589E@lukasswork>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox