katie77 Posted November 28, 2007 Share Posted November 28, 2007 Ok, I've got a dropdown list that is acting as a filter on a table, so the user can select a value and the page is reloaded only showing the matching values from the database. Now when the page reloads the dropdown was set to 'all' as it was first in the list, even if I'd selected a different value, this was confusing for users, so I added an initial <option> field which pulled the correct value from $_GET (code below) - however this is a little odd as it means the value appears twice! Does anyone have a genius solution to this? I've seen a <select selected="yes"> sort of thing around, but I don't know how I can tell the php to add this value dynamically? ??? <select size='1' name='occupation'> <option>".$_GET['occupation']."</option> <option value='All'>All</option> <option value='Student'>Students</a></option> <option value='Professional'>Professional</option> <option value='Hobbyist'>Hobbyists</option> </select> Quote Link to comment Share on other sites More sharing options...
Stooney Posted November 28, 2007 Share Posted November 28, 2007 I would suggest putting all of the options into an array. When an option is selected, re-arrange the array so that the selected option is first. Then just use this array for the options in the dropdown menu. Quote Link to comment Share on other sites More sharing options...
rlindauer Posted November 28, 2007 Share Posted November 28, 2007 You can always add selected="selected" into the option to make it the default value in the menu. <option value="value" selected="selected">Option</option> Quote Link to comment Share on other sites More sharing options...
jcd Posted November 28, 2007 Share Posted November 28, 2007 I've seen a <select selected="yes"> sort of thing around, but I don't know how I can tell the php to add this value dynamically? ??? Here is one way you could implement the suggestions above. It's only for demonstration purposes, I would put the array near the top of the actual document. <select size='1' name='occupation'> <?php $occupation_list = array('All','Acrobat','Clown','Juggler','Sword Swallower','Wrestler'); $occupation = $_GET['occupation']; foreach ($occupation_list as $occ) { echo "<option value=\"$occ\""; if ($occ == $occupation) { echo ' selected="selected"'; } echo ">$occ</option>"; } ?> </select> Quote Link to comment Share on other sites More sharing options...
Wes1890 Posted November 28, 2007 Share Posted November 28, 2007 Learn to use the search feature, either here or at google.com.. it will make things easier for everyone http://www.phpfreaks.com/forums/index.php/topic,169667.0.html Quote Link to comment Share on other sites More sharing options...
rarebit Posted November 29, 2007 Share Posted November 29, 2007 This is a simple way that doesn't allow for different labels and values... but this'll get you on your way... function helpers_form_select_s($name, $values, $selected) { $sret = "<select name='".$name."'>"; foreach($values as $v) { if (strcmp($v, $selected) == 0) { $sret .= "<option selected>".$v; } else { $sret .= "<option>".$v; } } $sret .= "</select>"; return $sret; } Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.