dongxi1320 2013-12-14 10:15
浏览 38

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

I am coding a search module. I do not know if I am ina good way but ti sound good for now excepted one point.

With the following code I can search only one word.

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

}

The problem, I would like to be able to search with different words, for exeple "red car", "house monrain","web html5 development"

With my code, if I enter one word, it return a result, but if I add second word to the first (with a space) , it retuirn no result.

How can I change this

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

to make it able to search in 'name' and 'content' several words?

Thak for your help Cheers

  • 写回答

4条回答 默认 最新

  • doubiankang2845 2013-12-14 10:21
    关注

    you can try this:

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

    Obviously you can put your value in array like this:

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

    and after:

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

报告相同问题?

悬赏问题

  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图