1

This is based in PHP.

Is it possible to take an array like this:

$SELECT_INDUSTRY = array("Medical" => "Specialty", "Dental" => "Specialty", "Pediatrics" => "Specialty");

And have that pass those two values into something simple like this:

<select>
    <option value="$SELECT_INDUSTRY[]">$SELECT_INDUSTRY[]</option>
</select>

Where Medical would be the value being passed, and Specialty would be the public facing text.

This is the function I'm using to build the actual select boxes:

$SELECT_INDUSTRY = array("Medical", "Dental", "Pediatrics");
$FORM_SELECT_SIZE = 'input-min';
function get_options_industry( $arr = array() ) {
global $FORM_SELECT_SIZE;
echo '<div class="control-group"><label class="control-label" for="industry">Industry</label><div class="controls"><select name="industry" id="industry" class="'.$FORM_SELECT_SIZE.'"><option value=>Select an Industry</option>';
foreach( $arr as $option ) {
    echo '<option>'.$option.'</option>';
}
echo '</select></div></div>';
}
$FORM_FIELD_INDUSTRY = $SELECT_INDUSTRY;

And this is how I'm displaying the select:

<?php get_options_industry( $FORM_FIELD_INDUSTRY ) ?>

I feel like I'm close, or at least on the right track, just can't figure out how to pull the first array value into the value field, then the second into the actual name.

The goal is to get the value being passed in the form, is different than the public facing name. I realize that the above array definition I gave may not be the correct way to do this.

SOLUTION

Thanks to a couple responses below, here is the final answer to solve my problem:

$SELECT_INDUSTRY = array("Medical" => "Medical", "Dental" => "Medical", "Pediatrics" => "Medical");
$FORM_SELECT_SIZE = 'input-min';
function get_options_industry( $arr = array() ) {
global $FORM_SELECT_SIZE;
echo '<div class="control-group"><label class="control-label" for="industry">Industry</label><div class="controls"><select name="industry" id="industry" class="'.$FORM_SELECT_SIZE.'"><option value=>Select an Industry</option>';
foreach( $arr as $key => $value ) {
    echo '<option value="'.$value.'">'.$key.'</option>';
}
    echo '</select></div></div>';
}
$FORM_FIELD_INDUSTRY = $SELECT_INDUSTRY;

I then have to make sure that my key is unique so no duplicates are erased.

0

3 Answers 3

3

How about this?

$SELECT_INDUSTRY = array("Medical" => "Specialty", "Dental" => "Specialty", "Pediatrics" => "Specialty");

.... other stuff you already have ....

foreach( $arr as $val => $option ) {
    echo '<option value="'.$val.'">'.$option.'</option>';
}
Sign up to request clarification or add additional context in comments.

4 Comments

Let me give this a shot here.
Both this solution, and the one given above (or a combo of both) work for this solution.
Is that possible to pass multiple value through one option just like- 'echo '<option value="'. $option->item1 .' '. $option->item2 .'">'. $key .'</option>';' ? If not, then what should I do in this case..?
That works, if $option is in fact an object with those properties. Everything should work just fine.
1

Quick example...

$SELECT_INDUSTRY = array("Medical" => "Specialty", "Dental" => "Specialty", "Pediatrics" => "Specialty");

echo '<select>';    
foreach ($SELECT_INDUSTRY as $key => $value) {
    echo '<option value="' . $value . '">' . $key . '</option>';
}
echo '</select>';

You can reverse $key and $value depending on which you want displayed as the text and which you would like passed as the value. The way I displayed it in my example made the most sense to me.

5 Comments

Looks like for this example, and what was given by @matt, the key is only giving a unique output in the foreach, for example, if I have ("Medical" => "Dental", "Medical => "Optometry", "Specialty Medical" => "Pediatrics"), Medical is only displayed once, even though there are two in here.
You should use unique keys in your array. So reverse what you have.
Ahh okay, that makes sense. Let me give that a shot here.
Yes, that's how PHP arrays work. If you put in a second value for the same key "Medical" then it will overwrite the first value.
Nailed it! Posting up the solution in the original post.
0

This one works fine

$SELECT_INDUSTRY = array("Medical", "Specialty", "Dental", "Specialty", "Pediatrics", "Specialty");

echo '<select>';    
foreach ($SELECT_INDUSTRY as $key => $value) {
    echo '<option value="' . $value . '">' . $key . '</option>';
}
echo '</select>';

1 Comment

Welcome to Stack Overflow! Thank you for this code snippet, which may provide some immediate help. A proper explanation would greatly improve its educational value by showing why this is a good solution to the problem, and would make it more useful to future readers with similar, but not identical, questions. Please edit your answer to add explanation, and give an indication of what limitations and assumptions apply.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.