我正在尝试使用来自mysql查询中多列的数据填充html select下拉列表。
当我检索第一列时,它将填充下拉列表而不会出现问题。当我添加第二列或第三列时,出现问题。本来我是空白字段,可通过添加!empty()来解决。
但是我现在得到的是重复的值,而不是空格
<?php
$base = '';
$base2 = '';
$base3 = '';
while($row = mysqli_fetch_assoc($resultpmb)) {
$code=$row["id"];
$name=$row["gw_name"];
if (!empty(trim($row['vg_name']))){
$vgname=$row["vg_name"];}
if (!empty(trim($row['vm_name']))){
$vmname=$row["vm_name"];}
$base .= "<option value=" .$code.">".$name."</option>";
$base2 .= "<option value=" .$code.">".$vgname."</option>";
$base3 .= "<option value=" .$code.">".$vmname."</option>";
}
?>
<div class="form-group col-4 col-m-12">
<div class="input-group">
<span class="input-group-addon"><span class="glyphicon
glyphicon-lock"></span></span>
<select name="base_colour1" class="form-control" id="base_colour1">
<option value="">Base...</option>
<?php
echo "<option value=''>---- GW Golour ----</option></br>";
echo $base;
echo "<option value=''>---- Vallejo Game Golour ----</option>";
echo $base2;
echo "<option value=''>---- Vallejo Model Golour ----</option>";
echo $base3;
?>
</select>
</div>
</div>
答案 0 :(得分:3)
在测试!empty()
中添加字符串可能会更容易,否则它将始终在其中添加值-即使没有...
while($row = mysqli_fetch_assoc($resultpmb)) {
$code=$row["id"];
$name=$row["gw_name"];
if (!empty(trim($row['vg_name']))){
$base2 .= "<option value=" .$code.">".$row["vg_name"]."</option>";
}
if (!empty(trim($row['vm_name']))){
$base3 .= "<option value=" .$code.">".$row["vm_name"]."</option>";
}
$base .= "<option value=" .$code.">".$name."</option>";
}