1

im new here and using CakePHP, I want make a searcher in my website but i have this error and warning:

Notice (8): Undefined variable: negocios [APP\View\Negocios\buscar.ctp, line 3] Warning (2): Invalid argument supplied for foreach() [APP\View\Negocios\buscar.ctp, line 3]

This is my code from the controller:

<?php

class NegociosController extends AppController {

public $components = array('Paginator');
public $helpers = array('Html', 'Form');

public function index(){
    $this->paginate = array('limit' => 1);
    $negocios = $this->paginate('Negocio');
    $this->set(compact('negocios'));
}


public function ver($id = null, $nombre){
    $this->Negocio->id = $id;
    $this->set('negocio', $this->Negocio->read());
}

public function buscar(){
    if(!empty($this->data)){
        $query = $this->data['Negocio']['buscador'];
        $this->set($query, $this->data['Negocio']['buscador']);
        $conditions = array(
            'conditions' => array(
                'or' => array(
                    'Negocio.titulo LIKE %$query%',
                    'Negocio.direccion LIKE %$query%',
                    'Negocio.lugar LIKE %$query%',
                    'Negocio.descripcion LIKE %$query%',
                    'Negocio.direccion LIKE %$query%',
                    'Negocio.email LIKE %$query%',
                    'Negocio.web LIKE %$query%',
                    'Negocio.facebook LIKE %$query%',
                    'Negocio.twitter LIKE %$query%',
                    'Negocio.categoria LIKE %$query%'
                    )
                )
            );
        $this->set('negocios', $this->Negocio->find('all', $conditions));
    }
}

}

?>

My View:

<?php

foreach ($negocios as $key => $negocio) {
    echo $negocio['Negocio']['titulo'].'<br />';
    echo $negocio['Negocio']['descripcion'].'<br />';
    echo $negocio['Negocio']['categoria'].'<br />';

}

?>

@cornelb I have this:

<?php 

echo $this->Form->create(array('controller' => 'negocios', 'action' => 'buscar'));
echo $this->Form->input('buscador', array('placeholder' => 'Buscar negocios...', 'label' => false));
echo $this->Form->submit('search.png', array('type' => 'button', 'id' => 'buscar', 'div' => false));
echo $this->Form->end();

?>

and when im searching something i get this error:

SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '%$query%) OR (Negocio.direccion LIKE %$query%) OR (Negocio.lugar LIKE %$' at line 1

but if i use the "get" method i get nothing.

4
  • Your view code is messed up.
    – user221931
    Jan 22, 2014 at 17:56
  • and... which view is that?
    – Dave
    Jan 22, 2014 at 17:57
  • 1
    you are only sending $negocios to the view inside the " if(!empty($this->data)){" condition. You should send an empty one even if the user has not searched for anything. Does the search return anything?
    – cornelb
    Jan 22, 2014 at 18:01
  • @user221931 My view is obviously not finished, it was only to see if it worked and see the information displayed and i search something only can see this in my view "buscar.ctp" Notice (8): Undefined variable: negocios [APP\View\Negocios\buscar.ctp, line 3] Warning (2): Invalid argument supplied for foreach() [APP\View\Negocios\buscar.ctp, line 3]
    – Hail Hydra
    Jan 22, 2014 at 18:05

3 Answers 3

1

try this one .

'Negocio.titulo LIKE' => "%" . $query. "%",
1

You can use Match Against Query also

$search = 'Search String';

$conditions = array( 
 "MATCH(Post.title) 
  AGAINST('$search' IN BOOLEAN MODE)" 
  );
 $matches = $this->Post->find('all', array('conditions' => $conditions));

for Match Against you need to change table "Storage Engine" into "MYISAM" and also make fulltext index on fields.

0

This would add the form to the view.

echo $this->Form->create('Negocio');
echo $this->Form->input('buscador');
echo $this->Form->submit('Search');
echo $this->Form->end();

And in the controller:

public function buscar(){
    $negocios = array();
    if (!empty($this->request->data)) {
        $query = $this->request->data['Negocio']['buscador'];
        $conditions = array(
            'conditions' => array(
                'or' => array(
                    'Negocio.titulo LIKE' => "%$query%",
                    'Negocio.direccion LIKE' => "%$query%",
                    'Negocio.lugar LIKE' => "%$query%",
                    'Negocio.descripcion LIKE' => "%$query%",
                    'Negocio.direccion LIKE' => "%$query%",
                    'Negocio.email LIKE' => "%$query%",
                    'Negocio.web LIKE' => "%$query%",
                    'Negocio.facebook' => "%$query%",
                    'Negocio.twitter' => "%$query%",
                    'Negocio.categoria LIKE' => "%$query%",                    )
            )
        );
        $negocios = $this->Negocio->find('all', $conditions);
    }

    $this->set('negocios', $negocios);
}
5
  • thanks for your answer but i have some problems can you check my post again i added information.
    – Hail Hydra
    Jan 22, 2014 at 19:03
  • I have updated the answer. The syntax for %LIKE% was different (example: 'Negocio.titulo LIKE' => "%$query%"
    – cornelb
    Jan 22, 2014 at 19:09
  • thank you, problem solved :) , but i have another question, it is working now with post method but with get i get nothing in the search, why?
    – Hail Hydra
    Jan 22, 2014 at 19:24
  • with get, the search term will not be in $this->request->data. It should be in $this->params You can use debug($this->params) and see if that works
    – cornelb
    Jan 22, 2014 at 19:26
  • Ok, thank you very much for your help, and maybe im asking simple things but im new and im a little lost on some things.
    – Hail Hydra
    Jan 22, 2014 at 19:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.