php - 如何一次在多个字段中搜索多个单词?

标签 php mysql cakephp search-engine cakephp-2.3

我正在编写一个搜索模块。我不知道我是不是在一个好的方式,但我现在听起来不错,除了一分。

使用下面的代码我只能搜索一个词。

<?php
class SearchesController extends AppController{

    function index(){

        if($this->request->is('put') || $this->request->is('post') ){
            // Save the send data
            $search = $this->request->data['Search']['key']; 

            //$nbWords = str_word_count($search);
            /*
            THIS DOES NOT WORKS
            if($nbWords > 1){
                $searchTerms = explode(' ', $search);

                $searchTermBits = array();
                foreach ($searchTerms as $term) {
                    $term = trim($term);
                    if (!empty($term)) {
                        $searchTermBits[] = "Page.name LIKE '%$term%' AND Page.content LIKE '%$term%'";
                    }
                }



            }else{
                $term = $search;
                $searchTermBits[] = "Page.name LIKE '%$term%' AND Page.content LIKE '%$term%'";

            }
            */
            // SEARCH IN Pages TABLE
            $this->loadModel('Page');
            // SAVE THE RESULT IN
            $d['pages'] = $this->Page->find('all', array(
                'conditions' => array(
                    'online'=>'1',
                    'type'=>'page',
                    'created <= NOW()',
                    'id > 1',
                    'OR' => array(
                        //implode(' AND ', $searchTermBits)
                        'Page.name LIKE'=>'%'.$search.'%',
                        'Page.content LIKE'=>'%'.$search.'%'
                        )
                    ),
                'fields' => array('name','content','created','id','type')
            ));
            //SEARCH IN Posts TABLE
            $this->loadModel('Post');
            $d['posts'] = $this->Post->find('all', array(
                'conditions' => array(
                    'online'=>'1',
                    'type'=>'post',
                    'created <= NOW()',
                    'OR' => array(
                        'Post.name LIKE'=>'%'.$search.'%',
                        'Post.content LIKE'=>'%'.$search.'%'
                        )
                    ),
                'fields'=> array('name','content','created','id','type','slug')
            ));
            // SEND THE VALUES TO THE VIEW
            $this->set($d);

        }else{
            echo 'e';
        }
    }

}

问题,我希望能够用不同的词进行搜索,例如“red car”、“house monrain”、“web html5 development”

使用我的代码,如果我输入一个单词,它会返回一个结果,但如果我将第二个单词添加到第一个单词(带空格),它不会返回任何结果。

我怎样才能改变这个

'OR' => array(
        'Post.name LIKE'=>'%'.$search.'%',
        'Post.content LIKE'=>'%'.$search.'%'
)

让它能够在“名称”和“内容”中搜索几个词?

谢谢你的帮助 干杯

最佳答案

你可以试试这个:

'OR' => array(
       'Post.name' IN ('name1', 'name2'),
       'Post.content' IN ('content1', 'content2')
)

显然你可以像这样把你的值放在数组中:

$name_arr = array();
$name_arr[] = 'name1';
$name_arr[] = 'name2';

及之后:

'OR' => array(
           'Post.name' IN ($name_arr),
           'Post.content' IN ('content1', 'content2')
    )

关于php - 如何一次在多个字段中搜索多个单词?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20582019/

相关文章:

php - 多图上传系统

mysql - MariaDB 中的慢 SQL

MySQL 选择类似路径的位置

php - cakePHP 表单不向数据库发送数据

Cakephp表单单选按钮标签类

php - Laravel 函数中无法识别变量

php - Laravel Socialite 访问 token 和 token

php - 如何使用Elastic Search PHP获取Elastic Search中的文档数

php - CakePHP 3 Composer 安装错误

php - 无法选择具有奇数整数的列