You are here: Best way to sort arrays « PHP Programming Language « IT news, forums, messages
Best way to sort arrays

Posted by floortje on 10/31/07 19:38

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

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация