将选项标记为从动态选项列表中选择

时间:2014-04-03 20:34:41

标签: php html

我在Selected="Selected"上使用SELECT,这是由我写入代码的值提供的,我的查询是如何将它与MySQL数据库中的动态查询一起使用?

这是我的书面代码

<label>Fuel</label>
<select tabindex="1" id="proptenure" name="proptenure">
<option value=""></option>
<option value="1" <?php echo ($searchfuel == '1' ? 'selected' : '')?>>Mains gas</option>
<option value="2" <?php echo ($searchfuel == '2' ? 'selected' : '')?>>Wood or coal fire</option>
<option value="3" <?php echo ($searchfuel == '3' ? 'selected' : '')?>>Oil</option>
<option value="4" <?php echo ($searchfuel == '4' ? 'selected' : '')?>>Electric storage heaters</option>
<option value="5" <?php echo ($searchfuel == '5' ? 'selected' : '')?>>LPG or bottled gas</option>
<option value="6" <?php echo ($searchfuel == '6' ? 'selected' : '')?>>No central heating system</option>
</select>

如何在基于查询的SELECT

上使用上述类型的行
<?php echo ($searchtenure == '2' ? 'selected' : '')?>

这是我如何使用SELECT查询

的示例
<label>Fuel Type</label> 
<?php $fueltype = db::getInstance()->query('SELECT * FROM lkup_fueltype'); 
if(!$fueltype->count()) { 
    echo 'Problem'; 
} else { ?> 
<select tabindex="1" id="propertyfueltype" name="propertyfueltype"> 
<?php foreach ($fueltype->results() as $fueltype) { ?> 
<option value="<?php echo $fueltype->PropertyFuelType; ?>"><?php echo $fueltype->PropertyFuelType; ?></option> <?php } } ?> 
</select>

那么如何使用Selected =“selected”?

1 个答案:

答案 0 :(得分:0)

这应该做的工作:

<label>Fuel Type</label> 
<?php $fueltype = db::getInstance()->query('SELECT * FROM lkup_fueltype'); 
if(!$fueltype->count()) { 
    echo 'Problem'; 
} else { ?> 
<select tabindex="1" id="propertyfueltype" name="propertyfueltype"> 
<?php foreach ($fueltype->results() as $fueltype) { ?> 
<?php if($fueltype->PropertyFuelType == 2){ $selected = ' selected="selected"'; }else{ $selected = NULL; }?>
<option value="<?php echo $fueltype->PropertyFuelType; ?>"<?php echo $selected; ?>><?php echo $fueltype->PropertyFuelType; ?></option> <?php } } ?> 
</select>