<select class="form-control" name="role_edit" id="role_edit" required> <option value="role_val" selected>- Select -</option> <?php $sql = "SELECT DISTINCT role FROM user_info"; $query = $conn->query($sql); while($row2 = $query->fetch_assoc()){ if($row2['role'] == '1'){ $role_text = 'Term Leader'; }else if($row2['role'] == '0'){ $role_text = 'Term Member'; } echo "<option value='".$row2['role']."'>".$role_text."</option> "; } ?> </select>将该代码中下拉菜单文本默认的Select改为识别到的role_val值对应的role_text
时间: 2024-03-13 13:45:17 浏览: 90
可以使用以下代码将该下拉菜单中默认的"Select"文本替换为对应的"role_text":
```php
<select class="form-control" name="role_edit" id="role_edit" required>
<?php
$sql = "SELECT DISTINCT role FROM user_info";
$query = $conn->query($sql);
while($row2 = $query->fetch_assoc()){
if($row2['role'] == '1'){
$role_text = 'Term Leader';
}else if($row2['role'] == '0'){
$role_text = 'Term Member';
}
if ($row2['role'] == 'role_val') {
echo "<option value='".$row2['role']."' selected>".$role_text."</option>";
} else {
echo "<option value='".$row2['role']."'>".$role_text."</option>";
}
}
?>
</select>
```
在这个修改后的代码中,我们使用了一个 if 语句来检查当前的 role 值是否等于识别到的 role_val 值。如果是,我们就使用 `selected` 属性来设置该选项为默认选中项。否则,我们仅仅输出该选项而已。
阅读全文