如何设置AND条件到所有列 - php [英] How set AND condition to ALL columns - php

查看:99
本文介绍了如何设置AND条件到所有列 - php的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我输入

floor fly

表返回没有匹配的记录因为全局搜索在一个SINGLE列中的php功能搜索记录。

但是我想要AND条件适用于所有列。

table returns No matching records because Global Search php function search records inside a SINGLE column.
But I want that AND condition works to ALL columns.

如果我键入 floor fly 表应该显示如下:

If I type floor fly table should me display something like:

|__Column1___|___Column2____|__Column4_|
|            |              |          |
|..FLOOR...  |DREAMS - FLY  |  1994    |
| ..dreams   |    xyz       | floor    |

注意:这不是OR函数 abc OR cde

这是我的PHP代码,但是不按照我的预期工作:

Attention: this is not a OR function abc OR cde
This is my php code but AND don't work like as I expect:

static function filter ( $request, $columns, &$bindings )
    {
        $globalSearch = array();
        $columnSearch = array();
        $dtColumns = self::pluck( $columns, 'dt' );

        if ( isset($request['search']) && $request['search']['value'] != '' ) {
            $str = $request['search']['value'];

            for ( $i=0, $ien=count($request['columns']) ; $i<$ien ; $i++ ) {
                $requestColumn = $request['columns'][$i];
                $columnIdx = array_search( $requestColumn['data'], $dtColumns );
                $column = $columns[ $columnIdx ];

                if ( $requestColumn['searchable'] == 'true' ) {
                    $binding = self::bind( $bindings, '%'.$str.'%', PDO::PARAM_STR );
                    $globalSearch[] = "".$column['db']." LIKE ".$binding;
                }
            }
        }

消除对什么有疑问我想要: LOOK

To dispel any doubts on what I want: LOOK

这是我想要的示例图片

推荐答案

你应该决定要使用哪些搜索条件。
如果您希望使用匹配的某个短语,并且不想使用全文搜索,则应重新构建您的查询,如:

well, you should decide what search criteria you want to use. If you want you use match by certain phrase, and do not want to use FULL TEXT search, you should rebuild your query like:

SELECT * FROM a WHERE a.col1 REGEXP 'text1|text2|text3' OR a.col2 REGEXP 'text1|text2|text3';

在您的PHP代码中,应该是这样的(你没有指定输入数据格式,所以我假设你使用$ str作为空格分隔的文本,并且想要检查$ str短语中的所有单词进行搜索:

In your PHP code, it should be smth like this (you did not specify input data format, so I assume, you are using "$str" as space separated text, and want to check all words in "$str" phrase for search:

if ( $requestColumn['searchable'] == 'true' ) {
   $str = str_replace(" ","|",$str);
   $binding = self::bind( $bindings, $str, PDO::PARAM_STR );
   $globalSearch[] = "".$column['db']." REGEXP ".$binding;
}

这篇关于如何设置AND条件到所有列 - php的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