CodeIgniter Forums

Full Version: set_select problem in setting default values from db
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

El Forum

[eluser]Jygz V[/eluser]
Hi,

Good day.

I am new to CI, I was confused on how to set default values of a dropdown using the "set_select".

This is the scenario.

I have a page with the following dropdown:
<select name='gender'>
<option name='male'>Male</option>
<option name='female'>Female</option>
</select>

And when this page is loaded, I get the gender from the database based from the user's profile. The gender drop down will be set to default depending on what gender is retrieved from the database. Can anybody show me how to do that using "set_select"?

Thanks.

El Forum

[eluser]xzela[/eluser]
Hi,

This is what I do to determine which select field to use. It may not be the 'proper' way to do it, however it works for me.

Code:
<select name='gender'>
    &lt;?php if(set_value('gender') == 'Male'):?&gt;
        <option value='male' selected >Male</option>
        <option value='female'>Female</option>
    &lt;?php else: ?&gt;
        <option value='male'>Male</option>
        <option value='female' selected>Female</option>
    &lt;?php endif;?&gt;
</select>

This probably isn't the best way to do it now that i think about it. I typically have a foreach loop to loop through all the options and use an if statement to determine the default selection.

I don't know, just a suggestion... Smile

El Forum

[eluser]Jygz V[/eluser]
Thanks for your response. My default value came from the database, how will I do that? Thanks.

El Forum

[eluser]Future Webs[/eluser]
this comes up all the time

http://ellislab.com/forums/viewthread/97495/

thats a post where i had the same problems