How to discount price from 1 to 99 %

Hello,
I have 2 input fields price and discount

if(isset($_POST[‘calculator’])){

$price = $_POST['price'];

$discount  = $_POST['discount'];

}

if($price <= 22) {

$price = 128.13;

}

.$result = $sum=$price+$discount;

How to discount price from 1 to 100 % depends on which discount I want to enter e.g. 25 or 12 or 35…Tnx

I would look into using a switch statement -> https://www.php.net/manual/en/control-structures.switch.php

What is the logic behind the price/discount thing? if price <= 22 then price = 128.13 doesn’t really make sense without any more info

Ok here is complete code …
form action=" " method=“post”>

I don’t know why he doesn’t show input feilds code here
here is two input fields Price and Discount

if(isset($_POST[‘calculator’])){

$price = $_POST[‘price’];

$discount = $_POST[‘discount’];
}

if($price <= 22) {

$price = 128.13;
}

echo "<font size=5><center>Price: ".$price."</center>"; 
echo "<font size=5><center>Discount: ".$discount."</center>"; 
echo "<font size=5><center>Result: ".$result = $sum=$price+$discount;"</center>";

}
?>

As u can see i have two input fields if, i enter in first field numbers between 1 and 22 result is 128.13,now i need discount on that result if i enter 1 in second field, result need to be 128.13 - 1.28,
i found solution for that and works fine with this code

if($discount == 1 && $price < 23) {
$discount = -1.28;
} elseif ($discount == 2 && $price < 23) {
$discount = -2.56;
} elseif ($discount == 3 && $price < 23) {
$discount = -3.84;
} else {
$discount = 0;
}

but since I have a huge price list it is quite hard to type 99 if statemant for every price,basicly for every 1% i need to code elseif statement…i hope there is another way to do this or shorter way…Tnx for help.

What happens if the price is 24? 100? 500?

Which is why I asked what is the logic behind how it’s calculated. From the discount code you wrote here and the “for every 1% I need to code elseif statement” it seems like this would be enough. But without more info it’s impossible to answer properly

$price = $_POST[‘price’] > 0 ? $_POST[‘price’] : 0;
$discount = $_POST[‘discount’] > 0 && $_POST[‘discount’] < 100 ? -floor($_POST[‘discount’]) * 1.28 : 0;

// ??
if ($price < 23) {
  $price = 128.13;
}

This way
discount=1 = -1*1.28 = -1.28
discount=2 = -2*1.28 = -2.56
discount=3 = -3*1.28 = -3.84
discount=4 = -4*1.28 = -5.12
etc

What happens if the price is 24? 100? 500?


<23 = 128.13
>23 && <= 33 = 153.07
>33 && <= 44 = 178.20 
>44 && <= 55 = 203.33 
>55 && <= 66 = 228.27 
>66 && <= 84 = 261.78 
>84 && <= 110 = 311.85
>110  = 370.30

this is price list, i have 5 more this lists with another prices but other categories,same values but other price… first cat is 128.13 second is 153.75 third is 179.38 and so on…

This not spelled correctly “for every 1% I need to code elseif statement” , correctly is i dont want to code for every 1 % , hope there is some calculation formula to do this automatically.tnx

Thats it, works perfect…

$price = $_POST['price'] > 0 ? $_POST['price'] : 0;
$discount = $_POST['discount'] > 0 && $_POST['discount'] < 100 ? -floor($_POST['discount']) * 1.28 : 0;
    
if($price > 0 && $price < 23)  { 
$dicount = -1*1.28;
$dicount = -2*1.28;
$dicount = -3*1.28;
$dicount = -4*1.28;  
    
}

the only thing I can do is thank you for helping me.

Ok. The if statement in the last code snippet you posted makes no sense though, so I hope you don’t have that in your actual code.

U mean on this code
if($price > 0 && $price < 23) {
$dicount = -11.28;
$dicount = -2
1.28;
$dicount = -31.28;
$dicount = -4
1.28;
}

it have sense, next calculation is 153.07 its not 1.28 anymore its 1.53 on 1%
if($price > 23 && $price <= 33 {
$discount = -1*1.53
}

You keep changing the code between each post o.O

But this code

if($price > 0 && $price < 23) {
$dicount = -11.28;
$dicount = -21.28;
$dicount = -31.28;
$dicount = -41.28;
}

Will give the exact same result as this code, as you just keep overwriting the value of the same variable

if($price > 0 && $price < 23) {
$dicount = -41.28;
}

JimL can u help me again with something …

if(($year < 2012) && ($ccm < 1151)) {

$year = 15.00;

} elseif (($year < 2012) && ($ccm >= 1150 || $ccm <= 1300)) {

$year = 20.00;

} elseif (($year < 2012) && ($ccm >= 1301 || $ccm <= 1600)) {

$year = 30.00;
}

The problem is only work first line of code, if i enter year 2011 or lower and ccm 1100 resutl is 15.00 thats ok ,but if i enter ccm over 1150 result is 15.00 again its not 20.00…Tnx

with year=2011 and ccm=1300 I get year=20

You do however have an issue in the other two conditions

$ccm >= 1150 || $ccm <= 1300

This condition is true
if ccm >= 1150
or
if ccm <= 1300

So its true if ccm is larger than or equal to 1150 or if ccm is less than or equal to 1300. In other words it’s always true.

This is too complicated for an if or a switch statement. Without seeing your form it’s difficult to be sure, but it looks like you expect a user to choose a year and an area then provide a discount. I’d implement this as a lookup table. Something like:

// If you post your actual form, we can show you how to implement getArea() and getYear()
$area = getArea(); // $area will now be something like 1200 - a value from your top row
$year = getYear(); // $year will now be something like 2017 - a value from your left column

/**
 * This is a truncated example - you'd put the rest of your table rows in here in a similar fashion
 */
$discounts = array(
    // This key is from your left column
    2018 => array(
        // Add your row values from highest to lowest

        // This key is the lowest number from your top row.
        // The value is the cell form this row and column: the cell from row 2018 and column 3000 is 1000
        3000 => 1000,
        2500 => 650,
        2000 => 200,
        1600 => 75,
        1300 => 50,
        1150 => 40,
        // Use 0 as the key for your leftmost column
        0 => 30
    )
    // Add the rest of your rows here...
);

// Get the row you want to look at
$year_discounts = $discounts[$year];

foreach ($year_discounts as $minimum_area => $area_discount) {
    /**
     * Check your user's area against the minimum area for each discount.
     * Once the user's area is greater than a discount's minimum area, apply that discount.
     */
    if ($area >= $minimum_area) {
        $discount = $area_discount;
        break;
    }
}

$discount should now be the discount you want to apply.

Thank u for help, im finish with discount everythings work fine,this is another problem,as u can see in table is year of the car and engine ccm, year is left side of table,upper is cm3,in the middle of table is tax,if i enter for example 2008 in year field and 1000 in ccm field result need to be 15.00…if year is 2008 and ccm between 1151 and 1300 result need to be 20.00 etc…

Ok. In future you’d be best to create new questions as new topics. This makes it easier for people to keep track. The lookup approach I gave above should work for what you describe.

i found solution if $year < 2012 && $ccm <=1150 {
$tax = 15;
}

Sponsor our Newsletter | Privacy Policy | Terms of Service