Gimo Gimo - 4 months ago 16
PHP Question

How to sum mysql values in multple while PHP

i like to sum all row values from the $orderfeed_query. But when i echo the $sum, i get just the sum from the last loop.

how can can i add the sum of all following loop on the $sum variable? I dont know how many loops a order have.

$checkorder = mysql_query("SELECT * FROM orders WHERE `email` = '$email' ") or die(mysql_error());
while ($row = mysql_fetch_assoc($checkorder)) {
$orderid = $row["orderid"];
$check_order = $row["check_order"];

if($check_order[0] == 1){

$orderfeed_query = mysql_query("SELECT * FROM orderfeed WHERE `orderid` = '$orderid' AND `product` = '1'") or die(mysql_error());
while ($row = mysql_fetch_assoc($orderfeed_query)) {
$signaturewiz = $row["signaturewiz"];
$flurstueckwiz = $row["flurstueckwiz"];
$uploadwiz = $row["uploadwiz"];
$exsignaturewiz = $row["exsignaturewiz"];
$ibanwiz = $row["ibanwiz"];

$sum = $signaturewiz+$flurstueckwiz+$uploadwiz+$exsignaturewiz+$ibanwiz;

echo $sum;

}
}

}
}

Answer

This code

$sum = $signaturewiz + $flurstueckwiz + $uploadwiz + $exsignaturewiz + $ibanwiz;

overwrites sum each time. You must add to sum not to overwrite

$sum += $signaturewiz + $flurstueckwiz + $uploadwiz + $exsignaturewiz + $ibanwiz;

And declare $sum = 0; before main loop