CodeIgniter Forums
set_select problem in setting default values from db - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forum-23.html)
+--- Thread: set_select problem in setting default values from db (/thread-15379.html)



set_select problem in setting default values from db - El Forum - 02-02-2009

[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.


set_select problem in setting default values from db - El Forum - 02-02-2009

[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


set_select problem in setting default values from db - El Forum - 02-02-2009

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


set_select problem in setting default values from db - El Forum - 02-03-2009

[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