使用选择组合制作IF ELSE语句

时间:2016-11-08 10:03:12

标签: php

目前,我正在申请推荐一款适合用户使用的汽车。根据他们的选择偏好。我跳过了HTML表单,但选项如下:

类型:MPV,SUV,轿车
乘客:5,7 发动机:汽油,柴油
预算:低,中,高,豪华

我试图组合,因为我希望每一个选择都很重要。因此,这是我迄今为止所做的:

if (isset($_POST['submit'])
{
 $type = $_POST['type'];
 $passengers = $_POST['passengers'];
 $engine = $_POST['engine'];
 $budget = $_POST['budget'];

 if ($type == 'MPV' && $passengers == 5 && $engine == 'Gasoline' && $budget == 'Low')
 {
   // execute my function here
 } 
 else if ($type == 'MPV' && $passengers == 7 && $engine == 'Gasoline' && $budget == 'Low')
 {
   // execute my function here
 } 
 else if ($type == 'MPV' && $passengers == 5 && $engine == 'Diesel' && $budget == 'Low')
 {
   // execute my function here
 }  
 // and it goes on and on......

我知道我的问题是我必须继续制作if / elseif函数,直到我列出每一个可能的组合。现在还有更好的方法吗?我需要获得每一种可能的组合,因为我的自定义函数还需要进行其他计算。

这是我的自定义功能:

public function calculateDetails($displacement_bottom, $displacement_top, $price_bottom, $price_top, $clearance)
{
    $stmt = $connect->prepare("SELECT id, brand, name FROM cars
                                WHERE type = '$type'
                                AND displacement BETWEEN '$displacement_bottom' AND '$displacement_top'
                                AND price BETWEEN '$price_bottom' AND '$price_top'
                                AND clearance = '$clearance'
                                AND passengers = '$passengers'
                                AND engine = '$engine'
                                LIMIT BY 3");
    $stmt->execute();
    while ($row = $stmt->fetch())
    {
        $result = echo htmlspecialchars($row['brand'])." ".htmlspecialchars($row['name']);
    }
    return $result;
}

所以我可以这样做

if ($type == 'MPV' && $passengers == 5 && $engine == 'Gasoline' && $budget == 'Low')
{
    $suggestion = calculateDetails(1500, 2000, 100, 150, 'Medium');
    echo $suggestion;
} else if ($type == 'MPV' && $passengers = 7 && $engine == 'Gasoline' && $budget = 'Low')
{
    // continue on and on...
}

因为我找不到简化if / else"循环的方法" (我开始考虑放弃:(),我进入了一些新功能

public function typeDetails($type)
{
    if ($type == 'MPV')
    {
        $detailed_type = echo "1500 AND 2000"; 
    }
    else if ($type == 'SUV')
    {
        $detailed_type = echo "2000 AND 3500";
    }
    else
    {
        $detailed_type = echo "2000 AND 3000";
    }
    return $detailed_type;
}

public function budgetDetails($budget)
{
    if ($budget == 'Low')
    {
        $detailed_price = echo "100 AND 150";
    }
    else if ($type == 'Medium')
    {
        $detailed_price = echo "150 AND 250";
    }
    else if ($type == 'High')
    {
        $detailed_price = echo "250 AND 450";
    }
    else
    {
        $detailed_price = echo "450 AND 800";
    }
    return $detailed_price;
}

public function groundClearance($type)
{
    if ($type == 'MPV')
    {
        $ground_clearance = "Medium"; 
    }
    else if ($type == 'SUV')
    {
        $ground_clearance = "High";
    }
    else
    {
        $ground_clearance = "Low";
    }
    return $ground_clearance;
}

我希望能够做到这一点

$type = $_POST['type'];
$budget = $_POST['budget'];

$displacement = typeDetails($type);
$price = budgetDetails($budget);

// same function but with simplified where clause
$suggestion = calculateDetails($displacement, $price, $clearance);
echo $suggestion;

我今天太累了,但是多亏了你们,我想出了更多的想法。我感谢这里给出的所有回复和评论。我可能在PHP中不那么好,因此我想学习如何使事情更简单

2 个答案:

答案 0 :(得分:4)

你在这里做的不多。作为替代选项,您可以使用多级数组为模型创建一种矩阵:

$matrix = [
    'MPV' => [
        'Diesel' => [
            5 => [
                'Low' => function() { ... },
                'Medium' => function() { ... },
            ],
        ],
        'Gasoline' => [ ... ],
    ],
    'SUV' => [ ... ],
];

if (isset($matrix[$type][$engine][$passengers][$budget])) {
    $matrix[$type][$engine][$passengers][$budget]();
}

如果它们没有太大差别或者设置特殊常量而不是将函数传递给单个自定义方法,您也可以重用某些函数。这实际上取决于每个组合的实际功能。

答案 1 :(得分:0)

我会选择你的功能是选择一辆车,这样你就可以将你拥有的所有选项(前3辆不同的车)添加到你的数据库中,每个选项都有4个值:

  1. 类型
  2. 乘客
  3. 引擎
  4. 预算
  5. 并设置每辆车的默认值:

    ex for this condition:

    $type == 'MPV' && $passengers == 5 && $engine == 'Gasoline' && $budget == 'Low'
    
    1. Type = 1
    2. 乘客= 1
    3. 引擎= 1
    4. 预算= 1
    5. 对于选择不同选项的客户,您可以过滤查询并选择与搜索匹配的汽车!

      前:

      $query = ("SELECT * FROM car WHERE type = :type AND passengers = :passengers AND engine = :engine AND budget = :budget");