I am on a few list serves and occasionally I am able to get a few of the questions answered. I will post some of them on my blog occassionally in case you run into the same issues. In this case, one of my fellow programmers ran into an issue where his subtotals where not adding up properly. I had run into the same issue myself. Just read below. It is simply a matter or recognizing that your variable might be formatted to display with a comma, such as 1,432.12
----- Original Message -----
From: Matt
To: List
Cc: Mauricio
Sent: Sunday, March 30, 2008 11:48 PM
Subject: Re: it it a php bug? or am i just slow?
That was it!!
apparently the ',' was settype()ing it on the fly as a string, so when it came to the += it was evaluating wrong..
Many thanks Mauricio!
Mauricio Zuniga - Systems Engineer wrote:
also,
even though you are giving us the values,
could it be possible that you have your number formatted with a comma?
such as 1,321.43.
I once ran into an issue where my subtotals were not correct because of this...
----- Original Message -----
To:
Cc:
Sent: Sunday, March 30, 2008 10:29 PM
Subject: Re: it it a php bug? or am i just slow?
I set up the following loop:
$item_price = 0;
$items = array(50, 100, 1357);
foreach ($items as $v) {
$item_price += $v;
echo "
$item_price";
}
and it gave the following output:
50
150
1507
which is what was expected.
Could you post the actual loop code you are using? Perhaps there is something there we can help with?
Daniel
Mattwrote: Not sure if this is a bug in PHP, or if I'm just especially slow...
I've got a script that's calculating the total order for items in a cart. To do this it's doing a simple += on the variable $subtotal as it prints each row on the cart page, then it prints $subtotal in the appropriate spot at the end. So on its way through the loop that prints each row, right at the end of the loop it does:
$subtotal += $item_price;
Simple, straight forward, not a problem.
It works perfectly fine. IF the value of $item_price <= 999.99 (?!??!!!?!?!?!?)
Anything >= 1000.00 and it adds 1 to subtotal, not the $itemprice. (?!?!?!?)
I've echoed $item_price and $subtotal while the loop is printing, I get results as below:
// first iteration
echo $item_price; // 50.00
echo $subtotal; // 50.00
// second iteration
echo $item_price; // 100.00
echo $subtotal; // 150.00
// so far, so good, right?
// third iteration
echo $item_price; // 1357.00
echo $subtotal; // 151.00
/// what??
// fourth iteration
echo $item_price; // 100.00
echo $subtotal; // 251.00
Any ideas? The only option I've considered so far is chucking every computer in the house out the damn window and becoming a carpenter, but I'm still on the fence about that.
-M.E.
--
_________________________________________
--
_________________________________________
Labels: bug, error, format, formatting, item, loop, number, php, price, pricing, string, subtotal, variable