Php过滤系统

Php过滤系统,php,mysql,filter,Php,Mysql,Filter,我想用4个变量制作一个php过滤系统: echo"<form action='' method='GET' class='form-inline' role='form'>"; $query = "SELECT Naam FROM Soortmaaltijd"; //Alle soortmaaltijden ophalen $result= mysql_query($query) or die(mysql_error()); echo"<div class='row'>"

我想用4个变量制作一个php过滤系统:

echo"<form action='' method='GET' class='form-inline' role='form'>";
$query = "SELECT Naam FROM Soortmaaltijd"; //Alle soortmaaltijden ophalen
$result= mysql_query($query) or die(mysql_error());
echo"<div class='row'>";
    echo"<div class='form-group' >";
    echo"<label for='soortmaaltijd'>Soort maaltijd</label></br>";
    echo"<select name='Soortmaaltijd' class='form-control' id='soortmaaltijd'>";
        echo"<option value=''>Alle</option>";
    while($row=mysql_fetch_array($result)) {        
        echo"<option value='$row[SoortmaaltijdID]'>$row[Naam]</option>";
    }
    echo"</select>";
    echo"</div>";

    $query = "SELECT * FROM Soortgerecht"; //Alle soortgerechten ophalen
    $result= mysql_query($query) or die(mysql_error());
    echo"<div class='form-group' >";
    echo"<label for='soortgerecht'>Soort gerecht</label></br>";
    echo"<select name='soortgerecht' class='form-control' id='soortgerecht'>";
        echo"<option value=''>Alle</option>";
    while($row=mysql_fetch_array($result)) {        
        echo"<option value='$row[SoortgerechtID]'>$row[Naam]</option>";
    }
    echo"</select>";
    echo"</div>";

    echo"<div class='form-group' >";
    echo"<label for='moeilijkheid'>Moeilijkheid</label></br>";//Moeilijkheid
    echo"<select name='moeilijkheid' class='form-control' id='moeilijkheid'>";
        echo"<option value=''>Alle</option>";       
        echo"<option value='1'>1</option>";
        echo"<option value='2'>2</option>";
        echo"<option value='3'>3</option>";
    echo"</select>";
    echo"</div>";

    echo"<div class='form-group' >";
    echo"<label for='tijd'>Max bereidingstijd</label></br>";//Max bereidingstijd
    echo"<select name='tijd' class='form-control' id='tijd'>";
        echo"<option value=''>Alle</option>";       
        echo"<option value='5'><5</option>";
        echo"<option value='10'><10</option>";
        echo"<option value='15'><15</option>";
        echo"<option value='20'><20</option>";
        echo"<option value='25'><25</option>";
        echo"<option value='30'><30</option>";
    echo"</select>";
    echo" <button type='submit' name='filter' class='btn btn-primary btn-lg-2'>Filter</button>";
    echo"</div>";
echo"</div>";
echo"</form>"; ?>

但是如果过滤器中没有设置某些值,如“Tijd”,“Tijd”有一个标准值吗

您可能只需要提前构建完整的
WHERE
,这样就可以发送一条
WHERE
语句,而不是向查询发送4个变量

示例1(构造连接条件的$where变量):

$where = "WHERE ";
$count = 0;
if ( !empty($tijd) ) {

    $where .= "`Tijd` = " . $tijd . " ";
    ++$count;

} elseif( !empty($soortmaaltijd) ) {

    if ($count == 0)
        $where .= "`Soortmaaltijd` = " . $soortmaaltijd . " ";
    else
        $where .= "AND `Soortmaaltijd` = " . $soortmaaltijd . " ";

    ++$count;

} elseif( !empty($soortgerecht) ) {

    if ($count == 0)
        $where .= "`Soortgerecht` = " . $soortgerecht . " ";
    else
        $where .= "AND `Soortgerecht` = " . $soortgerecht . " ";

    ++$count;

} elseif( !empty($moeilijkheid) ) {

    if ($count == 0)
        $where .= "`Moeilijkheid` = " . $moeilijkheid . " ";
    else
        $where .= "AND `Moeilijkheid` = " . $moeilijkheid . " ";

    ++$count;

} else {
    $where = null; // if none of the conditions are met we null 
                   // the entire `WHERE` statement so we can safely
                   // send to our SQL query regardless of no conditions
                   // being met
}

// then your sql statement could be something like:
$sql = "SELECT *
        FROM tablename
        $where";  // remember: if ($where == null)
                  // that means no filters are set and
                  // all records from table are returned
$default_tijd = "whatever you want";
$tijd = ($tijd) ? $tijd : $default_tijd; // if $tijd is already set take
                                         // that value, else use $default_tijd
本例假设您希望将空变量作为
null
查询处理。您的问题中有点不清楚是要跳过对空值的查询,还是要将空变量设置为默认值。如注释中所述,使用三元运算符将变量设置为启用默认值非常容易

示例2(设置空变量的默认值):

$where = "WHERE ";
$count = 0;
if ( !empty($tijd) ) {

    $where .= "`Tijd` = " . $tijd . " ";
    ++$count;

} elseif( !empty($soortmaaltijd) ) {

    if ($count == 0)
        $where .= "`Soortmaaltijd` = " . $soortmaaltijd . " ";
    else
        $where .= "AND `Soortmaaltijd` = " . $soortmaaltijd . " ";

    ++$count;

} elseif( !empty($soortgerecht) ) {

    if ($count == 0)
        $where .= "`Soortgerecht` = " . $soortgerecht . " ";
    else
        $where .= "AND `Soortgerecht` = " . $soortgerecht . " ";

    ++$count;

} elseif( !empty($moeilijkheid) ) {

    if ($count == 0)
        $where .= "`Moeilijkheid` = " . $moeilijkheid . " ";
    else
        $where .= "AND `Moeilijkheid` = " . $moeilijkheid . " ";

    ++$count;

} else {
    $where = null; // if none of the conditions are met we null 
                   // the entire `WHERE` statement so we can safely
                   // send to our SQL query regardless of no conditions
                   // being met
}

