简体   繁体   English

在If语句中为不同条件的结果计数值

[英]count value in If statement for different condition's result

Each customers who buy 5 products, they will get bonus of 2 products. 每个购买5个产品的客户将获得2个产品的奖励。

However, each customer who buy 10 products, they will get bonus of 6 products. 但是,每位购买10个产品的客户将获得6个产品的赠金。

The problem now is what if there is customer who buy 26 products? 现在的问题是,如果有客户购买26种产品怎么办?

So the condition will be : 因此条件将是:

the customer buy 客户购买

 (10 x 2) //get 6 products x 2 = 12 products bonus/free 

   + 

 (5 x 1) // get 2 products x 1 = 2 products bonus/free

So far here is my condition: 到目前为止,这是我的状况:

if ($quatity > 4 && $quatity < 10){
    echo "Congrats, you get 2 products free"
}

if ($quatity > 9){
    echo "Congrats, you get 6 products free"
}

The code above is working fine with me if only the customer buy more than 5 or 10 products. 如果仅客户购买超过5或10种产品,则上面的代码对我来说效果很好。

But as I mentioned above, what if the customer buy more than 10 products such as 26 products or 200 products? 但是,正如我上面提到的,如果客户购买了10种以上的产品,例如26种产品或200种产品,该怎么办? Should I write if function manually such as if ($quatity > 9){} , if ($quatity > 19){} , if ($quatity > 29){} , if ($quatity > 39){} , and so on, 我是否应该手动编写if函数,例如if ($quatity > 9){}if ($quatity > 19){}if ($quatity > 29){}if ($quatity > 39){}等上,

OR 要么

can anybody figure out the simple way for this? 有人能找到简单的方法吗?

Thanks in advance! 提前致谢!

This code will give you what you want. 此代码将为您提供所需的内容。

It will break down the 10 products bonus, and then see if there are any 5 bonus left to give. 它将分解10个产品奖金,然后查看是否还有5个奖金要给。

$tenBonus = (int)($quatity / 10);
$fiveBonus = $quatity % 10 > 4 ? 1 : 0;
$bonus = ($tenBonus * 5) + ($fiveBonus * 2);
if($bonus > 0) {
    echo "Congrats, you get ". $bonus ." products free";
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM