select option as parameter for codeigniter method

Asked

Viewed 353 times

0

I’m having a problem I can’t seem to solve at all. I have an html form that has a select option to take the selected value when the form is sent and use as parameter for a method, there is some way to do this?

  • 1

    You could put in your code so we can help you?

1 answer

2

There is, my friend. Assuming that the name of the combobox is consulta would look like this in the controller:

public funcition Consultar(){
$parametro = $this->input->post('consulta'); //formulário submetido via POST

/*lembrando que o valor que virá submetido pelo formulário 
é o que se encontra em value lá no html */

$this->load->model('Nome_da_tua_model');
$res = $this->Nome_da_tua_model->Teu_metodo_de_consulta($parametro);
}

Already in your model would look like this:

public function Teu_metodo_de_consulta($parametro){
$this->db->Select('*');
$this->db->From('Tua_tabela');
if(!empty($parametro)){
$this->db->where('Tua_tabela.Teu_campo', $parametro);
}
$result = $this->db->get()->Result();
return $result;
} 

Now in the controller the variable $res has the data of your query.

The command SQL which I executed in the above section, is for equal values, if another type of comparison is required, I advise you to read the documentation in the link.

I hope I helped, Oss!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.