TROUBLE WITH (TOTAL) SUM

A

Anonymous

Guest
I hope someone can help me with this problem.

1] ...while ($r=mysql_fetch_array($res)){

$Total_Trailer = $r["Qt_Product"] * $r["Price_Ex"];
echo "".sprintf("%.2f",$Total_Trailer)."";
}
(I put Qt_Product, Price_Ex and Total_Trailer in a 3 colums table - of course I get at least 2 rows or more back in this table - )

2] what I need now is to make the sum of all these results the entire colum ($Total_Trailer) to get the "grand total".

I hope I've been clear enough, otherwise please contact me if you feel you need some more explaination / details...
Thanks to all of you.
Erick.
 
Hi!
well, just modify your script a little bit.

Code:
while ($r=mysql_fetch_array($res)){
   $Total_Trailer = $r["Qt_Product"] * $r["Price_Ex"];
   echo "".$my_number = sprintf("%.2f",$Total_Trailer)."<br>";
   $sum += $my_number;
}
echo "Total sum --> ".$sum;

I hope it helped.
 
Joan Garnet said:
Hi!
well, just modify your script a little bit.

Code:
while ($r=mysql_fetch_array($res)){
   $Total_Trailer = $r["Qt_Product"] * $r["Price_Ex"];
   echo "".$my_number = sprintf("%.2f",$Total_Trailer)."<br>";
   $sum += $my_number;
}
echo "Total sum --> ".$sum;

I hope it helped.


Thanks a lot.
This script works, but print from the second row the sum result of the 2 rows uppon.
What I do need is to print the TOTAL of all the rows in a row (Grand total) after the last printed row from the DataBase.

Can somebody help me?

Thanks.
 
I don't undersand what you want...
Maybe something like this??

Code:
while ($r=mysql_fetch_array($res)){ 
   $Total_Trailer = $r["Qt_Product"] * $r["Price_Ex"]; 
   echo "".$my_number = sprintf("%.2f",$Total_Trailer)."<br>"; 
   $sum += ($r["Qt_Product"] + $r["Price_Ex"]); 
} 
echo "Total sum --> ".$sum;

There aren't many other possible operations with those variables...
;)
 
Joan Garnet said:
I don't undersand what you want...
Maybe something like this??

Code:
while ($r=mysql_fetch_array($res)){ 
   $Total_Trailer = $r["Qt_Product"] * $r["Price_Ex"]; 
   echo "".$my_number = sprintf("%.2f",$Total_Trailer)."<br>"; 
   $sum += ($r["Qt_Product"] + $r["Price_Ex"]); 
} 
echo "Total sum --> ".$sum;

There aren't many other possible operations with those variables...
;)

I guess I haven't been that clear... the best is an exemple

Exemple:
..... $Total_Trailer = $r["Qt_Product"] * $r["Price_Ex"];
$Qt_Product=10, $Price_Ex= 12, $Total_Trailer=120
$Qt_Product=20, $Price_Ex= 24, $Total_Trailer=480
$Qt_Product=30, $Price_Ex= 36, $Total_Trailer=1080
_____________________________________________
I NEED : $GTotal_Trailer = 1680

With your script I print beside $Total_Trailer as $my_number :
120 - 600 - 1680

I hope it's a bit clearer...
I know I'm not the best in PhP, but if somebody can show me how I can add these '$Total_Trailer' all together, I'll appreciate.
Thanks,
Erick
 
Pejone said:
Erick, you are alredy have all that you need, very easy you can do that you want...

Hi Pejone,

Glad to hear it's easy ! I won't have any problem then to understand how it works.... so now show me please.
 
same code

Code:
while ($r=mysql_fetch_array($res)){ 
   $Total_Trailer = $r["Qt_Product"] * $r["Price_Ex"]; 
   $All_Total_Trailer=$All_Total_Trailer+$Total_Trailer;
} 

echo "Total ".$All_Total_Trailer;
 
Pejone said:
same code

Code:
while ($r=mysql_fetch_array($res)){ 
   $Total_Trailer = $r["Qt_Product"] * $r["Price_Ex"]; 
   $All_Total_Trailer=$All_Total_Trailer+$Total_Trailer;
} 

echo "Total ".$All_Total_Trailer;


THANK YOU VERY MUCH !! Now I see the code, it seems logical and easy to me ! I guess we all have the same feeling afterwards !
Anyway....It works perfectly - within a few minuts I was done with my script. I now need to calculate the VAT (this is not a problem) but it must be calculated only if the country entered by the user in his profile is a particular one, otherwise I print in the VAT field : XXXXXXX.
So I gonna try to do that by myself (with some kind of If statment), and if I get some trouble, I'll come back to you, if you don't mind.

Thanks again,
Erick
 
Erick020 said:
Pejone said:
same code

Code:
while ($r=mysql_fetch_array($res)){ 
   $Total_Trailer = $r["Qt_Product"] * $r["Price_Ex"]; 
   $All_Total_Trailer=$All_Total_Trailer+$Total_Trailer;
} 

echo "Total ".$All_Total_Trailer;


THANK YOU VERY MUCH !! Now I see the code, it seems logical and easy to me ! I guess we all have the same feeling afterwards !
Anyway....It works perfectly - within a few minuts I was done with my script. I now need to calculate the VAT (this is not a problem) but it must be calculated only if the country entered by the user in his profile is a particular one, otherwise I print in the VAT field : XXXXXXX.
So I gonna try to do that by myself (with some kind of If statment), and if I get some trouble, I'll come back to you, if you don't mind.

Thanks again,
Erick
 
Back
Top