我从SQL服务器调用选项列表并输出到数组中..请参阅查询:
SQL
$location = "SELECT location_id, location_name, location_postcode, location_buildingname
FROM dbo.system_locations";
$stmt = sqlsrv_query( $conn, $location );
PHP
<select class="form-control" name="location">
<?php while ($row = sqlsrv_fetch_array($stmt)){ ?>
<option>
<?php echo $row['location_name']?>
</option>
<?php } ?>
</select>
这很好用,但我希望在数据库中选择的选项是选定的选项..
有一个因素,可以通过访问设置部分添加选择列表并添加新位置,否则我将使用三元运算符方法。
答案 0 :(得分:0)
试试这个
<select class="form-control" name="location">
<?php while ($row = sqlsrv_fetch_array($stmt)){ ?>
<option <?php ($row['selected'] == 1){echo 'selected';}?>>
<?php echo $row['location_name']?>
</option>
<?php }?>
</select>