这基于PHP。
是否可以采用这样的数组:
$SELECT_INDUSTRY = array("Medical" => "Specialty", "Dental" => "Specialty", "Pediatrics" => "Specialty");
并且将这两个值传递给像这样简单的东西:
<select>
<option value="$SELECT_INDUSTRY[]">$SELECT_INDUSTRY[]</option>
</select>
医疗将成为传递的价值,而专业将是面向公众的文本。
这是我用来构建实际选择框的函数:
$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;
这就是我显示选择的方式:
<?php get_options_industry( $FORM_FIELD_INDUSTRY ) ?>
我觉得我很接近,或者至少在正确的轨道上,只是无法弄清楚如何将第一个数组值拉入值字段,然后第二个进入实际名称。
目标是获取表单中传递的值,与面向公众的名称不同。我意识到我给出的上述数组定义可能不是正确的方法。
解
感谢下面的几个回复,这是解决我问题的最终答案:
$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;
然后我必须确保我的密钥是唯一的,因此不会删除重复项。
答案 0 :(得分:3)
这个怎么样?
$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>';
}
答案 1 :(得分:1)
快速举例......
$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>';
您可以反转$ key和$ value,具体取决于您希望显示为文本以及您希望作为值传递的内容。我在我的例子中展示它的方式对我来说最有意义。
答案 2 :(得分:0)
这个工作正常
$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>';