简体   繁体   English

php中如何实现多个搜索过滤器

[英]How to implement multiple search filters in php

I m trying to implement filters in php and mysql to search record in database....Filters should be like if among 6 filters only two is selected query will perform "AND" operation for these two and fetch data from database and if three then it will perform "AND" operation for these three filters..... one way to implement is to check for each filter like.我正在尝试在 php 和 mysql 中实现过滤器以搜索数据库中的记录....如果在 6 个过滤器中只有两个被选中,过滤器应该是这样的,查询将对这两个执行“AND”操作并从数据库中获取数据,如果三个则它将对这三个过滤器执行“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 ...

But the problem is if i have 6 filters then i have to create 64 combinations for it... I m thinking about that is there any alternative solution exists for it?但问题是,如果我有 6 个过滤器,那么我必须为其创建 64 种组合……我在考虑是否存在其他解决方案?

This could work, assuming that your $_GET keys match columns in your table:假设您的$_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';"

I think this may work, although this code sample does not include sanitization against SQL injection, so you should probably add something to scrub potentially dangerous input我认为这可能有效,尽管此代码示例不包括针对 SQL 注入的清理,因此您可能应该添加一些内容来清除潜在危险的输入

Updated : added $filtered_get = array_filter($_GET);更新:添加$filtered_get = array_filter($_GET); to filter out any empty fields from $_GET array$_GET数组中过滤掉任何空字段

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

You can use a prefix for the form elements, like filter-, so items will look like filter-age, filter-name and so on.您可以为表单元素使用前缀,例如 filter-,因此项目看起来像 filter-age、filter-name 等。 Now, let's suppose you have a function like现在,让我们假设你有一个像

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);
}

and then you can call it like然后你可以称之为

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

I tried using the answer from @tshimkus.我尝试使用@tshimkus 的答案。 Maybe it's the way I have the arrays set up or something but there were a couple problems I ran into before getting it to work.也许这是我设置阵列的方式或其他方式,但在让它工作之前我遇到了一些问题。

this line:这一行:

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

for me needed to be:对我来说需要:

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

I also didn't get the check (shown below) for last item to work.我也没有收到最后一项工作的支票(如下所示)。

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

ended up using a count along with this instead:最终使用了一个计数代替:

if($i!==$len){
$whereClause = '  ';
  
    if ( !empty($_POST['gender']) || !empty($_POST['hobby']) || !empty($_POST['search_filter']) ) {
    
    $whereClause .= ' WHERE ';
    if ( !empty($_POST['gender']) && !empty($_POST['hobby']) && !empty($_POST['search_filter']) ) {
        $whereClause .= ' gender = "'.$_POST['gender'].'" AND gender = "'.$_POST['gender'].'" AND search_filter LIKE "%'.$_POST['search_filter'].'%" ';

    } else {

        if ( !empty($_POST['gender']) && $_POST['gender'] != "" && empty($_POST['hobby']) && empty($_POST['search_filter']) ) {
            $whereClause .= ' gender = "'.$_POST['gender'].'"  ';          
        }
        else if ( (!empty($_POST['gender']) && $_POST['gender'] != "") &&  (!empty($_POST['hobby']) || !empty($_POST['search_filter']))) {
            $whereClause .= ' gender = "'.$_POST['gender'].'"  '; 
            $whereClause .= ' AND ';
        }
        
        if ( !empty($_POST['hobby']) && $_POST['hobby'] != "" && empty($_POST['gender']) && empty($_POST['name'])){
            $whereClause .= ' hobby = "'.$_POST['hobby'].'"  ';           
        }
        else if ( (!empty($_POST['hobby']) && $_POST['hobby'] != "") &&  (!empty($_POST['gender']) || !empty($_POST['search_filter']))) {
            $whereClause .= ' hobby = "'.$_POST['hobby'].'"  '; 
            $whereClause .= ' AND ';
        } 
        
        if ( !empty($_POST['search_filter'])) {
            $whereClause .= ' search_filter LIKE "%'.$_POST['search_filter'].'%"  ';       
        }    
    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM