CodeIgniter Forums
Set select to post-value else use given value - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Set select to post-value else use given value (/showthread.php?tid=24252)



Set select to post-value else use given value - El Forum - 11-04-2009

[eluser]Grind[/eluser]
Of a <select> menu, how can display the menu-item that was selected according to the post value and - if the post value isn't set - to a given value? Just like you can do with text fields:
Code:
&lt;input type="text" name="username" value="&lt;?=set_value('username', $userinfo['username'])?&gt;" /&gt;

Is my question clear enough? Smile

Thanks in advance.


Set select to post-value else use given value - El Forum - 11-04-2009

[eluser]rogierb[/eluser]
Using set_select() like the almighty user guide says?


Set select to post-value else use given value - El Forum - 11-04-2009

[eluser]Grind[/eluser]
I tried, but the third boolean parameter doesn't work for me, because the default selected option is a variable that comes out of a database.


Set select to post-value else use given value - El Forum - 11-04-2009

[eluser]Grind[/eluser]
The user guide says this:
Code:
<select name="myselect">
<option value="one" &lt;?php echo set_select('myselect', 'one', TRUE); ?&gt; >One</option>
<option value="two" &lt;?php echo set_select('myselect', 'two'); ?&gt; >Two</option>
<option value="three" &lt;?php echo set_select('myselect', 'three'); ?&gt; >Three</option>
</select>

That would be fine, but in my case all the data is selected out of a database:

Code:
$options = array('one', 'two', 'three');
$default_value = 'one';

echo '<select name="myselect">';
foreach($options as $value)
{
   echo '<option value="'.$value.'" '.set_select('myselect', $value).'>'.$value.'</option>';
}
echo '</select>';

In this case, how can I select the option that's equal to $default_value if no form is submitted? If form submitted, the submitted option must be selected.
Hope this makes it a little bit clearer.


Set select to post-value else use given value - El Forum - 11-04-2009

[eluser]mattpointblank[/eluser]
In these situations, I just drop set_select and do something like:

Code:
<option value="123"&lt;?php if($db_value == 123) { echo ' selected="selected"'; } ?&gt;>Test</option>

Oldschool.


Set select to post-value else use given value - El Forum - 11-04-2009

[eluser]Grind[/eluser]
Very oldschool Wink
And how do you select the option according to a submitted form? Because in my case the value form the post array must be selected, but if no form is submitted the database value must be selected.


Set select to post-value else use given value - El Forum - 11-04-2009

[eluser]bigtony[/eluser]
The third parameter can be anything that resolves to a boolean TRUE or FALSE, so it could be (using your example code):
Code:
$options = array('one', 'two', 'three');
$default_value = 'one';

echo '<select name="myselect">';
foreach($options as $value)
{
   echo '<option value="'.$value.'" '.set_select('myselect', $value, ($value == $default_value)).'>'.$value.'</option>';
}
echo '</select>';
Just do something similar with your db field instead by comparing the db field value to the current loop value.


Set select to post-value else use given value - El Forum - 11-04-2009

[eluser]Grind[/eluser]
Of course! Thank you!