doubi6303 2015-11-03 09:07
浏览 11
已采纳

搜索两个字段不是必需的

I'd like to do a search function in my website. This research will show products based on product's name and seller's location. My view :

<?php echo $this->Form->create('Product', array('type' => 'GET')); ?>

<div class="col col-sm-4">
    <?php echo $this->Form->input('search', array('label' => false, 'div' => false, 'class' => 'form-control', 'autocomplete' => 'off', 'value' => $search)); ?>
</div>
    <div class="col col-sm-2">
        <?php echo $this->Form->input('searchCity', array('label' => false, 'div' => false, 'class' => 'form-control', 'autocomplete' => 'off')); ?>
    </div>
<div class="col col-sm-3">
    <?php echo $this->Form->button('Search', array('div' => false, 'class' => 'btn btn-sm btn-primary')); ?>
</div>

<?php echo $this->Form->end(); ?>

Two search fields : one for the name and the second for location.

My controller (worked for only one search field) :

if(!empty($this->request->query['search']) || !empty($this->request->data['name'])) {
        $search = empty($this->request->query['search']) ? $this->request->data['name'] : $this->request->query['search'];
        $search = preg_replace('/[^a-zA-Z0-9 ]/', '', $search);
        $terms = explode(' ', trim($search));
        $terms = array_diff($terms, array(''));
        $conditions = array(
            'Brand.active' => 1,
            'Product.active' => 1,
            'Product.date_discount >' => date('Y-m-d H:i:s')
        );
        foreach($terms as $term) {
            $terms1[] = preg_replace('/[^a-zA-Z0-9]/', '', $term);
            $conditions[] = array(
                'OR' => array(
                    array('Product.name LIKE' => '%' . $term . '%'),
                    //array('Brand.city LIKE' => '%' . $this->request->query['searchCity'] . '%')
                )
            );
        }
        $products = $this->Product->find('all', array(
            'recursive' => -1,
            'contain' => array(
                'Brand'
            ),
            'conditions' => $conditions,
            'limit' => 200,
        ));
        if(count($products) == 1) {
            return $this->redirect(array('controller' => 'products', 'action' => 'view', 'slug' => $products[0]['Product']['slug']));
        }
        $terms1 = array_diff($terms1, array(''));
        $this->set(compact('products', 'terms1'));
    }

No search fields are mandatory. If only name search field, it will show all products based on product's name whatever the location. If only city search field, it will show all products in location. If both, it will show all products based on name and location.

I don't know what to modify to do it. I tried to do another if(!empty($this->request->query['searchCity']) under the if I posted above but it didn't work (copy paste the first if and I changed conditions). What should I do?

Thanks.

  • 写回答

2条回答 默认 最新

  • douyiavxxh02727 2015-11-03 10:35
    关注

    You can use this CakeDC Search plugin

    First include the plugin in your app

    CakePlugin::load('Search');
    

    Then in your model include the behavior

    public $actsAs = array(
       'Search.Searchable'
    );
    

    and

    public $filterArgs = array(
        'search' => array(
            'type' => 'like'
        ),
        'searchcity' => array(
            'type' => 'like'
        )
    );
    

    and in your controller

    public $components = array(
        'Search.Prg','Paginator'
    );
    
    public function find() {
        $this->Prg->commonProcess();
        $this->Paginator->settings['conditions'] = $this-> Product->parseCriteria($this->passedArgs);
        $this->set('products', $this->Paginator->paginate());
    }
    

    For complete example visit here

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?