通过从数据库表中获取值,在while循环中在php中打印下拉列表。这是代码:
<select name='supervisor' class='form-control' name='supervisor'>
<?php
$sql = "SELECT username FROM system_user where type='supervisor'";
$result = mysql_query($sql);
while ($row = mysql_fetch_array($result)) {
echo "<option value='" . $row['username'] ."'>" . $row['username'] ."</option>";}?>
</select>
如何为此设置默认值?我想将一个“用户名”值作为默认值。我怎样才能做到这一点?
答案 0 :(得分:2)
<select name='supervisor' class='form-control' name='supervisor'>
<?php
$sql = "SELECT username FROM system_user where type='supervisor'";
$result = mysql_query($sql);
while ($row = mysql_fetch_array($result)) {
$sel = ''; if($row['username'] == 'usename'){ $sel = 'selected'; }
echo "<option $sel value='" . $row['username'] ."'>" . $row['username'] ."</option>";
} ?>
</select>
答案 1 :(得分:1)
function dropDown(array $array, $default = null, $select_attrs = '')
{
$s = '<select $select_attr>';
foreach((array)$array as $k => &$v) {
$default = ($v === $default) ? 'selected' : null;
$s.='<option '.$default.' >'.$v.'</option>';
}
return $s;
}
答案 2 :(得分:1)
这个对我有用......
<select name='supervisor' class='form-control' name='supervisor'>
<?php
$sql = "SELECT username FROM system_user where type='supervisor'";
$result = mysql_query($sql);
while ($row = mysql_fetch_array($result)) {
$sel = ''; if($row['username'] == 'usename'){ $sel = 'selected'; }
echo "<option $sel value='" . $row['username'] ."'>" . $row['username'] ."</option>";
} ?>