Jump to content

Extract Saved Dropdown Answer


dmhall0

Recommended Posts

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!

Link to comment
Share on other sites

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().

Link to comment
Share on other sites

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>';
}

Link to comment
Share on other sites

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;
}

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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>';
}

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.