我正在尝试进行选择,这将对我的“编辑用户”页面有帮助。 我有这段代码,但是我不知道如何更改它,因此我可以加载从数据库中选择的值。 这段代码正在起作用...
<div class="form-group">
<th><label for="text">Tipo Sanguineo:</label></th>
<td><select class="form-control" id="sangue" name="tipo_sangue">
<option value="O-" <?php if ($cl_tipo_sangue == "O-") echo 'selected';?> >O-</option>
<option value="O+" <?php if ($cl_tipo_sangue == "O+") echo 'selected';?> >O+</option>
<option value="A-" <?php if ($cl_tipo_sangue == "A-") echo 'selected';?> >A-</option>
<option value="A+" <?php if ($cl_tipo_sangue == "A+") echo 'selected';?> >A+</option>
<option value="B-" <?php if ($cl_tipo_sangue == "B-") echo 'selected';?> >B-</option>
<option value="B+" <?php if ($cl_tipo_sangue == "B+") echo 'selected';?> >B+</option>
<option value="AB-" <?php if ($cl_tipo_sangue == "AB-") echo 'selected';?> >AB-</option>
<option value="AB+" <?php if ($cl_tipo_sangue == "AB+") echo 'selected';?> >AB+</option>
</select></td>
</div>
我需要使用以下代码执行此操作:
<?php $dts = DBRead11(); ?>
<select name="diretor_turma" id="diretor_turma" required class="input-field4">
<?php
foreach($dts as $option) {
?>
<option value="<?php echo $option['nome']; ?>"><?php echo $option['nome']; ?></option>
<?php
}
?>
</select>
我需要更改的代码是将值从数据库加载到站点,而且我不确定如果从数据库中加载选定的值,怎么看。
答案 0 :(得分:0)
尝试一下
<?php $dts = DBRead11(); ?>
<?php $cl_tipo_sangue = "B+"; ?>// here you need to add db value which you saved in DB
<select name="diretor_turma" id="diretor_turma" required class="input-field4">
<?php
foreach($dts as $option) {
?>
<option value="<?php echo $option['nome']; ?>" <?php if ($cl_tipo_sangue == $option['nome']) { echo 'selected="selected"'; } ?> ><?php echo $option['nome']; ?></option>
<?php
}
?>
</select>
答案 1 :(得分:0)
步骤:
1)获取一个数组,以保存下拉选项的ID名称对。
2)在<select>
标签中的<option>
上对其进行循环。
3)检查所选ID是否为当前迭代ID。
4)如果是,则将属性selected="selected"
添加到当前的<option>
然后在循环中检查是否已选择一个。
<?php
$bloodGroups = [
'O-' => 'O-',
'O+' => 'O+',
'A-' => 'A-',
'A+' => 'A+',
'B-' => 'B-',
'B+' => 'B+',
'AB-' => 'AB-',
'AB+' => 'AB+'
];
?>
<div class="form-group">
<th><label for="text">Tipo Sanguineo:</label></th>
<td><select class="form-control" id="sangue" name="tipo_sangue">
<?php
if (! empty($bloodGroups)) {
foreach ($bloodGroups as $bgid => $bloodGroup) {
$selected = ($cl_tipo_sangue == $bgid) ? 'selected="selected"' : '';
?>
<option value="<?php echo $bgid;?>" <?php echo $selected;?>><?php echo $bloodGroup;?></option>
<?php
}
}
?>
</select></td>
</div>
答案 2 :(得分:0)
使用javascript
<?php $dts = DBRead11(); ?>
<select name="diretor_turma" id="diretor_turma" required class="input-field4">
<?php
foreach($dts as $option) {
?>
<option value="<?php echo $option['nome']; ?>"><?php echo $option['nome']; ?></option>
<?php
}
?>
</select>
<script>
document.querySelector('#diretor_turma').value = '<?= $cl_tipo_sangue ?>'
</script>
答案 3 :(得分:0)
然后您需要获取保存在数据库中的值
<?php
$nome = $database_value;
?>
<select name="diretor_turma" id="diretor_turma" required class="input-field4">
<?php
foreach($dts as $option) {
?>
<option value="<?php echo $option['nome']; ?>" <?php echo ($option['nome'] == $nome)?'selected':'' ?>><?php echo $option['nome']; ?></option>
<?php
}
?>
</select>
答案 4 :(得分:0)
<select name="diretor_turma" id="diretor_turma" required class="input-field4">
<?php
foreach($dts as $option) {
$select = (isset($cl_tipo_sangue) && $cl_tipo_sangue == $option['nome'])?"selected = 'selected'":"";?>
<option value="<?php echo $option['nome']; ?>" <?php echo $select; ?>><?php echo $option['nome']; ?></option>
<?php } ?>
</select>