传递数组值到一个表单中选择选项字段?

传递数组值到一个表单中选择选项字段?

问题描述:

这是基于在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.

这个怎么样?

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