Re: Best way to sort arrays



On Oct 31, 2:38 pm, floortje <n...@xxxxxxxxx> wrote:
Hi,

On my page I oftend find myself presenting my data like this

- all entries
1 - first (last modified)
2 - second
3 - last

- last entries
3 - last

- last modified
1 - first (last modified)

I have found a couple of ways to solve this

The first is 3 different queries
$allentries=$db::select('SELECT * FROM entries')
$lastentries=$db::select('SELECT * FROM entries ORDER BY id DESC LIMIT 5')
$lastmodefiedentries=$db::select('SELECT * FROM entries ORDER BY
modefied DESC limit 5')

$allentries=$db::select('SELECT * FROM entries')
foreach($allentries as $val)
{
//
$lastentries[$val['id']]=$val;
$lastmodefiedentries[$val['modefied']]}

// sort by id
krsort($lastentries)

// sort by modefied
krsort($lastmodefiedentries)

// etc

But I cant help wondering there should be a more efficient way to solve this.

In the first solution I use 2 extra queries and in the last soution I
use more system resources and it isn't really reusable.

Anyone know a better way ?

Floortje

Could you not cut out the first query? Just in your second query lose
the 'limit' clause and then when you print "all entries", use the
$lastentries result. If ordered backwards, start from the last key and
iterate backwards, I suppose.

And then when you print part 2, just take the first 5.

Other than that, I would go with the 3 queries. Is there a need for it
to be much faster?

.



Relevant Pages

  • Re: Check boxes and union queries
    ... You might want to provide and explain your SQL of the queries. ... MS Access MVP ... then all the entries that are not checked come ... > My second query uses the first query as a union - I want ...
    (microsoft.public.access.queries)
  • Check boxes and union queries
    ... I have two queries: ... then all the entries that are not checked come ... My second query uses the first query as a union - I want ... to sum up all the checked responses as one total and all ...
    (microsoft.public.access.queries)
  • Unmatched Query Wizard
    ... entries in 2 different tables. ... for other queries, 'Is Null' criteria works fine. ... field 2 [assigned tasks] ...
    (microsoft.public.access.queries)
  • RE: I have another question. Sorry.
    ... Christi Lee ... Generally you want to get valid data in as soon as you can. ... uses of your data where you don't want to look at the newest entries, ... queries, forms etc.) and then should take only seconds of your time to "do." ...
    (microsoft.public.access.tablesdbdesign)
  • 2003 - Address Book
    ... How can I make the address book include all contacts entries, ... Failing that, is there a quick way to enter a dummy email address into the empty ... Checked by AVG anti-virus system. ...
    (microsoft.public.outlook)