0

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;

        }
    }   

}
}   
1
  • You should define $sum outside of the loop, you're just overriding it each time. Commented Aug 1, 2016 at 13:52

3 Answers 3

1

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

Sign up to request clarification or add additional context in comments.

2 Comments

echo $sum; must be outside the loop not in the loop
Right now you have echo 1 and echo 2 which gives you 1 and 2 which is 12 ;)
1

Several people have pointed out that $sum should be outside the loop, and that is indeed correct. However, MySQL can do this all for you:

    $orderfeed_query = mysql_query("SELECT SUM(signaturewiz + flurstueckwiz + uploadwiz + exsignaturewiz + ibanwiz) FROM orderfeed WHERE orderid = '$orderid' AND product = '1'") or die(mysql_error());
    if ($row = mysql_fetch_row($orderfeed_query)) {
        echo $row[0];
    }

4 Comments

@Gimo You've posted the same comment to all three answers. That kinda makes me think the actual sum of all the fields in the selection really is 12. What happens when you var_dump($row);print "<hr/>"; ' in place of the echo()` in your original example?
array(16) { ["id"]=> string(1) "1" ["orderid"]=> string(10) "1468863981" ["product"]=> string(1) "1" ["check_order"]=> string(5) "11100" ["productamount"]=> string(2) "39" ["strasse"]=> string(9) "Ackerstr." ["hausnummer"]=> string(1) "1" ["plz"]=> string(5) "32051" ["ort"]=> string(7) "Herford" ["koordinaten"]=> string(0) "" ["regionalschluessel"]=> string(0) "" ["signaturewiz"]=> string(1) "1" ["flurstueckwiz"]=> string(1) "0" ["uploadwiz"]=> string(1) "0" ["exsignaturewiz"]=> string(1) "0" ["ibanwiz"]=> string(0) "" }
and array(16) { ["id"]=> string(1) "7" ["orderid"]=> string(10) "1470041156" ["product"]=> string(1) "1" ["check_order"]=> string(5) "10000" ["productamount"]=> string(2) "39" ["strasse"]=> string(13) "Aachener Str." ["hausnummer"]=> string(1) "1" ["plz"]=> string(5) "10178" ["ort"]=> string(6) "Berlin" ["koordinaten"]=> string(0) "" ["regionalschluessel"]=> string(0) "" ["signaturewiz"]=> string(1) "1" ["flurstueckwiz"]=> string(1) "0" ["uploadwiz"]=> string(1) "1" ["exsignaturewiz"]=> string(1) "0" ["ibanwiz"]=> string(0) "" }
when a wiz have a "1" it is activate a message and i like to sum all new messages, it works by 1 orderid but not by multiple orderids
1

Move the echo of $sum outside the loop

Use += rather than + to accumulate a total over multiple iterations

Initialize your variable before using += as if $sum has an undefined value it can mess up the count when using +=

    $sum = 0;   // init variable
    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;

ADDITIONAL INFO:

You see 12 and not 3 so the data from your table I assume is text and not numeric so do this to convert text numbers to integers

    $sum = 0;   // init variable
    while ($row = mysql_fetch_assoc($orderfeed_query)) {
        $signaturewiz   = (int)$row["signaturewiz"];
        $flurstueckwiz  = (int)$row["flurstueckwiz"];
        $uploadwiz      = (int)$row["uploadwiz"];
        $exsignaturewiz = (int)$row["exsignaturewiz"];
        $ibanwiz        = (int)$row["ibanwiz"];

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

3 Comments

You do want to get the total of ALL the columns in ALL the rows in your result set, Right? Or am I confused
i like to sum all values of the rows: $signaturewiz = $row["signaturewiz"]; $flurstueckwiz = $row["flurstueckwiz"]; $uploadwiz = $row["uploadwiz"]; $exsignaturewiz = $row["exsignaturewiz"]; $ibanwiz = $row["ibanwiz"];, the result of the first order is 1 the second is 2. but i see just 12 not 3.
See additional Info RE: Non numeric data from the database

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.