if语句选择switch语句

时间:2013-10-02 21:05:42

标签: php if-statement switch-statement

我希望这是有道理的。现在我使用以下代码添加复选框的数量检查并获得基于该数字的运费。这只能让我得到一个出货价格(地面)。我知道想要添加多个运输选项,所以我需要添加另一组switch / case语句。

//Add up club totals for shipping
            $clubTotal = array(
                            $driversChecked,
                            $hybridsChecked,
                            $woodsChecked,
                            $boxesChecked,
                            $wedgesChecked,
                            );  

            //see what shipping was picked
            $shippingChoice = $_POST['ShipMethod'];

            //Figure out ground shipping
            if (array_sum($clubTotal))
            {
                switch(array_sum($clubTotal))
                {
                    case 6:
                        $shippingCost =  15.99 ;
                        break ;

                    case 7:
                    case 8:
                        $shippingCost =  16.99 ;
                        break ;

                    case 9:
                    case 10:
                        $shippingCost =  17.99 ;
                        break ;

                    case 11:
                    case 12:
                        $shippingCost =  18.99 ;
                        break ;

                    case 13:
                        $shippingCost =  19.99 ;
                    break ;

                    case 14:
                        $shippingCost =  20.99 ;
                    break ;

                    case 15:
                        $shippingCost =  21.99 ;
                    break ;

                    case 16:
                        $shippingCost = 22.99;
                    break ;

                    default:
                        $shippingCost = 14.99;
                        break ;
                }
            }

如果$shippingChoice = $_POST['ShipMethod'];正在获取我的发货方法,我该如何使用它来选择要使用的switch / case语句。就像$shippingChoice返回“ground”的值一样,它将使用我当前的switch / case语句。如果它返回“2 Day”,它将使用我将做的另一个switch / case语句。

2 个答案:

答案 0 :(得分:1)

你想要这样的东西吗?为其他运输方式添加子阵列。

$shippingCostData = array(
    'ground' => array(
        6 => 15.99,
        7 => 16.99,
        8 => 16.99,
        9 => 17.99,
        10 => 17.99,
        11 => 18.99,
        12 => 18.99,
        13 => 19.99,
        14 => 20.99,
        15 => 21.99,
        16 => 22.99,
        'default' => 14.99,
    ),
);

$method = 'ground';
$clubTotalSum = array_sum($clubTotal);
$shippingCost = (isset($shippingCostData[$method][$clubTotalSum]) ? $shippingCostData[$method][$clubTotalSum] : $shippingCostData[$method]['default']);

您还可以使用此类内容来过滤送货方式,以确保其有效。

$method = (isset($shippingCostData[$_POST['shippingMethod']]) ? $_POST['shippingMethod'] : key($shippingCostData));

答案 1 :(得分:1)

您应该以另一种形式定义运费,这种形式在运行时更具延展性。例如:

$costs = [
    1 => 14.99, // 1 or more = 14.99
    6 => 15.99, // 6 or more
    7 => 16.99,
    9 => 17.99,
];

然后,您可以编写一个带有数字和成本数组的函数,并返回成本:

function calculateCost($items, $costs) {
    ksort($costs); // just to be on the safe side
    $result = reset($costs);
    foreach($costs as $number => $cost) {
        if ($number > $items) break;
        $result = $cost;
    }

    return $result;
}

完成此操作后,可以轻松保留多个成本数组并转发到您需要的数组,例如。

$costs = [
    'ground' => [
        1 => 14.99,
        6 => 15.99,
    ],
    'air' => [
        1 => 24.99,
        6 => 25.99,
    ],
];

$shippingChoice = $_POST['ShipMethod'];
$cost = calculateCost(array_sum($clubTotal), $costs[$shippingChoice]);

您可以轻松地将此扩展到任意数量的运输方式。

相关问题