Convert excel formula to php

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

What happens with $rate2 if it is between 99 and 100?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

you may want to just use the last solution, it's far simpler.
Jim_Bo
Forum Contributor
Posts: 390
Joined: Sat Oct 02, 2004 3:04 pm

Post 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 :oops:


Cheers
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
Jim_Bo
Forum Contributor
Posts: 390
Joined: Sat Oct 02, 2004 3:04 pm

Post 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
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
Jim_Bo
Forum Contributor
Posts: 390
Joined: Sat Oct 02, 2004 3:04 pm

Post 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
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
Stewsburntmonkey
Forum Commoner
Posts: 44
Joined: Wed Aug 24, 2005 2:09 pm

Post 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;
:)
Jim_Bo
Forum Contributor
Posts: 390
Joined: Sat Oct 02, 2004 3:04 pm

Post by Jim_Bo »

Sorry this is so painfull :oops:

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
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

because you didn't reverse the array, like I said earlier it should have done 1.1 for both.
Jim_Bo
Forum Contributor
Posts: 390
Joined: Sat Oct 02, 2004 3:04 pm

Post by Jim_Bo »

I thought

$array = array_reverse($adjustor);

reversed the array?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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... :)
Post Reply