搜索两个字段不是强制性的 [英] Search with two fields not mandatory

查看:91
本文介绍了搜索两个字段不是强制性的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的网站上执行搜索功能。本研究将根据产品名称和卖家位置显示产品。
我的意见:

 <?php echo $ this-> Form-> create ,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(); ?>

两个搜索字段:一个用于名称,另一个用于位置。



我的控制器(仅适用于一个搜索字段):

  if - > 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('Ymd 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,
'contains' & 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'));
}

没有搜索字段是必填字段。如果只有名称搜索字段,它将显示基于产品名称的所有产品,无论位置。如果只有城市搜索字段,它将显示所有产品的位置。如果两者都会显示基于名称和位置的所有产品。



我不知道要修改什么。我试图做另一个 if(!empty($ this-> request-> query ['searchCity'])下如果我发布上面,工作(复制粘贴第一个如果我改变条件)
我应该怎么办?



谢谢。

解决方案

您可以使用此 CakeDC搜索插件

首先在应用程序中包含插件

  CakePlugin :: load搜索'); 

然后在您的模型中包含行为

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

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

);

b
$ b

  public $ components = array(
'Search.Prg','Paginator'
);

公共函数find(){
$ this-> Prg-> commonProcess();
$ this-> Paginator-> settings ['conditions'] = $ this-> Product-> parseCriteria($ this-> passedArgs);
$ this-> set('products',$ this-> Paginator-> paginate());
}

有关完整示例,请访问此处


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.

解决方案

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

这篇关于搜索两个字段不是强制性的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