dmhall0 Posted December 9, 2011 Share Posted December 9, 2011 I have a function that creates a dropdown list in a form. The User saves their answer to the mysql table. How do I get their preselected answer back out of the table when they visit the form again? I know how to pull it from mysql and into a variable, but how to modify the below code to display the correct answer stumps me. Here is my dropdown function: function dropdown($array, $id) { echo '<select name="'.$id.'" id="'.$id.'" class="select"><option value="">Select one...</option>'; foreach($array as $key => $value) { echo '<option value="'.$value.'">'.$value.'</option>'; } echo '</select>'; } Thanks for the help! Quote Link to comment Share on other sites More sharing options...
scootstah Posted December 9, 2011 Share Posted December 9, 2011 Try this: function dropdown($array, $id, $selected = '') { echo '<select name="'.$id.'" id="'.$id.'" class="select"><option value="">Select one...</option>'; foreach($array as $key => $value) { echo '<option value="'.$value.'" . '($value == $selected) ? 'selected="selected"' : '') . '>'.$value.'</option>'; } echo '</select>'; } Just pass the value from the database as a third parameter to dropdown(). Quote Link to comment Share on other sites More sharing options...
dmhall0 Posted December 9, 2011 Author Share Posted December 9, 2011 There is an error in the code, something about the (). Ideas? Quote Link to comment Share on other sites More sharing options...
scootstah Posted December 9, 2011 Share Posted December 9, 2011 Whoops, typo. Here you go: function dropdown($array, $id, $selected = '') { echo '<select name="'.$id.'" id="'.$id.'" class="select"><option value="">Select one...</option>'; foreach($array as $key => $value) { echo '<option value="'.$value.'" ' . ($value == $selected) ? 'selected="selected"' : '') . '>'.$value.'</option>'; } echo '</select>'; } Quote Link to comment Share on other sites More sharing options...
Psycho Posted December 9, 2011 Share Posted December 9, 2011 Personally, I prefer the define a $selected variable. Just looks cleaner IMHO. Also, in many cases the actual 'value' of a select list options will be different than the 'label'. So, I create my functions with a parameter on whether to use the keys of the array or not. If so, the keys are used as the value and the values as the labels May seem backwards, but makes sense logically. Values are routinely numeric IDs in the database so using the keys of the array makes sense. Lastly, it is poor implementation to have your functions actually echo content. Instead the function should return the content to be displayed. Anyway, here is a possible solution for you. function dropdown($id, $optionsAry, $use_keys=false, $selected_value=false) { $selectHTML = "<select name='{$id}' id='{$id}' class='select'>\n"; $selectHTML .= "<option value=''>Select one...</option>\n"; foreach($optionsAry as $value => $label) { if(!$use_keys) { $value = $label; } $selected = ($value===$selected_value) ? " selected='selected'": ''; $selectHTML .= "<option value='{$value}'{$selected}>{$label}</option>\n"; } $selectHTML .= "</select>\n"; return $selectHTML; } Quote Link to comment Share on other sites More sharing options...
scootstah Posted December 9, 2011 Share Posted December 9, 2011 In my opinion you are over-complicating this function. All it should be concerned with is if value == selected. What the value of selected is should be pre-determined; whether it be array keys or not. Quote Link to comment Share on other sites More sharing options...
dmhall0 Posted December 9, 2011 Author Share Posted December 9, 2011 Whoops, typo. Here you go: function dropdown($array, $id, $selected = '') { echo '<select name="'.$id.'" id="'.$id.'" class="select"><option value="">Select one...</option>'; foreach($array as $key => $value) { echo '<option value="'.$value.'" ' . ($value == $selected) ? 'selected="selected"' : '') . '>'.$value.'</option>'; } echo '</select>'; } I tried this and it just returned the "Select one..." option, with nothing in the dropdown. Quote Link to comment Share on other sites More sharing options...
scootstah Posted December 9, 2011 Share Posted December 9, 2011 Whoops, typo. Here you go: function dropdown($array, $id, $selected = '') { echo '<select name="'.$id.'" id="'.$id.'" class="select"><option value="">Select one...</option>'; foreach($array as $key => $value) { echo '<option value="'.$value.'" ' . ($value == $selected) ? 'selected="selected"' : '') . '>'.$value.'</option>'; } echo '</select>'; } I tried this and it just returned the "Select one..." option, with nothing in the dropdown. Sorry, actually tested this time: function dropdown($array, $id, $selected = '') { echo '<select name="'.$id.'" id="'.$id.'" class="select"><option value="">Select one...</option>'; foreach($array as $key => $value) { echo '<option value="'.$value.'" ' . ($value == $selected ? 'selected="selected"' : '') . '>'.$value.'</option>'; } echo '</select>'; } Quote Link to comment Share on other sites More sharing options...
dmhall0 Posted December 9, 2011 Author Share Posted December 9, 2011 That got it... THANKS!!! mjdamato - Yours is very interesting and might be applicable in another situation for me... THANKS also!! Quote Link to comment Share on other sites More sharing options...
Psycho Posted December 9, 2011 Share Posted December 9, 2011 In my opinion you are over-complicating this function. All it should be concerned with is if value == selected. What the value of selected is should be pre-determined; whether it be array keys or not. For the purposes of whether the option will be selected or not - that is exactly what that function does. I was simply providing some additional functionality into the function so it is more flexible and can be re-purposed as needed. By the way, there is a bug in the last script you provided. It would only be an issue based on specific values existing in the list of options and may not be experienced for the OPs implementation. But, the bug is there nonetheless. Quote Link to comment Share on other sites More sharing options...
scootstah Posted December 9, 2011 Share Posted December 9, 2011 But you are deciding which to use inside the function, which isn't necessary. You are giving the function too much responsibility. All it needs to do is compare the value. For example if this was our data: $names = array( 0 => 'john', 1 => 'debbie', 2 => 'matt' ); You are determining whether to use the the value or the array key inside the function. In my opinion you should determine this ahead of time. foreach($names as $key=>$val) { if ($id == $key) $selected = $key; } And then just pass $selected to the dropdown function. In the end either way works, just preference I guess. Quote Link to comment Share on other sites More sharing options...
Psycho Posted December 9, 2011 Share Posted December 9, 2011 The "purpose" of the switch for determining to use the key as the value has nothing to do with the process for determining the selected option. The purpose of using the key of the array or not is due to the fact that some select lists will have the same value as the label. I.e. <select> <option value="Red">Red</option> <option value="Green">Green</option> <option value="Blue">Blue</option> </select> While other select lists will have different values than the labels <select> <option value="1">Red</option> <option value="2">Green</option> <option value="3">Blue</option> </select> The latter is very common when dealing with assignments to DB records. So, the function I provided was an example of a function that one could use to meet the needs of both types of selects lists. $colors = (1 => 'Red', 2 => 'Green', 3 => 'Blue'); //Create select list with same data for the value and label echo dropdown('colors', $colors, true); //Create select list with IDs as values and description as label echo dropdown('colors', $colors, false); Again, the purpose of this option had nothing to do with the functionality for determining the selected value. It was about giving greater flexibility to the function for creating select lists. The ONLY line I provided for determining the selected value was this $selected = ($value===$selected_value) ? " selected='selected'": ''; And, I guess you still haven't found the flaw in the code you provided. Quote Link to comment Share on other sites More sharing options...
scootstah Posted December 9, 2011 Share Posted December 9, 2011 I see now that you are talking about the part of the function that actually generates the options whereas I was simply talking about comparing to a selected value. So then I concede, you win this round. 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.