1

In my PHP page, I have a while loop that displays the Total of Orders of Customers Table. I want to get the sum of all the Totals values in while loop. This is how the relevant sections of my code look at the moment:

<?php

include'includes/config.php';
$sql ="Select * from new_booking order by order asc";
$re = mysqli_query($mysqli,$sql);

while($row=mysqli_fetch_array($re)) { 
    $order    =   $row['order'];
    $duration =   12;
    $total = $order * $duration;

    echo " <p> $total </p>";
  // echo "<p> All Sumtotals should display here </p> ";

}

?> 

7*12=84
8*12=96
Sum total = 180

3
  • I guess you just need to make one more global var outside of your while loop and add your total to that each iteration. Commented Jan 20, 2019 at 14:16
  • Use another variable inside while to add total values, then show it outside while loop, i.e $total = $order * $duration; $GrandTotal+=$total; Commented Jan 20, 2019 at 14:16
  • 1
    Do you want to show the rows total per row and the sum total last, or do you want to show the current sum total on each row? Commented Jan 20, 2019 at 14:22

5 Answers 5

4

Declare $total outside while loop as $total=0, write $total=$total+ ($order*$duration) inside loop

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

Comments

3

Keep track of the sum total in a new variable..

<?php

include'includes/config.php';
$sql ="Select * from new_booking order by order asc";
$re = mysqli_query($mysqli,$sql);
$sumTotal = 0;

while($row=mysqli_fetch_array($re)) { 
    $order    =   $row['order'];
    $duration =   12;
    $total = $order * $duration;
    $sumTotal = $sumTotal + $total;
    echo " <p> $total </p>";
    echo " <p> Running total $sumTotal </p>";
}
echo " <p> Grand total $sumTotal </p>";
?> 

Comments

2
<?php

include'includes/config.php';
$sql ="Select * from new_booking order by order asc";
$re = mysqli_query($mysqli,$sql);
$total = 0;
while($row=mysqli_fetch_array($re)) { 
    $order    =   $row['order'];
    $duration =   12;
    $total = $total + ($order * $duration);


}

    echo " <p> $total </p>";
  // echo "<p> All Sumtotals should display here </p> ";

?> 

Comments

1

Define a variable before the loop that will contain the sum total of all records:

$sumTotal = 0;

while($row = mysqli_fetch_array($re)) { 
    $order    = $row['order'];
    $duration = 12;
    $total    = $order * $duration;

    // Add this records total to the sum total
    $sumTotal += $total;

    echo "<p> $total </p>";
}    

echo "<p>Here's the sum total: $sumTotal</p>";

This will give you the total for each record and then the sum total of all records after.

If you rather want to see the sum total on each record (to see it increasing), then just echo $sumTotal instead of $total inside the loop.

Comments

1

If you don't need to print out each total one at a time, you could do this in the SQL statement instead, sum up all the order values (*12) and give it an alias to make it easier to access...

$sql = "SELECT SUM(`order` * 12) AS total FROM new_booking";
$run = mysqli_query($mysqli, $sql);
$row = mysqli_fetch_assoc($run);
echo $row['total'];

1 Comment

@Mohamed This is the wisest advice. This will sum all of the order amounts and multiply them by 12 to deliver the grand total with no php looping at all. Clean and good. For your information, order is a mysql reserved keyword which means you need to wrap it in backticks. dev.mysql.com/doc/refman/5.5/en/… Best advice is that you never use mysql reserved words as table columns.

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.