【问题标题】:How to implement multiple search filters in php如何在php中实现多个搜索过滤器
【发布时间】:2019-07-03 12:43:02
【问题描述】:

我正在尝试在 php 和 mysql 中实现过滤器以在数据库中搜索记录....过滤器应该类似于如果在 6 个过滤器中只选择了两个,查询将对这两个执行“AND”操作并从数据库中获取数据和如果是三个,那么它将对这三个过滤器执行“与”操作..... 一种实现方法是检查每个过滤器。

if (isset($_GET['name'] && isset($_GET['city'])) { 
    // perform AND for these two
} elseif(isset($_GET['name'] && (isset($_GET['age'])) {
    // perform AND for these three
}

// and so on ...

但问题是,如果我有 6 个过滤器,那么我必须为其创建 64 个组合... 我正在考虑是否有任何替代解决方案存在?

【问题讨论】:

    标签: php mysql search filter


    【解决方案1】:
    single if condition to check value is empty or not.for non-empty then mysql query is two between 'AND' operation.
    $query = 'select * from info';
    $where = ' Where';
    $and = 'id = 1';
    if(!empty($_GET['name'])){
      $and .= ' AND name="test"';
    }
    if(!empty($_GET['city'])){
      $and .= ' AND city="test"';
    }
    $q = $query.''.$where.''.$and;
    make like this query is 'select & from info where id=1 AND name="test" AND city="test"';
    

    【讨论】:

      【解决方案2】:

      假设您的 $_GET 键与表中的列匹配,这可能会起作用:

      $query = "SELECT * FROM table";
      
      $filtered_get = array_filter($_GET); // removes empty values from $_GET
      
      if (count($filtered_get)) { // not empty
          $query .= " WHERE";
      
          $keynames = array_keys($filtered_get); // make array of key names from $filtered_get
      
          foreach($filtered_get as $key => $value)
          {
             $query .= " $keynames[$key] = '$value'";  // $filtered_get keyname = $filtered_get['keyname'] value
             if (count($filtered_get) > 1 && (count($filtered_get) > $key)) { // more than one search filter, and not the last
                $query .= " AND";
             }
          }
      }
      $query .= ";"
      

      $query = "SELECT * FROM table WHERE name = 'name' AND city = 'city' AND age = 'age';"

      我认为这可能有效,尽管此代码示例不包括针对 SQL 注入的清理,因此您可能应该添加一些内容来清除潜在的危险输入

      更新:添加$filtered_get = array_filter($_GET); 以过滤掉$_GET 数组中的任何空字段

      【讨论】:

      • 如果在这里传递一个空的输入文本值会发生什么情况。“AND”可能不会起作用
      • 我在 $_GET 数组中添加了一个 array_filter() 函数来清除所有空字段,这应该构建查询以仅包含已设置的过滤器
      【解决方案3】:

      您可以为表单元素使用前缀,例如 filter-,因此项目看起来像 filter-age、filter-name 等。现在,假设你有一个类似的函数

      function buildFilters($input, $delimiter) {
          foreach ($input as $key => $value) {
              $filters = array();
              $filters[]="1=1";//default filter
              if (strpos($key, $prefix) === 0) {
                  //add the filter to $filters via $filters[]=yourvalue
              }
          }
          return implode($delimiter, $filters);
      }
      

      然后你可以这样称呼它

      $myFilters = buildFilters($_POST, " AND ");
      

      【讨论】:

      • @Inam 我将您的逻辑转换为循环并将其分离为函数。为了向您提供更多信息,我需要进一步了解您所拥有的表格。
      • 我的表单是这样的......
      • @Inam 您可以更改您的表单,以便 name = "filter-title" 等以使其与我给您的代码兼容。
      【解决方案4】:

      我尝试使用@tshimkus 的答案。也许这是我设置阵列的方式或其他方式,但在让它工作之前我遇到了几个问题。

      这一行:

      $query .= " $keynames[$key] = '$value'";
      

      对我来说需要:

      $query .= " $key = '$value'";
      

      我也没有得到最后一项工作的支票(如下所示)。

      if (count($filtered_get) > 1 && (count($filtered_get) > $key)) { // more than one search filter, and not the last
      

      最终使用了一个计数来代替:

      if($i!==$len){
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-03-08
        • 2011-09-06
        • 1970-01-01
        • 2018-09-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多