peppericious Posted March 8, 2012 Share Posted March 8, 2012 Some forum users here gave me great help yesterday in working with dropdown menus. In fact, I need quite a few of these dropdown menus in several forms over several pages, so I created several simple arrays and made a function to create the dropdowns. Here's one of my arrays: <?php $instruments = array( 'Bassoon', 'Cello', 'Clarinet', 'Double Bass', 'Flute', 'French Horn', 'Oboe', 'Percussion', 'Trombone', 'Trumpet', 'Tuba', 'Viola', 'Violin', 'Other' ); Here's my function: <?php function create_dropdown($array_name, $array_item) { echo "<select name='$array_item'>\n"; echo "<option value='select'>Select…</option>\n"; foreach( $array_name as $v ) { echo "<option value='$v'>" . $v . "</option>\n"; } echo "</select>\n"; } ... and anywhere I need a dropdown menu, I'm calling it like this: <?php create_dropdown($instruments, 'instrument'); The dropdown menus, however (in some fairly detailed forms for my local youth orchestra's site), need to be sticky. How can I modify my function, above, so that the selected value will be retained if there are other form submission errors when the form is submitted? I have tried endlessly today but to no avail... If you can help while I still have some hair left, I'd be greatly appreciative. Quote Link to comment Share on other sites More sharing options...
Anon-e-mouse Posted March 8, 2012 Share Posted March 8, 2012 While you foreach through the values of the select menu itself you could add a third variable the function doesn't have to expect which is your "sticky variable"? So whenever you call it and you need to stick something to the menu you would simply put that field into the function. Something like: <?php function create_dropdown($array_name, $array_item, $sticky_item = null) { echo "<select name='$array_item'>\n"; echo "<option value='select'>Select…</option>\n"; foreach( $array_name as $v ) { if($v == $sticky_item){ echo "<option value='$v' selected>" . $v . "</option>\n"; } else { echo "<option value='$v'>" . $v . "</option>\n"; } } echo "</select>\n"; } See what I'm getting at? Quote Link to comment Share on other sites More sharing options...
Psycho Posted March 8, 2012 Share Posted March 8, 2012 Functions should not echo content, they should return content (which you can then echo). Also, I would advise NOT creating the opening/closing select tags with the function - just create the options. The reason is if you want to add style/class/event handlers to the select statement you have to make the function very complex. Also, it is quite typical that the VALUE of an option is not exactly the same as the LABEL. Therefore, I create these types of functions so I can pass an array with the values as the keys of the array and the values as the labels. Or use the values for both. Here is what I would use for the function function create_options($valuesAry, $useKeysAsValues=false, $selectedValue=false) { $optionsList = ''; foreach($valuesAry as $value => $label) { if(!$useKeysAsValues) { $value = $label; } $selected = ($selectedValue===$value) ? ' selected="selected"' : ''; $optionsList = "<option value='{$value}'>{$label}</option>\n"; } return $optionsList; } Usage: <select name='instrument'> <option value='select'>Select…</option> <?php echo create_options($instruments, false, $selectedInstrument); ?> </select> So, let's say you get a list of values from the database and you need to use the primary key as the value. You would set up your array such that the primary ID is the key and the values are, well, the values. Examples: //Don't use keys as values $instruments = array('Bassoon', 'Cello', 'Clarinet'); create_options($instruments, false); //Output: // <option value='Bassoon'>Bassoon</option> // <option value='Cello'>Cello</option> // <option value='Clarinet'>Clarinet</option> //Do use keys as values $instruments = array(5 => 'Bassoon', 8=>'Cello', 13=>'Clarinet'); create_options($instruments, true); //Output: // <option value='5'>Bassoon</option> // <option value='8'>Cello</option> // <option value='13'>Clarinet</option> Quote Link to comment Share on other sites More sharing options...
peppericious Posted March 8, 2012 Author Share Posted March 8, 2012 Thank you both for your help. Psycho, that's a super solution. I'm going to work on it right now. Thanks, too, for your pointers about not echoing with the function. Noted. Quote Link to comment Share on other sites More sharing options...
peppericious Posted March 8, 2012 Author Share Posted March 8, 2012 I'm running into a problem.... Using the function as in this example: //Don't use keys as values $instruments = array('Bassoon', 'Cello', 'Clarinet'); create_options($instruments, false); //Output: // <option value='Bassoon'>Bassoon</option> // <option value='Cello'>Cello</option> // <option value='Clarinet'>Clarinet</option> ... only the last item in the array is being displayed in the menu. Do I need to amend the function somehow?... TIA Quote Link to comment Share on other sites More sharing options...
Psycho Posted March 8, 2012 Share Posted March 8, 2012 Per my signature I do not always test the code I provide Change $optionsList = "<option value='{$value}'>{$label}</option>\n"; To $optionsList .= "<option value='{$value}'>{$label}</option>\n"; Note the '.=' (dot equal) which appends the string to the current value of the variable Quote Link to comment Share on other sites More sharing options...
peppericious Posted March 8, 2012 Author Share Posted March 8, 2012 Change $optionsList = "<option value='{$value}'>{$label}</option>\n"; To $optionsList .= "<option value='{$value}'>{$label}</option>\n"; Note the '.=' (dot equal) which appends the string to the current value of the variable Thank you... However, the menu selection is not being retained on submission of the form... :0( Quote Link to comment Share on other sites More sharing options...
Psycho Posted March 9, 2012 Share Posted March 9, 2012 Forgot to include the $selected variable in the output $optionsList .= "<option value='{$value}'{$selected}>{$label}</option>\n"; Quote Link to comment Share on other sites More sharing options...
peppericious Posted March 9, 2012 Author Share Posted March 9, 2012 Forgot to include the $selected variable in the output $optionsList .= "<option value='{$value}'{$selected}>{$label}</option>\n"; Thank you very much for your help. Much appreciated. 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.