用于多个条目的PHP Sql

用于多个条目的PHP Sql,php,mysql,Php,Mysql,我有一个带有两个选择选项的HTML <select name="stransmission" id="stransmission" style="width: 200px" > <option>All</option> <option>Manual</option> <option>Transmission</option> </select> <select name="s

我有一个带有两个选择选项的HTML

 <select  name="stransmission" id="stransmission" style="width: 200px" >
 <option>All</option>

 <option>Manual</option>
 <option>Transmission</option>
  </select>

<select  name="sfuel" id="sfuel" style="width: 200px" >
<option>All</option>

<option>Petrol</option>
<option>Diesel</option>
<option>Electric</option>
</select>
这是可行的,但我会有很多Sql语句,因为可能有多种组合,我打算添加更多的选择框,所以if条件会很多

有更简单的方法吗


感谢您的帮助

您可以使用阵列来完成。像这样的

<?php

$selects = array (
    "stransmission" => array ( // field name for the select element in html
        "dbField" => "KindOfCar", // field for the SQL query
        "defaultItem" => "1", // index of the default value 
        "selectedItem" => false, // it's updated when user submits the form
        "items"=> array(
            "1" => array ( // the index must be unique in the array
                "text"=> "All", // text to display in the select box option
                "condition"=>false // condition for the SQL query (set false, when you want to skip this condition in the query)
            ),
            "2" => array (
                "text"=> "Manual",
                "condition"=>"'manual'"
            ),
            "3" => array (
                "text"=> "Transmission",
                "condition"=>"'transmission'"
            )
        )
    ),
    "sfuel" => array (
        "dbField" => "Fuel",
        "defaultItem" => "1", 
        "selectedItem" => false,
        "items"=> array(
            "1" => array (
                "text"=> "All",
                "condition"=>false
            ),
            "2" => array (
                "text"=> "Petrol",
                "condition"=>"'petrol'"
            ),
            "3" => array (
                "text"=> "Diesel",
                "condition"=>"'diesel'"
            ),
            "4" => array (
                "text"=> "Electric",
                "condition"=>"'electric'"
            )
        )
    ),
    "newField" => array (
        "dbField" => "NewField",
        "defaultItem" => "1",
        "selectedItem" => false,
        "items"=> array(
            "1" => array (
                "text"=> "All",
                "condition"=>false
            ),
            "2" => array (
                "text"=> "Value #1",
                "condition"=>"'value-1'"
            ),
            "3" => array (
                "text"=> "Value #2",
                "condition"=>"'value-2'"
            ),
            "4" => array (
                "text"=> "Value #3",
                "condition"=>"'value-3'"
            )
        )
    )
);

function processPostValues(&$selects, $postArr){
    // receives and process the values submitted by the user
    foreach($selects as $fieldName=>$selectObj){
        // check if the user sent the selected value thru post request, and check if exists the selected value in the items array.
        if (isset($postArr[$fieldName]) && isset($selectObj["items"][$postArr[$fieldName]])){
            $selects[$fieldName]["selectedItem"]=$postArr[$fieldName];
        }
    }
}

function drawSelectBoxes($selects){
    $str = "";
    foreach($selects as $fieldName=>$selectObj){
        $str .= "<select name='{$fieldName}' id='{$fieldName}' style='width: 200px' >";
        $selectedItem = ($selectObj["selectedItem"] != false)?$selectObj["selectedItem"]:$selectObj["defaultItem"];
        foreach ($selectObj["items"] as $key=>$item){
            $str .= "<option ";
            if ($key==$selectedItem){
                $str .= "selected='selected'"; 
            }
            $str .= " value='{$key}'>{$item['text']}</option>";
        }
        $str .= "</select><br>";
    }
    return $str;
}

function createQuery($selects){
    $query = "SELECT * from tblCar";
    $queryWhere = array();
    foreach($selects as $fieldName=>$selectObj){
        $selectedItem = ($selectObj["selectedItem"] != false)?$selectObj["selectedItem"]:$selectObj["defaultItem"];
        $condition = $selectObj["items"][$selectedItem]["condition"];
        $dbField = $selectObj["dbField"];
        if ($condition!=false){
            $queryWhere[]= "{$dbField} LIKE {$condition}";
        }
    }
    if (count($queryWhere)>0){
        $query .= " WHERE ".implode(" AND ",$queryWhere);
    }
    return $query;
}
processPostValues($selects, $_POST);
?>
<form method="POST">
    <?php
        echo drawSelectBoxes($selects);
    ?>
    <br>
    <button>Submit</button>
    <br>
    <strong>Query:</strong> 
    <?php
        echo createQuery($selects);
    ?>
