I am trying to create a simple script to add to my html website.
I need it to calculate the price based on the quantity the user inputs.
For example, a value of 1-1000 will be multiplied by 1.50 and displayed, 1001-5000 multiplied by 1.20 and displayed, 5001-10000 multiplied by 1 and displayed and any number above that would display an error message like "Must be below 10000".
I've been trying to do this in php with no success.
Instead of defining every condition in an IF statement, I would suggest defining constants for every stage:
You may find operator switch very useful (Switch in PHP). Something like this:
Edited: another option using loop:
Please notice, that after foreach last value element ($value is a common name for it, there it is $limits) still stores the last item from array. Take a look at brackets.
If you want it in pure php - that is not so good. To many reloads, but here how it works(it's a bad code, for bad idea):
In my opinion, you should try achieve it with JS(or easier - with jQuery). First of all, create a form:
Then do something like this:
Fiddle
You can use
if
to to check the vault and diplay the calculated value: