用于多个条目的PHP Sql

时间:2015-07-28 02:04:17

标签: php mysql

我有一个带有几个选择选项的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>

所以这些是选项,现在用户可以选择然后搜索。现在我想编写一个PHP代码,它将根据选择运行查询。因此,如果用户在第一个选项中选择ALL而在第二个选项中选择Petrol,那么SQL将

SELECT * from tblCar Where Fuel = '".#$fuel."'";

这是有效的,但我会有很多Sql语句,因为可能有多个组合,我打算添加更多的选择框,所以if条件会很多。

任何更简单的方法

感谢您的帮助

1 个答案:

答案 0 :(得分:0)

您可以使用数组执行此操作。这样的事情。

<?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>