我需要一个帮助。我无法使用PHP和MySQL在select标签中显示一些db值。我正在解释下面的代码。
<?php
while($row=mysql_fetch_array($sql)){
if($row['status']==1){
$status=1;
}else{
$status=0;
}
$id=$row['id'];
echo "<tr>
<td>".$counter++."</td>
<td>".$row['pincode']."</td>
<td>".$row['area']."</td>
<td>
<select class='form-control' id='status' name='status' onchange='javascript:adminstatus()' style='height:23px; padding:0px;'>
<option value=''>Select Status</option>
<option value='1' ".$row['status']."=='1'?'selected':'';>Enable</option>
<option value='0' ".$row['status']."=='0'?'selected':'';>Disable</option>
</select>
</td>
?>
我需要在$status==1
启用时选择,$status==0
禁用将选择。请帮助我。
答案 0 :(得分:1)
**更新** 更正我会把你的语句从echo中取出来,只使用echo来表示php值。它将更容易阅读和维护。例如:
<td><?php echo $counter++ ?></td>
然后我会像这样编写逻辑部分
<option value='1' <?php echo ($row['status']=='1'? 'selected' : '') ?>>Enable</option>
<option value='0' <?php echo ($row['status']=='0'? 'selected' : '') ?>>Disable</option>
但是,如果你想以你的方式编写代码......
$id=$row['id'];
echo "<tr>
<td>".$counter++."</td>
<td>".$row['pincode']."</td>
<td>".$row['area']."</td>
<td>
<select class='form-control' id='status' name='status' onchange='javascript:adminstatus()' style='height:23px; padding:0px;'>
<option value=''>Select Status</option>
<option value='1' " . ($row['status']=='1'?'selected':'') . ">Enable</option>
<option value='0' " . ($row['status']=='0'?'selected':'') . ">Disable</option>
</select>
</td>