PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Wednesday, February 16, 2022

[FIXED] Cakephp search feature doesn't show the desired items

 February 16, 2022     cakephp, php, search     No comments   

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'];?>
                     &euro;
				</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
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing