Reputation: 7065
Hope y'all are having a wonderful day full of rainbows and roses!
Ok, ok .. I'll get to the point:
I've got a function that accepts multiple parameters and calculates the price of a product based on those parameters. Here's a conceptual example:
public function getPrice( $params ) {
// black magic goes here
return $price;
}
The parameters include:
$params[width]
which ranges all the way from 10 - 72 inches$params[height]
which ranges from 10 - 44 inches(There are actually more params, but for the sake of simplicity, I've kept those out).
Now, I have a table in Excel (something like a truth table) with rows that represent the width
and columns that represent the height
. The value in the corresponding cell is the price
.
How could I best implement this pricing strategy in PHP? I thought nested if
statements would work but got tired after the 10th if
. Help?
Upvotes: 0
Views: 189
Reputation: 3496
You could store the information from Excel in a 2D array.
I.e.
$prices = array( // Columns --
array(1,2,3), // Rows
array(4,5,6), // |
array(7,8,9) // |
);
Then you can look up your price based on width/height by doing:
return $prices[row][column];
In your case, row would be width and column would be height.
Some extra work would be required as you have a range starting at 10, so you'd need to subtract 10 from the value you enter.
I.e:
return $prices[width-10][height-10];
.
Upvotes: 3
Reputation: 3015
Why wouldn't you just put all that data in your database? Have a row with all values of every parameter, and the accoriding price ? It's just 1 query away ...
Upvotes: 1