Forum Moderators: coopster

Message Too Old, No Replies

Select fields have too many items

         

Sandd

1:24 pm on Feb 21, 2008 (gmt 0)

10+ Year Member



Submitted values are stored in MySQL but I'd like my form to show me the current value as well as show the other options available. So if the current value is 'Active' when I view the form again, I'd like the list to show the current value 'Active' and then the other 3 options.

With the code below, it shows 'Active', then 'Active' again followed by the other 3 items.

How can I get it to show just the current value, which can be any of the 4 options, then only show the current value and then only the other 3.

<select name='status'>
<option value=$status selected='selected'>$status</option>
<option value='Active'>Active</option>
<option value='Available'>Available</option>
<option value='Lost/Stolen'>Lost/Stolen</option>
<option value='Retired'>Retired</option>
</select>

Thanks!

whoisgregg

2:15 pm on Feb 21, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hello Sandd! Take a look at the code below and see if it helps. :)

$select_options = array(
'Active' => 'Active',
'Available' => 'Available',
'Lost/Stolen' => 'Lost/Stolen',
'Retired' => 'Retired'
);
echo '<select name="status">';
foreach($select_options as $value=>$label){
echo '<option value="'.$value.'"'.( ($value == $status)? ' selected="selected"' : '' ).'>'.$label.'</option>';
}
echo '</select>';

Sandd

2:29 pm on Feb 21, 2008 (gmt 0)

10+ Year Member



Thank you. Your code produces the select box and 4 options just fine, but it shows the current value and then all 4 options again. I'd like to see just the current value and the 3 remaining options. So the remaining options will be different depending on the current value.

whoisgregg

2:45 pm on Feb 21, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



// uses same $select_options array from before
echo '<select name="status">';
if(isset($select_options[$status])){
echo '<option value="'.$status.'" selected="selected">'.$select_options[$status].'</option>';
}
foreach($select_options as $value=>$label){
if($status != value){
echo '<option value="'.$value.'">'.$label.'</option>';
}
}
echo '</select>';