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')
        )
    
    评论

报告相同问题?