Forum Moderators: coopster
$query = 'select *, sum(value) from table1, table2, table3 where table1.blah = table2.blah and table2.blee = table3.blee group by zone';
$result = mysql_query($query);
if ($result && mysql_num_rows($result)) {
$num_results = mysql_num_rows($result);
echo '<P>There are '.$num_results.' zones: <P>';
while ($row = mysql_fetch_array($result)) {
echo 'Zone ID: '.$row['zone'];
echo '<BR>Total Amount for This Zone: '.$row['value'];
echo '<HR>';
}
// This is where I want to echo the grand total.
}
else {
echo 'Sorry, there are no results.';
}
select *, sum(value), (select sum(value) from table1, table2, table3
where table1.blah = table2.blah
and table2.blee = table3.blee) as grandtotal
from table1, table2, table3
where table1.blah = table2.blah
and table2.blee = table3.blee
group by zone
you're retrieving an associative array of one row from the database table. Each time you loop, it overwrites the previous data. When mysql has no more row data to give you, it returns false, which is why you can't echo $row['value'] after the while loop.
You could also sum your values as you're executing the loop. Set a variable to zero before the loop, and inside the loop add $row['value'] to it.
you can't echo $row['value'] after the while loop.
Your suggestion did the trick, I just set a variable $blah = 0 outside the loop, and inside just did $blah = ($blah + $row['value']); echoing $blah at the end, outside the loop, returns the grand total just like I wanted. Thanks!