Search engine with 2 fields filter ajax codeigniter 3?

0

I have a search engine that filters by a variable but I can not find the way to perform the search but I do it with 2 variables at the same time.

This is my method that I use to filter by a single field "name_event" of the database "event", I need to filter at the same time for another field "type of event".

function mostrar($valor){
    $this->db->like("nombre_evento",$valor);
    $consulta = $this->db->get("evento");
    return $consulta->result();
}
    
asked by Javier Antonio Aguayo Aguilar 19.05.2016 в 06:29
source

2 answers

1

In codeigniter you can use like the number of times you want, to search for different variables for example you could do something like this:

$this->db->like('nombre_evento', $var1);
$this->db->like('tipo_evento', $var2);

If for example $var1= "prueba" and $var2 = "prueba1"

You would get a query like the following:

//WHERE 'title' LIKE '%prueba%' ESCAPE '!' AND  'body' LIKE '%prueba1% ESCAPE '!'

It is also possible to use associative arrays in the query, such as:

$comparaciones = array('nombre_evento'=>$valor_nombre,'tipo_evento'=>$tipo);
$this->db->like($comparaciones);

Extending this, you could simplify it to the point of using the names of the columns in the database as keys in the ajax. This way you could pass the post as a comparison variable.

function mostrar(){
    return $this->db->like($this->input->post())->get("evento");
}

Remember to allow only ajax requests with

$this->input->is_ajax_request()
    
answered by 19.05.2016 в 15:08
0

To pass more parameters to your query there are 2 ways (they are the ones I remember) The first one is like this:

$this->db->like('campotabla', $paramBuscar); 
$this->db->like('campotabla', $panamBuscar); 

The other is to create the separate query and concatenate the parameters to search.

$sql = "SELECT id, campo, campo FROM tabla WHERE campo LIKE '%".$this->db->escape_like_str($cadena)."%'";

Greetings !!

    
answered by 03.08.2017 в 00:02