Forum Moderators: coopster

Message Too Old, No Replies

php math is off, why?

         

jake66

3:17 am on Feb 10, 2007 (gmt 0)

10+ Year Member



need to: find 1% of the total

example:
500.00 should be 5.00

in php, it's coming off as 7.07

code:
$query = "SELECT employee, SUM(balance) FROM commission where `date` < DATE_SUB(CURDATE(), INTERVAL 30 DAY) GROUP BY employee";

$total_sold = $row['SUM(balance)'];
$percentage = ($total_sold / '99');
echo number_format($percentage,2);

what am i doing wrong?

eelixduppy

3:25 am on Feb 10, 2007 (gmt 0)



It seems that you are not calculating the value correctly. To get 1% of a number, you must multiply that number by 0.01. So, for your example, it would be something like the following:

$percentage = ($total_sold*.01);

Hope this helps! :)

jake66

3:29 am on Feb 10, 2007 (gmt 0)

10+ Year Member



ah thank you for the obvious note (but math was/is my absolute worst subject!)

however when i replaced my code, the 2.02 (200) went to 2.00 (as expected)

but the 500.00 stayed the same.
am i still missing an important bit?

eelixduppy

3:35 am on Feb 10, 2007 (gmt 0)



If the number is truly 500, multiplying by .01 would give you 5. I have a feeling that its not 500 that you are dealing with, but I'm not entirely sure. There is definitely something wrong here. Trying echoing all of he values before you do the manipulation, to see what is taking place.

jake66

3:45 am on Feb 10, 2007 (gmt 0)

10+ Year Member



think you may have found it!

employee1 (last month)200 / 2.00
employee1 (this month)700 / 7.00
employee2 (this month)52.5 / 0.53

in the database:
2007-01-09 09:02:46 employee1 = 200.00
2007-02-09 09:02:17 employee1 = 500.00

2007-02-09 09:02:17 employee2 = 52.50

so it was not the math causing my troubles, it was how i was pulling the dates.

what i want to do is display their balances separated by month.

so for employee1 it should be:
employee1 (last month)200 / 2.00
employee1 (this month)700 / 7.00
essentially what i want to do, is showcase their commission balances for this month and last month.

my queries (i'm no php whiz obviously, so i have these running in 2 different queries.):

//this month

$query = "SELECT employee, SUM(balance) FROM commission where `date` < DATE_SUB(CURDATE(), INTERVAL 30 DAY) GROUP BY date";

//last month

$query = "SELECT employee, SUM(balance) FROM commission where `date` > DATE_SUB(CURDATE(), INTERVAL 60 DAY) GROUP BY employee"; 

thank you for your help, i've been pulling my hair out for about an hour trying to figure out how such simplistic math was fudging up :)

jake66

3:58 am on Feb 10, 2007 (gmt 0)

10+ Year Member



my bad, i had my queries mixed up.

is it logical to have 2 queries like i do now or is there a way to condense them into 1?

fixed queries (does what i want, but is there a way to limit it by MONTH, rather than 30 days?)

//this month
$query = "SELECT employee, SUM(balance) FROM commission where `date` < DATE_SUB(CURDATE(), INTERVAL 30 DAY) GROUP BY date";

//last month
$query = "SELECT employee, SUM(balance) FROM commission where `date` > DATE_SUB(CURDATE(), INTERVAL 30 DAY) GROUP BY employee";

eelixduppy

4:19 pm on Feb 11, 2007 (gmt 0)



For more help, you can go to MySQLs Date and Time Functions [dev.mysql.com] as they may provide some insight as to what you want to achieve. Instead of using
30 DAY
maybe something like
1 MONTH
would be better.

Good luck! :)