Page 2 of 2
Posted: Sat Aug 27, 2005 5:01 am
by timvw
What happens with $rate2 if it is between 99 and 100?
Posted: Sat Aug 27, 2005 8:22 am
by feyd
you may want to just use the last solution, it's far simpler.
Posted: Sat Aug 27, 2005 3:03 pm
by Jim_Bo
Not sure about it been between 99 - 100 ... it has =99 or =100 .. I guess as long as its a full number it will be fine under that senario. For that piece of code it would need to be
if ($rate2 >= 1 and $rate2 < 100)
if ($rate2 >= 100 and $rate2 < 200)
?
The last solution I havnt managed to get my head around to make the enitre formula to work so it includes all the if's
Cheers
Posted: Sat Aug 27, 2005 3:17 pm
by feyd
it's quite simple
Code: Select all
$adjustor = array(1=>1, 100=>1.1, 200=>1.2, 300=>1.3);
foreach($adjustor as $limit => $mul)
{
if($value < $limit)
{
$value *= $mul;
break;
}
}
at this point, it'll compare $value against 1, 100, 200, and 300. The first one it falls under is used and the loop exits. You simply add more elements to the $adjustor array. The key is the number to compare $value against and the value is the multiplier.
Posted: Sat Aug 27, 2005 4:05 pm
by Jim_Bo
using:
$adjustor = array(1=>1, 100=>1.1, 200=>1.2, 300=>1.3, 500=>1.4);
If I set $value to 500 it returns 500, I read above as being .. if value is = or greater than 500 add 40%
So should return value 700?
Also I dont really get where $mul comes from?
Thanks
Posted: Sat Aug 27, 2005 4:15 pm
by feyd
the operators in the array setting are not math. key=>value is how it works. It returned 500 because you didn't specify a limit greater than 500 for it to fall under.
$mul is the value component of the key=>value pair.
Posted: Sat Aug 27, 2005 4:41 pm
by Jim_Bo
So by switching it to:
$adjustor = array(1=<1, 100=<1.1, 200=<1.2, 300=<1.3, 500=<1.4);
to create an affect where if its 100 or greater add 10% if 200 or greater add 20% there for add 40% to anything 500 of greater?
Thanks
Posted: Sat Aug 27, 2005 4:50 pm
by feyd
no. => is NOT math. It's how arrays are created.
If you want to perform greater-equal ops you will have to reverse the array so the highest values are compared first. You'll also need to change the compare < to >= inside the foreach.
Posted: Sat Aug 27, 2005 5:00 pm
by Stewsburntmonkey
Code: Select all
$adjustor = array(1=>1, 100=>1.1, 200=>1.2, 300=>1.3, 500=>1.4);
Just to reiterate, the above creates an array named adjustor and is equivalent to:
Code: Select all
$adjustor[1] = 1;
$adjustor[100] = 1.1;
$adjustor[200] = 1.2;
$adjustor[300] = 1.3;
$adjustor[500] = 1.4;

Posted: Sat Aug 27, 2005 5:29 pm
by Jim_Bo
Sorry this is so painfull
Something like:
Code: Select all
$adjustor = array(1=>1, 100=>1.1, 200=>1.2, 300=>1.3);
$array = array_reverse($adjustor);
foreach($array as $limit => $mul)
{
if($rate2 >= $limit)
{
$rate2 *= $mul;
break;
}
}
That added 30% to 500 but also added 30% to 100 which should have only been 10%?
Thanks
Posted: Sat Aug 27, 2005 7:08 pm
by feyd
because you didn't reverse the array, like I said earlier it should have done 1.1 for both.
Posted: Sat Aug 27, 2005 9:00 pm
by Jim_Bo
I thought
$array = array_reverse($adjustor);
reversed the array?
Posted: Sat Aug 27, 2005 9:12 pm
by feyd
Code: Select all
[feyd@home]>php -r "var_export(array_reverse(array(1=>1, 100=>1.1, 200=>1.2, 300=>1.3)));"
array (
0 => 1.3,
1 => 1.2,
2 => 1.1,
3 => 1,
)
notice how the values reversed, but the indexes were redone...
