Cakephp搜索功能不显示所需的项目

问题描述 投票:1回答:1

我是CakePHP的新手,但我开始深入研究它。我在项目中添加搜索功能时遇到了一些问题。我试图添加各种youtube教程的说明,并试图修改它们在我的项目中工作,但似乎没有任何工作。我已经感谢你的帮助!

所以这是我的控制器代码:

class ProductsController extends AppController {

public function index() {
   /* $keyword = $this->request->query('keyword');
        if(!empty($keyword)){
        $this-set('products', $this->Product->find()); 
        }     */
    $keyword = $this->request->data('keyword');
    if($keyword != null) 
    {
        $this->set('products', $this->Product->find('all', array('conditions' => '%'.$keyword.'%')));
    }
    else {
        $this->set('products', $this->Product->find('all'));
    }   


}

这是我的索引文件(主页):

    <div id="listaus">
    <?= $this->Html->link('+ Listaa Uusi Auto', ['action' => 'add']) ?>
    
    <?= $this->Form->create("", array('type' => 'get')); ?>   
        <?= $this->Form->control('keyword'); ?>
    <?= $this->Form->end(__('Search')); ?>
</div>

<div class="row">
	<?php foreach ($products as $product):?>
	<div class="col-sm-6 col-md-4">
		<div class="">
			<?php echo $this->Html->link($this->Html->image($product['Product']['kuva']),
					array('action'=>'view',$product['Product']['id']),
					array('escape'=>false,'class'=>'thumbnail'));?>
			<div class="caption">
				<h4>
					<?php echo $product['Product']['name'];?>
				</h4>
				<h5>
					Price:
					<?php echo $product['Product']['hinta'];?>
                     &euro;
				</h5>
			</div>
		</div>
	</div>
	<?php endforeach;?>
</div>

希望有人能解决我的问题,谢谢! :)

php search cakephp
1个回答
0
投票

看来你没有添加LIKE子句来匹配搜索条件。

试着改变

$this->set('products', $this->Product->find('all', array('conditions' => '%'.$keyword.'%')));

 $this->set('products', 
      $this->Product->find('all', 
        array(
         'conditions' => 
            array(
                   'Product.name LIKE ' => '%'.$keyword.'%'
            )
        )
      )
    );
© www.soinside.com 2019 - 2024. All rights reserved.