[eluser]CroNiX[/eluser]
I generally do this:
Code:
function get_projects()
{
$data = array();
$query = $this->db->get('projects')->result();
//form helper for dropdown requires array of id=>value
$data[0] = 'All Projects'; //add default first option
foreach($query as $q)
{
$data[$q->id] = $q->proj_name;
}
return $data;
}
Generally I add a default first option with an ID of 0 (like you did), since most columns with an id are autoincremented and start at 1, so you would never have a choice with 0.
Then in your form for the dropdown, you can:
Code:
<p><?php echo form_dropdown('my_select', $project_menu, 0); ?></p>
add the default of 0 as the selected value in the dropdown.
[/code]
But if you want to make this reusable (since all form dropdowns require this format), make a helper:
Code:
function get_form_dropdown($table, $id_field, $value_field, $first_option = '')
{
$CI =& get_instance();
$values = $CI->db
->select($id_field)
->select($value_field)
->get($table)
->result();
$data = array();
//create the first option if one was supplied
if( ! empty($first_option))
{
$data[0] = $first_option;
}
foreach($values as $value)
{
$data[$value->$id_field] = $value->$value_field;
}
return $data;
}
Then just load your helper in your controller and:
Code:
$data['project_menu'] = get_form_dropdown('projects', 'id', 'proj_name', 'All Projects');
And in a different project which uses a dropdown:
Code:
$data['project_menu'] = get_form_dropdown('users', 'userid', 'username', 'Select a User');
...or whatever