Jump to content

[SOLVED] Get Total From While Loop


dubc07

Recommended Posts

I'm trying to get the total for each product and add them for a Grand Total to list outside the loop or only echo once I can do the addition but it will echo 5 time in the loop.

How would I go about doing this?

Thanks in advance for your help.

<?php 

$cart2="SELECT * FROM products WHERE pro_indid='glass'";
	  
$res2 = mysql_query($cart2) or die('Error, query failed 2');
  
while($cr2=mysql_fetch_array($res2)){
	  
$totrow=$cr2["total"];
///the $totrow will echo below 5 time each price in database 5.67 which the total should be 28.35 //
echo $totrow;
}

///Output of total would be $28.35
  
  ?>

The output I'm looking for would be like so

 

5.67

5.67

5.67

5.67

5.67

 

GrandTotal:28.35

 

Link to comment
https://forums.phpfreaks.com/topic/180516-solved-get-total-from-while-loop/
Share on other sites

This should work:

 

<?php 

$cart2="SELECT * FROM products WHERE pro_indid='glass'";
$res2 = mysql_query($cart2) or die('Error, query failed 2');

$total = 0; //initialize the total

while($cr2=mysql_fetch_array($res2)){

echo $cr2['total'].'<br>'; //show each total with a break

$total += $cr2['total']; //add the value of the row to the total
}

echo $total; //show the total
     
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.