tags:

views:

67

answers:

2

How would you go about implementing an array design and function to achieve the following table in php. The data would be drawn from a mysql database but i would like to limit the number of mysql queries required and therefore use a formatted array of some sort.

-----------------------------------------
|           | 2009 | 2008 | 2007 | 2006 |
-----------------------------------------
| country a | d d  |      | doc  | doc  |
| country b |      | doc  | doc  |      |
| country c | doc  | d d  |      | doc  |
| country d | doc  |      | d d  |      |
-----------------------------------------

Where both d & doc are documents. And any date(y) / country can have many documents. The resulting table would be an html table.

+1  A: 

You should not be using arrays to manipulate or group data if you can help it. Try to get mySQL to do all the heavy lifting.

For example:

...GROUP BY YEAR(item_date)

Have a look at the Time Functions in mySQL.

Soviut
A: 

use uksort to sort by keys with a callback

in the callback simply parse the date to a timestamp and use a simple comparison

function cmp($a, $b) { global $array; return strcmp($array[$a]['db'], $array[$b]['db']); }

uksort($array, 'cmp');

chandru_cp