Forum Moderators: coopster
Ok. So I have a report that needs to be based on a query which will produce perhaps 50-150 records.
I then need to lay those records out in a tabular sort of format, and do some other twisty bits with it - producing averages and such.
Because of the report layout and the data is a bit irregular (not all items will have data in all years) just looping through the query results to lay out in the tabular format isn't going to work readily, at least not without running a subquery to pull every darn record individually. Way too heavy.
So, brainwave - dump the data in an array and pull it out as needed on the page. eg:
Array([0] => Array([itemid] => 1
[year] => 2003
[grosssales] => 36792
[netsales] => 25758)
[1] => Array([itemid] => 1
[year] => 2004
[grosssales] => 107640
[netsales] => 75348)
[2] => Array([itemid] => 1
[year] => 2005
[grosssales] => 77958
[netsales] => 55350)
[3] => Array([itemid] => 1
[year] => 2002
[grosssales] => 120690
[netsales] => 78444)
[4] => Array([itemid] => 2
[year] => 2003
[grosssales] => 80442
[netsales] => 57924)
[5] => Array([itemid] => 2
[year] => 2005
[grosssales] => 103986
[netsales] => 77994)
[6] => Array([itemid] => 3
[year] => 2007
[grosssales] => 41580
[netsales] => 35748)
Question. How do I then access each piece of data within the array as needed?
eg, I need at a particular point to show the gross sales figure for itemid 2 for the year 2003.
help? please?
//the total gross
$totalGross = int;
foreach($arr as $tempArr){
if($tempArr["itemid"] == "2" && $tempArr["year"] == 2003){
//sum up the total gross
$totalGross = (int)$tempArr["grosssales"] + (int)$totalGross;
}//if temparr
}//foreach
echo("The total gross sales is $totalGross");
I guess I was mainly wondering if there is a more direct/efficient way to search the array than looping through foreach, but looking at it again it's not really heavy code is it?
Not practical to store the data in a table in this case btw - grosssales/netsales is just a simplified example and there are so many factors in the numbers I need to pull that keeping the table up to date would be a bit of a nightmare.