dongqiang5541 2015-06-13 20:54
浏览 21
已采纳

Php过滤系统

I want to make a php filter system like this with 4 variables:

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

But how can I contruct a query that uses all the variables even when some filter settings aren't changed. It is possible to create 20 queries but that costs too much time. Is it possible to create something like this:

WHERE Tijd = $tijd AND Soortmaaltijd = $soortmaaltijd AND Soortgerecht = $soortmaaltijd AND Moeilijkheid = $moeilijkheid

But if some value is not set in the filter like 'Tijd', 'Tijd' has a standard value?

  • 写回答

1条回答 默认 最新

  • dongtang4019 2015-06-13 21:11
    关注

    You probably just need to construct your full WHERE in advance so that instead of sending 4 vars to your query you send a single WHERE statement.

    Example 1 (constructing a $where var which concatenates your conditions):

    $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
    

    This example assumes that you want to handle your empty vars as null queries. It's a bit unclear in your question whether you want to skip querying for null values or if you want to set your null vars to default values. As mentioned in comments, setting vars to toggle on default values is really easy using a ternary operator.

    Example 2 (setting a default value on empty vars):

    $default_tijd = "whatever you want";
    $tijd = ($tijd) ? $tijd : $default_tijd; // if $tijd is already set take
                                             // that value, else use $default_tijd
    

    However, I'm almost positive you don't want to return records for filters which aren't set to anything so setting a default value to your vars is just going filter records when you don't want to. What you probably want is the first example.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 速帮,学校需要在外上班没空
  • ¥15 人在外地出差,速帮一点点
  • ¥15 如何使用canvas在图片上进行如下的标注,以下代码不起作用,如何修改
  • ¥15 Windows 系统cmd后提示“加载用户设置时遇到错误”
  • ¥50 vue router 动态路由问题
  • ¥15 关于#.net#的问题:End Function
  • ¥15 无法import pycausal
  • ¥15 VS2022创建MVC framework提示:预安装的程序包具有对缺少的注册表值的引用
  • ¥15 weditor无法连接模拟器Local server not started, start with?
  • ¥20 6-3 String类定义