spertuit Posted June 27, 2013 Share Posted June 27, 2013 I have a function that builds an array an works great. Form <?php $name = 'logWeather'; $options = array( 'Fair', 'Windy', 'Rainy','Stormy'); $selected = $logWeather; echo dropdown( $name, $options, $selected ); ?> Function <?php function dropdown( $name, array $options, $selected) { /*** begin the select ***/ $dropdown = '<select name="'.$name.'" id="'.$name.'">'."\n"; $selected = $selected; /*** loop over the options ***/ foreach( $options as $key=>$option ) { /*** assign a selected value ***/ $select = $selected==$key ? ' selected' : null; /*** add each option to the dropdown ***/ $dropdown .= '<option value="'.$key.'"'.$select.'>'.$option.'</option>'."\n"; } /*** close the select ***/ $dropdown .= '</select>'."\n"; /*** and return the completed dropdown ***/ return $dropdown; } ?> This works perfectly to edit a dropdown built from an array, but I am trying to add in a little more functionality. How can I add in a hidden value and a displayed value ex in html <select name="logSeaCondition"> <option value="1">0-1 feet</option> <option value="4">2-4 feet</option> <option value="7">5-7 feet</option> <option value="10">8-10 feet</option> <option value="13">11-13 feet</option> <option value="16">14-16 feet</option> <option value="19">17-19 feet</option> <option value="21">20+ feet</option> </select> Can I possibly pass in an associative array and use value as key and echo the item in the array? Quote Link to comment https://forums.phpfreaks.com/topic/279641-dropdown-list-from-an-array/ Share on other sites More sharing options...
Solution AbraCadaver Posted June 27, 2013 Solution Share Posted June 27, 2013 It looks like your existing code works for this, just build your array like: $options = array( 1=>'0-1 feet', 4=>'2-4 feet' ); //etc Quote Link to comment https://forums.phpfreaks.com/topic/279641-dropdown-list-from-an-array/#findComment-1438261 Share on other sites More sharing options...
spertuit Posted June 27, 2013 Author Share Posted June 27, 2013 Geeze, all the things I tried and I didn't even think about it. Thanks a million! Working code to pass in an associative array into a generated dropdown box and echoing stored selection using three parameters. $name is the name of the drop down to retrieve your post variable, $options is your array, and $selected is your echoed variable from the database or null if it is a new form. Form Side $name = 'logSeaCondition'; $options = array(1=>'0-1 feet', 4=>'2-4 feet', 7=>'5-7 feet',10=>'8-10 feet',13=>'11-13 feet',16=>'14-16 feet',19=>'17-19 feet',20=>'20+ feet'); $selected = $logSeaCondition; echo dropdown( $name, $options, $selected ); Function function dropdown( $name, array $options, $selected) { /*** begin the select ***/ $dropdown = '<select name="'.$name.'" id="'.$name.'">'."\n"; $selected = $selected; /*** loop over the options ***/ foreach( $options as $key=>$option ) { /*** assign a selected value ***/ $select = $selected==$key ? ' selected' : null; /*** add each option to the dropdown ***/ $dropdown .= '<option value="'.$key.'"'.$select.'>'.$option.'</option>'."\n"; } /*** close the select ***/ $dropdown .= '</select>'."\n"; /*** and return the completed dropdown ***/ return $dropdown; } Quote Link to comment https://forums.phpfreaks.com/topic/279641-dropdown-list-from-an-array/#findComment-1438276 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.