Jump to content

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

$totrow = 0

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 $cr2["total"];

}

 

echo "total: $totrow";

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
     
?>

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.