</form>

您是否尝试过
语句中的燃油位置?请向我们展示您的尝试,我们会纠正它。
<?php

$selects = array (
    "stransmission" => array ( // field name for the select element in html
        "dbField" => "KindOfCar", // field for the SQL query
        "defaultItem" => "1", // index of the default value 
        "selectedItem" => false, // it's updated when user submits the form
        "items"=> array(
            "1" => array ( // the index must be unique in the array
                "text"=> "All", // text to display in the select box option
                "condition"=>false // condition for the SQL query (set false, when you want to skip this condition in the query)
            ),
            "2" => array (
                "text"=> "Manual",
                "condition"=>"'manual'"
            ),
            "3" => array (
                "text"=> "Transmission",
                "condition"=>"'transmission'"
            )
        )
    ),
    "sfuel" => array (
        "dbField" => "Fuel",
        "defaultItem" => "1", 
        "selectedItem" => false,
        "items"=> array(
            "1" => array (
                "text"=> "All",
                "condition"=>false
            ),
            "2" => array (
                "text"=> "Petrol",
                "condition"=>"'petrol'"
            ),
            "3" => array (
                "text"=> "Diesel",
                "condition"=>"'diesel'"
            ),
            "4" => array (
                "text"=> "Electric",
                "condition"=>"'electric'"
            )
        )
    ),
    "newField" => array (
        "dbField" => "NewField",
        "defaultItem" => "1",
        "selectedItem" => false,
        "items"=> array(
            "1" => array (
                "text"=> "All",
                "condition"=>false
            ),
            "2" => array (
                "text"=> "Value #1",
                "condition"=>"'value-1'"
            ),
            "3" => array (
                "text"=> "Value #2",
                "condition"=>"'value-2'"
            ),
            "4" => array (
                "text"=> "Value #3",
                "condition"=>"'value-3'"
            )
        )
    )
);

function processPostValues(&$selects, $postArr){
    // receives and process the values submitted by the user
    foreach($selects as $fieldName=>$selectObj){
        // check if the user sent the selected value thru post request, and check if exists the selected value in the items array.
        if (isset($postArr[$fieldName]) && isset($selectObj["items"][$postArr[$fieldName]])){
            $selects[$fieldName]["selectedItem"]=$postArr[$fieldName];
        }
    }
}

function drawSelectBoxes($selects){
    $str = "";
    foreach($selects as $fieldName=>$selectObj){
        $str .= "<select name='{$fieldName}' id='{$fieldName}' style='width: 200px' >";
        $selectedItem = ($selectObj["selectedItem"] != false)?$selectObj["selectedItem"]:$selectObj["defaultItem"];
        foreach ($selectObj["items"] as $key=>$item){
            $str .= "<option ";
            if ($key==$selectedItem){
                $str .= "selected='selected'"; 
            }
            $str .= " value='{$key}'>{$item['text']}</option>";
        }
        $str .= "</select><br>";
    }
    return $str;
}

function createQuery($selects){
    $query = "SELECT * from tblCar";
    $queryWhere = array();
    foreach($selects as $fieldName=>$selectObj){
        $selectedItem = ($selectObj["selectedItem"] != false)?$selectObj["selectedItem"]:$selectObj["defaultItem"];
        $condition = $selectObj["items"][$selectedItem]["condition"];
        $dbField = $selectObj["dbField"];
        if ($condition!=false){
            $queryWhere[]= "{$dbField} LIKE {$condition}";
        }
    }
    if (count($queryWhere)>0){
        $query .= " WHERE ".implode(" AND ",$queryWhere);
    }
    return $query;
}
processPostValues($selects, $_POST);
?>
<form method="POST">
    <?php
        echo drawSelectBoxes($selects);
    ?>
    <br>
    <button>Submit</button>
    <br>
    <strong>Query:</strong> 
    <?php
        echo createQuery($selects);
    ?>
</form>