// then your sql statement could be something like:
$sql = "SELECT *
        FROM tablename
        $where";  // remember: if ($where == null)
                  // that means no filters are set and
                  // all records from table are returned
$default_tijd = "whatever you want";
$tijd = ($tijd) ? $tijd : $default_tijd; // if $tijd is already set take
                                         // that value, else use $default_tijd

但是,我几乎可以肯定的是,您不希望返回未设置为任何值的筛选器的记录,因此为VAR设置默认值只是在您不希望的情况下过滤记录。您可能想要的是第一个示例。

您可能只需要提前构建完整的
WHERE
,这样,您就可以发送一条
WHERE
语句,而不是向查询发送4个变量

示例1(构造连接条件的$where变量):

$where = "WHERE ";
$count = 0;
if ( !empty($tijd) ) {

    $where .= "`Tijd` = " . $tijd . " ";
    ++$count;

} elseif( !empty($soortmaaltijd) ) {

    if ($count == 0)
        $where .= "`Soortmaaltijd` = " . $soortmaaltijd . " ";
    else
        $where .= "AND `Soortmaaltijd` = " . $soortmaaltijd . " ";

    ++$count;

} elseif( !empty($soortgerecht) ) {

    if ($count == 0)
        $where .= "`Soortgerecht` = " . $soortgerecht . " ";
    else
        $where .= "AND `Soortgerecht` = " . $soortgerecht . " ";

    ++$count;

} elseif( !empty($moeilijkheid) ) {

    if ($count == 0)
        $where .= "`Moeilijkheid` = " . $moeilijkheid . " ";
    else
        $where .= "AND `Moeilijkheid` = " . $moeilijkheid . " ";

    ++$count;

} else {
    $where = null; // if none of the conditions are met we null 
                   // the entire `WHERE` statement so we can safely
                   // send to our SQL query regardless of no conditions
                   // being met
}

// then your sql statement could be something like:
$sql = "SELECT *
        FROM tablename
        $where";  // remember: if ($where == null)
                  // that means no filters are set and
                  // all records from table are returned
$default_tijd = "whatever you want";
$tijd = ($tijd) ? $tijd : $default_tijd; // if $tijd is already set take
                                         // that value, else use $default_tijd
本例假设您希望将空变量作为
null
查询处理。您的问题中有点不清楚是要跳过对空值的查询,还是要将空变量设置为默认值。如注释中所述,使用三元运算符将变量设置为启用默认值非常容易

示例2(设置空变量的默认值):

$where = "WHERE ";
$count = 0;
if ( !empty($tijd) ) {

    $where .= "`Tijd` = " . $tijd . " ";
    ++$count;

} elseif( !empty($soortmaaltijd) ) {

    if ($count == 0)
        $where .= "`Soortmaaltijd` = " . $soortmaaltijd . " ";
    else
        $where .= "AND `Soortmaaltijd` = " . $soortmaaltijd . " ";

    ++$count;

} elseif( !empty($soortgerecht) ) {

    if ($count == 0)
        $where .= "`Soortgerecht` = " . $soortgerecht . " ";
    else
        $where .= "AND `Soortgerecht` = " . $soortgerecht . " ";

    ++$count;

} elseif( !empty($moeilijkheid) ) {

    if ($count == 0)
        $where .= "`Moeilijkheid` = " . $moeilijkheid . " ";
    else
        $where .= "AND `Moeilijkheid` = " . $moeilijkheid . " ";

    ++$count;

} else {
    $where = null; // if none of the conditions are met we null 
                   // the entire `WHERE` statement so we can safely
                   // send to our SQL query regardless of no conditions
                   // being met
}

// then your sql statement could be something like:
$sql = "SELECT *
        FROM tablename
        $where";  // remember: if ($where == null)
                  // that means no filters are set and
                  // all records from table are returned
$default_tijd = "whatever you want";
$tijd = ($tijd) ? $tijd : $default_tijd; // if $tijd is already set take
                                         // that value, else use $default_tijd

但是,我几乎可以肯定的是,您不希望返回未设置为任何值的筛选器的记录,因此为VAR设置默认值只是在您不希望的情况下过滤记录。您可能想要的是第一个示例。

只需根据设置的值动态构建查询字符串。当然,请看一下“三元运算符”就可以了……请注意,
mysql\u
函数现在不再像过去几年那样被禁止使用,而是正式使用。您应该真正使用或,因为这段代码很快就会停止工作。另请参见根据设置的值动态构建查询字符串。当然,请查看“三元运算符”…请注意,
mysql\uuu
函数现在不再像过去几年那样被禁止使用,而是正式使用。您应该真正使用或,因为这段代码很快就会停止工作。另请参见根据设置的值动态构建查询字符串。当然,请查看“三元运算符”…请注意,
mysql\uuu
函数现在不再像过去几年那样被禁止使用,而是正式使用。您应该真正使用或,因为这段代码很快就会停止工作。也看到