Welcome Guest, Not a member yet? Register   Sign In
Filling data in a dropdown from the db
#1

[eluser]Guillem[/eluser]
I am trying to fill a dropdown in a form with some data in my db.

I get the id and the name of the elements in the table I want to show in my dropdown and I pass them to the view:
Code:
$this->db->select('id, nombre');
$data['operadoras'] = $this->db->get('operadoras');
Then, I would like to create an array with id and nombre, but I cannot figure it up how. I tried something like that
Code:
array(
foreach ($operadoras->array_result() as $row):
$row->id => $row->nombre,
endforeach;);
but definitely it's not working.

Is there any function in the helpers of CI to make what I want to? If not, how can I make it?

Thank you all
#2

[eluser]Michael Wales[/eluser]
Code:
$this->db->select('id, nombre');
$query = $this->db->get('operadors');

foreach ($query->result() as $row) {
  $data['operadoras'][$row->id] = $row->nobre;
}

In view:
Code:
<select name="yomomma">
&lt;?php
  foreach ($data['operadoras'] as $id => $nombre) {
    ?&gt;
    <option value="&lt;?= $id; ?&gt;">&lt;?= $nombre; ?&gt;</option>
    &lt;?
  }
?&gt;
</select>
#3

[eluser]Guillem[/eluser]
That's what I am using now, but I'd like to know if I could use the helper since it seems nicer in the code (not another reason!).

Anyway, thanks walesmd
#4

[eluser]Michael Wales[/eluser]
I believe this would work, in the view:
Code:
&lt;?= form_dropdown('yomomma', $data['operadoras']); ?&gt;
#5

[eluser]Sean Murphy[/eluser]
If you want to use the form helper, you can change the view to use:
Code:
&lt;?=form_dropdown('operadoras', $data['operadoras']);?&gt;
#6

[eluser]Guillem[/eluser]
Definitely it works.

Thank you all folks Smile
#7

[eluser]Bharani[/eluser]
dear i used your code..
but i am getting error..
shalli send the code




Theme © iAndrew 2016 - Forum software by © MyBB