How set AND condition to ALL columns - php

梦想与她 提交于 2019-12-02 06:04:15

问题


In MY TABLE if I type

floor fly

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.

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

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

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

To dispel any doubts on what I want: LOOK

This is an example picture of that I want


回答1:


This is a bit long for a comment.

For this type of search, you might consider a full text index. The documentation is here. These implement the MATCH . . . AGAINST functionality.

With such functionality, you can order the results by relevance. This means that you do not have to decide in advance whether the connector is "and" or "or" between multiple words. You can also implement a boolean search, which would allow for more complex complex logic, if you so desire.




回答2:


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

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


来源:https://stackoverflow.com/questions/29728752/how-set-and-condition-to-all-columns-php

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!