php - Calculate price form excel javascript
On a website the customer fills in a height and a width. I round up the number withecho round(657, -2, PHP_ROUND_HALF_UP);
for example.
So now i have my width & height in hundreds. Just like my price scheme:
*breedte = width, hoogte = height in millimeters. The result is a price.
The calculation would have been simple if there was a formula. Like Width * height = X. X * Y = the price. But we don't know Y. Client says Y is irregular and cannot be found. We think otherwise. For the math guys, if someone can find a formula you have my eternal thanks.
Now the real question. Let's say I would turn this into a massive function with php or fill it in a database to calculate the price. Would be a painful. But what is the best way to do it? Php function? Javascript function? Fill up a database and retrieve the price from there?
Answer
Solution:
If you can find a pattern in the data and are able to calculate it from the X and Y coordinates, you can make it a function. Preferably in the business logic of your application (that would likely be PHP, not Javascript).
Otherwise you'll be looking at a lookup table. It's best not to hard-code these, and for maintainability you're going to want to put these values either in a file or database separate from your code, so you can adjust them easily in the future.
I'd suggest either using a database or a CSV-like file, which can easily be edited by non-tech people using the right software.
Answer
Solution:
I don't know if this qualifies as an answer, but I would go for using a database for the lookup.
If a function had all those values in it, it would be very messy. Even if you could reduce the table to a mathematical function, the thing about prices is that they are likely to change quite often so it would be much easier to run an update on the database than have to modify the function each time and you could easily have special cases for certain height/width combinations if you needed them. You could also use a spreadsheet for the lookup, but I think a database would be better suited to an on-line environment.
The downside of using a database is that you'd have to flatten the data, but if it's already in some sort of table it shouldn't be too difficult.
If you do want to go down the function route, I could see that your data was linear in each row and column but had a non-linear component.
If you regress price on height, width and height*width you can get a correct prediction to the nearest whole number.