问题
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.
回答1:
You can use if
to to check the vault and diplay the calculated value:
if($input <= 1000)
{
echo $input * 1.5;
}
elseif($input <= 5000)
{
echo $input * 1.2;
}
elseif($input <= 10000)
{
echo $input;
}
else
{
echo "Must be below 10000";
}
回答2:
You may find operator switch very useful (Switch in PHP). Something like this:
...
switch ($quantity) {
case (($quantity >= 1) && ($quantity <= 1000)):
$multiplicator = 1.5;
echo $quantity * $multiplicator;
break;
case (($quantity >= 1001) && ($quantity <= 5000)):
$multiplicator = 1.2;
echo $quantity * $multiplicator;
break;
case (($quantity >= 5001) && ($quantity <= 10000)):
$multiplicator = 1.2;
echo $quantity * $multiplicator;
break;
case ($quantity > 10000):
echo 'Quantity must be less then 10000!';
break;
}
....
Edited: another option using loop:
...
$limits_array = array(
0 => array(
'min' => 1,
'max' => 1000,
'mul' => 1.5,
),
1 => array(
'min' => 1001,
'max' => 5000,
'mul' => 1.2,
),
2 => array(
'min' => 5001,
'max' => 10000,
'mul' => 1,
),
);
foreach ($limits_array as $limits)
if (($quantity >= $limits['min']) && ($quantity <= $limits['max']) {
echo $quantity * $limits['mul'];
break;
}
if ($quantity > $limits['max'])
echo 'Quantity must be less then 10000!';
...
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.
回答3:
Instead of defining every condition in an IF statement, I would suggest defining constants for every stage:
<form action="" method="post">
<input type="text" name="qnt" />
<input type="submit" />
</form>
<?php
define('MIN_STAGE', 1);
define('MAX_STAGE', 3);
define('STAGE_1_MIN', 0);
define('STAGE_1_MAX', 1000);
define('STAGE_1_MULTIPLIER', 1.50);
define('STAGE_2_MIN', STAGE_1_MAX);
define('STAGE_2_MAX', 5000);
define('STAGE_2_MULTIPLIER', 1.20);
define('STAGE_3_MIN', STAGE_2_MAX);
define('STAGE_3_MAX', 10000);
define('STAGE_3_MULTIPLIER', 1);
for($i = MIN_STAGE; $i <= MAX_STAGE; $i++) {
if ($_POST['qnt'] > constant("STAGE_".$i."_MIN") && $_POST['qnt'] <= constant("STAGE_".$i."_MAX")) {
echo $_POST['qnt'] * constant("STAGE_".$i."_MULTIPLIER");
}
elseif ($_POST['qnt'] < constant("STAGE_".MIN_STAGE."_MIN") || $_POST['qnt'] > constant("STAGE_".MAX_STAGE."_MAX")) {
die ('Must be between ' . constant("STAGE_".MIN_STAGE."_MIN") . ' AND ' . constant("STAGE_".MAX_STAGE."_MAX"));
}
}
回答4:
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):
<?php
$price = 10;
if(isset($_REQUEST['count']){
$count = (int)$count;
if($count > 10000){
echo 'Must be below 10000';
}
elseif($count >= 1 && $count <= 1000){
$price *= 1.2;
}
//Etc...
$total = $price * $count;
}
In my opinion, you should try achieve it with JS(or easier - with jQuery). First of all, create a form:
<input type="text" id="count" />
<input type="button" id="calculate" value="Calculate" />
Then do something like this:
$(function(){
var price = 10;
var multiplier = 1;
$('#calculate').click(function(){
var count = parseInt($('#count').val());
if(count > 10000){
alert('Must be below 10000');
return;
}
else if(count >= 1 && count <= 1000){
multiplier = 1.2;
}
alert('Current price is ' + price + ' * ' + multiplier + ' = ' + getActualPrice());
});
function getActualPrice(){
return price * multiplier;
}
});
Fiddle
来源:https://stackoverflow.com/questions/19752094/price-calculator-based-on-quantity