• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Result Set directly pass to dropdown

Hello There,

I need to know is there any way in Codeigniter in which we can pass database result set directly to form_dropdown function.

Please let me know, if there is any way to do this task.


foreach($query->result() as $item):
     $data[$item->name] = $item->value;

form_dropdown('name' ,$data);

Try this. Big Grin

No there isn't. You'd have to do what guzzilar suggested, or create a helper to do it since this is a very common thing and you will probably use it many times.

function array_to_dropdown($raw_data, $key_field, $value_field)
  $data = array();
  foreach($raw_data as $raw)
    $data[$raw[$key_field]] = $raw[$value_field];
  return $data;

$values = $this->db
  ->select('id, name, address, city, state_id')

$dropdown_values = array_to_dropdown($values, 'id', 'name');

echo form_dropdown('my_dropdown', $dropdown_values);

Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  

  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.