Reputation: 473
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.
Upvotes: 1
Views: 663
Reputation: 1495
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.
Upvotes: 1
Reputation: 6066
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);
}
Upvotes: 0