在php foreach中选择下拉值

时间:2012-09-08 13:04:10

标签: php html

我有一个用PHP生成的下拉列表。

<select name="diploma" id="diploma-<? echo $edu['id']; ?>">
                    <? foreach($diplomas->result_array() as $diploma) {
          echo "<option    value=".$diploma['id'].">".$diploma['name']."</option>";
                            } ?>
</select>

我还有<? echo $edu['diploma']; ?>这是数据库中的值。我想将selected="selected"添加到与该值匹配的下拉列表中的选项。我应该只使用jquery吗?最好的方法是什么?

5 个答案:

答案 0 :(得分:1)

您可以像这样检查循环内部:

<select name="diploma" id="diploma-<? echo $edu['id']; ?>">
    <? 
    foreach($diplomas->result_array() as $diploma) 
        {
            if($edu['diploma']==$diploma['name'])
            {
                echo "<option selected ='selected' value=".$diploma['id'].">".$diploma['name']."</option>";
            }
            else
            {
                echo "<option value=".$diploma['id'].">".$diploma['name']."</option>";
            }
        } 
    ?>
</select>

或者缩短代码:

<select name="diploma" id="diploma-<? echo $edu['id']; ?>">
    <? 
    foreach($diplomas->result_array() as $diploma) 
        {
            $selected = ($edu['diploma']==$diploma['name']) ? " selected='selected' " : "" ;
            echo "<option $selected value=".$diploma['id'].">".$diploma['name']."</option>";
        } 
    ?>
</select>

答案 1 :(得分:1)

使用相同的PHP代码:

<select name="diploma" id="diploma-<? echo $edu['id']; ?>">
    <? foreach($diplomas->result_array() as $diploma) {
        echo "<option value=".$diploma['id'];
        if ($edu['diploma'] == $diploma['name']) {
            echo 'selected="selected"';
        }
        echo ">".$diploma['name']."</option>";
    } ?>
</select>

代码会针对每个选项将$diploma['name']$edu['diploma']变量进行比较,如果它们相同则输出selected="selected"

根据$edu['diploma']的内容,您需要将其与一件事或另一件事进行比较。如果$edu['diploma']包含id更改$diploma['name']$diploma['id']

如果$edu['diploma']包含文凭对象本身,则需要以这种方式进行比较:$edu['diploma']['id'] == $diploma['id']

答案 2 :(得分:1)

这就是我提出的:

<select name="diploma" id="diploma-<? echo $edu['id']; ?>">
    <?php foreach ($diplomas->result_array() as $diploma) {
    //echo "<option value='{$diploma['id']}'>{$diploma['name']}</option>";
    echo "<option value='{$diploma['id']}";
    if ($edu['diploma'] === $diploma['name']) {
        echo " selected";
    }
    echo ">{$diploma['name']}</option>\n";
} ?>
</select>

答案 3 :(得分:0)

<select name="diploma" id="diploma-<? echo $edu['id']; ?>">
   <?php foreach($diplomas->result_array() as $diploma) {
      $selected = $diploma['id'] == $edu['diploma'] ? ' selected="selected"' : ''; 
      echo '<option value="' . $diploma['id'] . '"' . $selected . '>' . $diploma['name'] . '</option>';
     } ?>
</select>

在循环内部进行检查,并在值匹配时回显所选属性。注意,我在测试中使用ternary statement

答案 4 :(得分:0)

如果我找对你,试试这个;

<select name="diploma" id="diploma-<? echo $edu['id']; ?>">
<?
$selected = $edu['diploma'];
foreach($diplomas->result_array() as $diploma) {
  if($diploma['name'] == $selected){
    echo '<option selected="selected" value="'.$diploma['id'].'">'.$diploma['name']."</option>";
  }else{
    echo '<option value="'.$diploma['id'].'">'.$diploma['name']."</option>";
  }
}
?>
</select>