Jump to content

Help with SUM


Canman2005

Recommended Posts

Hi all

 

I have the following table

 

ID  UID  COST  VAT

1    11    2.99    17.5

2    11    1.99    17.5

3    11    0.50   

4    32    9.99    17.5

5    23    2.40

 

ID = Unique ID

UID = User ID

COST = Price (excluding VAT)

VAT = VAT Percentage %

 

What I want to do is grab all UID's with number 11, so a simple QUERY for this  would return;

 

ID  UID  COST  VAT

1    11    2.99    17.5

2    11    1.99    17.5

3    11    0.50   

 

But what I want to then do is add up all the costs, so that would equal

 

2.99 + 1.99 + 0.50 = 5.48

 

But I also want to include the VAT % if one exists, so with the above it would be

 

2.99 + VAT% = 3.51

 

1.99 + VAT% = 2.34

 

0.50 + VAT% = 0.50 (your notice 0.50 doesnt have a VAT value)

 

So it would take those values and add them together, giving

 

3.51 + 2.34 + 0.50 = 6.35

 

Can this be done or will it require tons of QUERIES and adding values?

Link to comment
https://forums.phpfreaks.com/topic/131490-help-with-sum/
Share on other sites

Do it in your PHP, not within the query. So when you loop through the rows, keep a variable, say $total and add to that with each loop. Something like:

 

while ($row = mysql_fetch_assoc($query)) {
    // print out your table and stuff ..
    $total = $total + ($row['cost'] + ($row['cost'] / $row['vat']));
}

echo 'TOTAL: ' . $total;

Link to comment
https://forums.phpfreaks.com/topic/131490-help-with-sum/#findComment-682892
Share on other sites

ahh okay, it might be trying to divide by a string - try converting it to a float (not 100% sure this would work):

 

if ($total == '' || $total == null || $total == 0) {
    $total = $total + $row['cost'];
} else {
    $vat = (float) $row['float'];
    $total = $total + ($row['cost'] + ($row['cost'] / $vat));
}

 

Oh also make sure there's no % on the front of it either!

Link to comment
https://forums.phpfreaks.com/topic/131490-help-with-sum/#findComment-682964
Share on other sites

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.