简体   繁体   English

PHP 按价格区间算手续费

[英]PHP Calculate fee by price range

Is anyone who can help me to create PHP and mysql Code.谁能帮我创建 PHP 和 mysql 代码。 Here is the condition.这是条件。

If price range is 1 to 20 USD, it will be show 2 USD.如果价格范围是 1 到 20 美元,它将显示 2 美元。 If price range is 21 to 50 USD, it will be show 5 USD.如果价格范围是 21 到 50 美元,它将显示 5 美元。 If price range is 51 to 100 USD, it will be show 7 USD.如果价格范围是 51 到 100 美元,它将显示 7 美元。

how to do it with PHP or WordPress php coding.如何使用 PHP 或 WordPress php 编码。

I don't get the MySQL part here did you want to do this on the database level?我在这里没有得到 MySQL 部分,您想在数据库级别执行此操作吗? or are you looking for both?或者你正在寻找两者? anyway, in PHP it should be like this反正PHP里面应该是这样的

$price = 5;

if($price >= 1 && $price <= 20){
  $price = 2;
}else if($price > 20 && $price <= 50){
  $price = 5;
}else if($price > 50 && $price <= 100){
  $price = 7;
}

Note: you should be careful with the ranges 20.1 is greater than 20 but it is less than 21注意:您应该注意范围 20.1 大于 20 但小于 21

I hope it's helpful希望对您有所帮助

If you're using PHP 8, this would be elegantly done with match :如果您使用的是 PHP 8,则可以使用match优雅地完成此操作:

$fee = match(true) {
    $price <= 20 => 2,
    $price <= 50 => 5,
    $price <= 100 => 7,
    default => 0 // free shipping!
};

Since the evaluation ends when a "match arm" matches, we don't need to define the lower range -- as long as our ranges are defined in ascending order.由于评估在“匹配臂”匹配时结束,因此我们不需要定义较低的范围——只要我们的范围按升序定义即可。 I haven't used $price >= 1 && $price <= 20 => 2 for the first condition, assuming that $0.50 still costs something.我没有使用$price >= 1 && $price <= 20 => 2作为第一个条件,假设 $0.50 仍然需要花费一些东西。

Since we're evaluating for the truth of expressions ( true|false ), a simple match($fee) wouldn't work.由于我们正在评估表达式的真实性 ( true|false ),简单的match($fee)是行不通的。 We need match(true) instead, where, when an expression evaluates as true, the corresponding value will be returned.我们需要match(true)来代替,当表达式的计算结果为真时,将返回相应的值。 Note that match must be exhaustive, ie.请注意, match必须是详尽无遗的,即。 it must have a default condition if none of the conditions match (or you get an error).如果所有条件都不匹配(或者出现错误),则它必须具有默认条件。

On older PHP versions, the more long-winded switch can be used:在较旧的 PHP 版本上,可以使用更冗长的switch


switch(true) {
    case ($price >= 1 && $price <= 20):
        $fee = 2;
        break;
    case $price <= 50:
        $fee = 5;
        break;
    case $price <= 100:
        $fee = 7;
        break;
    default: 
        $fee = 0;
        break;
};

While switch doesn't require a default statement, it's a good idea to have a default value for transactions that exceed your ranges.虽然switch不需要 default 语句,但最好为超出范围的事务设置默认值。 Being nice, I give free shipping for orders > 100 .很好,我为订单> 100免费送货。

You can also of course use a series of if/elseif statements, but they become quite clunky if you have more than a small handful of conditions to check.当然,您也可以使用一系列if/elseif语句,但如果您要检查的条件不止一小部分,它们就会变得相当笨拙。 match and switch exist to make cases like this more readable and easier to maintain. matchswitch的存在是为了使这样的案例更具可读性和更易于维护。

Per @AhmedHassan's notes, your logic has a gap between 20/21, and 50/51.根据 @AhmedHassan 的注释,您的逻辑在 20/21 和 50/51 之间存在差距。 The next step from "less than or equal to 20" has to be "more than 20, less than or equal with 50" .“小于或等于 20”开始的下一步必须是“大于 20,小于或等于 50” Or, if $20 flat already incurs the higher fee, then you should use >= 20 and < 50 for your price range boundaries.或者,如果 20 美元的固定费用已经产生了更高的费用,那么您应该使用>= 20< 50作为您的价格范围边界。

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

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