I am trying to create a dropdown form where the name of a choice is shown to the user, but when they select it, the id of that choice is what is passed as input. Is this possible?
echo form_label('Category');
echo form_dropdown('category_id', $category_options,
$category[0]->category_name);
Right now this just shows the id of each option.
The reason I have this issue is that my model is taking in the id as that was the only way i could figure out to make it update properly, but the user won't really know what the id stands for and needs the name for a meaningful dropdown menu.
I am getting the ID and the name from the database using a method in my model.
I am new to codeigniter and any help would be appreciated!
Your $category_options array should be formatted as key => value (category_id => category_name) pair. For example your array from model should be like this:
where 1 and 2 are category ids and category1 and category2 are category names. The above code will output
For more reference check codeigniter form helper documentation : https://ellislab.com/codeigniter/user-guide/helpers/form_helper.html