Reputation: 69
I'm currently busy developing a PHP price calculator for window decorations, but I'm up against a little challenge (at least for me it is).
Some curtains have a height of 140cm, so if a customer has a window of 200cm high, he will need two pieces of 140cm below each other.
For the calculator i would like to specify the following ranges (height):
1 piece of fabric: 1cm until and including 140 cm
2 pieces of fabric: 141cm until and including 280cm
3 pieces of fabric: 281cm until and including 420
So when the customer enters a height of 200cm, the calculator knows that he will need 2 pieces of fabric (and that the width of the windows needs to be multiplied by 2).
$aantalbanen = ($_POST["hoogte"]);
switch ($aantalbanen){
case ($aantalbanen>= 100 && $aantalbanen<= 140):
echo "within range 1";
break;
case ($aantalbanen>= 141 && $aantalbanen<= 280):
echo "within range 2";
break;
case ($aantalbanen>= 281 && $num<= 420):
echo "within range 3";
break;
default: //default
echo "within no range";
break;
}
Currently working with the CASE function, but that is getting a bit messy if I need to copy the calculation every time I add a range.
I hope things are clear (since English is not my first language :p) and someone can help me get started!
Thank you in advance!
Upvotes: 0
Views: 68
Reputation: 15609
Done this for you (Including form). I'm using HTML5's input type number
so only a number can go in and it also has a minimum
and maximum
number.
<form method="post" action=""> <!-- goes to same page -->
<input type="number" min="1" max="420" name="height" placeholder="Height">
<input type="submit" value="Submit">
</form>
<?php
if(isset($_POST['height'])){ //doesn't error on page if not set
$height = $_POST['height']; //create a variable
if ($height >= 1 && $height <= 140){ //if 1 to 140
$pieces = 1; //variable for pieces
}elseif($height>=141 && $height<=280){
$pieces = 2;
}elseif($height>=281 && $height<=420){
$pieces = 3;
}
else
{
$pieces = 0; //You won't need anything, it's not within the limits.
}
printf("You will need %d pieces!", $pieces);
}
Upvotes: 1
Reputation:
You need a form tag like this to enter the user data
<form method="post" action="submit.php">
<input type="text" name="height">
<input type="submit" value="Calculate">
</form>
You also need a php page (which can be the same page as above or a different page 'submit.php'
<?php
if ($_POST['height'] > 1 && $_POST['height'] <= 140) {
$pieces = 1;
} else if ($_POST['height'] > 140 && $_POST['height'] <= 280) {
$pieces = 1;
} else if ($_POST['height'] > 281 && $_POST['height'] <= 420) {
$pieces = 1;
}
echo "you need $pieces pieces";
?>
Upvotes: 1
Reputation: 5327
Do it like this:
if($height>1 && $height<=140) {
$pieces = 1;
} else if ($height>141 && $height<=280){
$pieces = 2;
} else if ($height>281 && $height<=420){
$pieces = 3;
}
Upvotes: 0