Forum Moderators: coopster
I am pulling data from a MySQL database. I want to print out all individual rows, then total them. My use of arrays and sum of those arrays has me baffled. I want to print out one total value that sums all data from field "sales_total"
The following prints the LAST row in the result set only.
Help!
Thank you.
<?
$result = mysql_query ("SELECT query goes here");
$myrow=mysql_fetch_array($result);if ($myrow)
{
print "<ul>
$myarray = array($myrow ["sales_total"]);
$sum = array_sum($myarray);
do
{
$salestotal= $myrow ["sales_total"];
print "<li>$salestotal</li>";
}
while ($myrow=mysql_fetch_array($result));
print "</ul>$sum\n";
}
else
{
print "<h1>No results</h1>\n";
}
?>
$result = mysql_query('SELECT sales_total FROM table');
if (mysql_num_rows($result) > 0) {
print '<ul>';
$sum = array(); // initialize
while ($myrow = mysql_fetch_array($result)) {
$sum[] = $myrow['sales_total']; // sum
$salestotal= $myrow['sales_total'];
print "<li>$salestotal</li>";
}
$sum = array_sum($sum);
print "</ul>$sum\n";
} else {
print "<h1>No results</h1>\n";
}