Issue
I am new to CakePHP, but I'm starting to get into it. I've been having some problems with adding a search feature on my project. I've tried to add with with the instructions of various youtube tutorials and been trying to modify them to work in my project but nothing seems to work. I appreciate the help already!
So here's my Controller code:
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'));
}
}
And here is my index file (main page):
<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'];?>
€
</h5>
</div>
</div>
</div>
<?php endforeach;?>
</div>
Hopefully someone has a solution for my problem, thanks! :)
Solution
It seems you didn't add LIKE clause to match the search condition.
Try to change
$this->set('products', $this->Product->find('all', array('conditions' => '%'.$keyword.'%')));
to
$this->set('products',
$this->Product->find('all',
array(
'conditions' =>
array(
'Product.name LIKE ' => '%'.$keyword.'%'
)
)
)
);
Answered By - Sehdev
0 Comments:
Post a Comment
Note: Only a member of this blog may post a comment.