TROUBLE WITH (TOTAL) SUM

Ask about general coding issues or problems here.

Moderators: macek, egami, gesf

Erick020
New php-forum User
New php-forum User
Posts: 13
Joined: Thu Dec 26, 2002 6:20 am

TROUBLE WITH (TOTAL) SUM

Postby Erick020 » Thu Dec 26, 2002 6:39 am

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.

User avatar
Joan Garnet
Moderator
Moderator
Posts: 387
Joined: Sat Aug 03, 2002 2:56 am
Location: Mars
Contact:

Re: TROUBLE WITH (TOTAL) SUM

Postby Joan Garnet » Thu Dec 26, 2002 6:11 pm

Hi!
well, just modify your script a little bit.

Code: Select all

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.

Erick020
New php-forum User
New php-forum User
Posts: 13
Joined: Thu Dec 26, 2002 6:20 am

Re: TROUBLE WITH (TOTAL) SUM

Postby Erick020 » Sat Dec 28, 2002 5:10 am

Joan Garnet wrote:Hi!
well, just modify your script a little bit.

Code: Select all

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.

User avatar
Joan Garnet
Moderator
Moderator
Posts: 387
Joined: Sat Aug 03, 2002 2:56 am
Location: Mars
Contact:

Postby Joan Garnet » Sat Dec 28, 2002 2:05 pm

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

Code: Select all

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...
;)

Erick020
New php-forum User
New php-forum User
Posts: 13
Joined: Thu Dec 26, 2002 6:20 am

Postby Erick020 » Sat Dec 28, 2002 7:09 pm

Joan Garnet wrote:I don't undersand what you want...
Maybe something like this??

Code: Select all

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

User avatar
Oleg Butuzov
Last Samuray
Last Samuray
Posts: 831
Joined: Sun Jun 02, 2002 3:09 am

Postby Oleg Butuzov » Sun Dec 29, 2002 12:19 am

Erick, you are alredy have all that you need, very easy you can do that you want...

Erick020
New php-forum User
New php-forum User
Posts: 13
Joined: Thu Dec 26, 2002 6:20 am

Postby Erick020 » Sun Dec 29, 2002 1:04 am

Pejone wrote: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.

User avatar
Oleg Butuzov
Last Samuray
Last Samuray
Posts: 831
Joined: Sun Jun 02, 2002 3:09 am

Postby Oleg Butuzov » Sun Dec 29, 2002 1:35 am

same code

Code: Select all

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;

Erick020
New php-forum User
New php-forum User
Posts: 13
Joined: Thu Dec 26, 2002 6:20 am

Postby Erick020 » Sun Dec 29, 2002 3:23 am

Pejone wrote:same code

Code: Select all

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
New php-forum User
New php-forum User
Posts: 13
Joined: Thu Dec 26, 2002 6:20 am

Postby Erick020 » Sun Dec 29, 2002 8:54 am

Erick020 wrote:
Pejone wrote:same code

Code: Select all

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


Return to “PHP coding => General”

Who is online

Users browsing this forum: Google [Bot] and 1 guest