Say I have a select tag on my edit user form. It contains a list of schools a user can be assigned to.
<select 'school'>
<option value='1'>School 1</option>
<option value='2'>School 2</option>
</school>
created via: echo form_dropdown('school', $school_list)
If the user I'm editing already has a school assigned to him, I want the dropdown to default to that.
So, I did this:
$school = $this->db->get_where('schools', $filters) # result object from database
echo form_dropdown('school', $school_list, $school->id);
The problem is, of course, this throws an error when the user isn't assigned a school yet, and $school->id is empty.
So my question is; What is the best way to solve a problem like this in codeigniter?
One way, I could think of to solve this, is by returning NULL on $school->id. So,
empty($school->id) && ($school->id = NULL);
echo form_dropdown('school', $school_list, $school->id);
But, it rather seems inelegant and, although, I haven't tested it. I don't think it will work well when I want to repopulate the form if, in case, it was submitted with errors.
echo form_dropdown('school', $school_list, set_value('school', $school->id));
Anyone have a better solution?
EDIT: Please note that this isn't the actual code I use. I just typed this in right now, so if there are syntax errors or whatnot, my apologies.