Forum Moderators: coopster

Message Too Old, No Replies

php math? possible to add rows together?

         

jake66

6:14 pm on Apr 19, 2006 (gmt 0)

10+ Year Member



my database currently looks like this:
Date: apr 5 06
Vendor: ph
Tax: 2.00
Cost: 100.00

Date: mar 5 06
Vendor: ph
Tax: 3.00
Cost: 250.00

how can i add all values of cost and tax together, like so:
Tax Cost: 5.00
Total Cost: 350.00
Grand Total: $450.00

any tutorial sites i was able to find didn't have anything to do with php

inveni0

6:21 pm on Apr 19, 2006 (gmt 0)

10+ Year Member



Make sure they are type INT and use the SUM command.

jake66

6:40 pm on Apr 19, 2006 (gmt 0)

10+ Year Member



not sure how to call or use the SUM command?

coopster

6:42 pm on Apr 19, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Or just add them.
SELECT 
myDate,
myVendor,
myTax,
myCost,
myTax + myCost AS myTotal
FROM myTable
;

inveni0

6:42 pm on Apr 19, 2006 (gmt 0)

10+ Year Member



Use it in a query. Check PHP.net, I think they have more information.

jake66

7:19 pm on Apr 19, 2006 (gmt 0)

10+ Year Member



coopster, i tried:
$sql = "select tax, cost tax + cost as total from wholesale";
and
$sql = "select date, vendor, tax, cost, tax + cost as total from wholesale";
and
$sql = "select SUM(tax) from wholesale";

but no result (the table shows as empty) or just simply doesn't show anything.

for coopster's suggestion, once i got it to select properly i tried echo $total; but it didn't show anything either

jake66

7:53 pm on Apr 19, 2006 (gmt 0)

10+ Year Member



i have achieved desired effect by the following:
$sumtax += $row[tax];
$sumcost += $row[cost];

now, i'm wondering if it's possible to add $sumtax and $sumcost together for the grand total?

jake66

8:21 pm on Apr 19, 2006 (gmt 0)

10+ Year Member



desired effect achieved by the following:
$grandtotal = $sumtax + $sumcost;

thanks for your help everybody

coopster

9:29 pm on Apr 19, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Sorry about that jake66, you are correct. I was showing you a way to accummulate a total per line whereas you were looking for final totals. My mistake. Yes, accummulating them as you show here would be the easiest way to do that. I usually initialize the total values to zero first.
$totalTax  = 0; // initialize 
$totalCost = 0; // initialize
while (looping through the result set) {
$totalTax += $row['tax'];
$totalCost += $row['cost'];
}
$grandTotal = $totalTax + $totalCost;

jake66

11:00 pm on Apr 19, 2006 (gmt 0)

10+ Year Member



thank you coopster :)

i am having a problem: this is not allowing me decimals? i went into the database and changed tax and cost's "type" to decimal and it still shows as a whole number. is there anyway i can fix this?

coopster

11:15 pm on Apr 19, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



number_format [php.net]?

jake66

11:51 pm on Apr 19, 2006 (gmt 0)

10+ Year Member



the problem seems to be when it's going to the database, it's not being saved as a decimal.. the latter two digits and the period disappear

jake66

12:32 am on Apr 20, 2006 (gmt 0)

10+ Year Member



fixed my problem by changing the field types back to "text"

why would "decimal" prevent it being shown as a decimal?

coopster

2:15 am on Apr 20, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Which database are you using?

jake66

5:49 am on Apr 20, 2006 (gmt 0)

10+ Year Member



not sure what you're asking?
but it's mysql

coopster

12:31 pm on Apr 20, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



How did you define the column when you were defining it as DECIMAL? Did you give it a precision and scale? If you did not specify the scale it will default to 0. You can read more about MySQL DECIMAL numeric types here:

[dev.mysql.com...]