我有一个下拉列表工作正常,但是80个值列表的中间位置是我想要的默认值
$cosOptions = cos_options();
是变量
<td><select name="options"><?php print $cosOptions; ?></select></td>
打印下拉列表,然后打印下面的函数。
function cos_options()
{
$dbh = dbh_get();
$options = '';
$sql = 'select code, descr FROM cos';
$stmt = $dbh->prepare($sql);
$stmt->execute();
while (true) {
$r = $stmt->fetch();
if (is_bool($r)) break;
$options .= '<option value="' . $r['code'] . ' ' . $r['descr'] . '">' . $r['code'] . ' ' . $r['descr'] . '</option>';
}
dbh_free($dbh);
return $options;
}
Postgres表只有两列和80行
代码说明
D1 Pizza
D2面包
D3 Cornflakes等,
我需要的是首先选择 D43 Rice 因为90%的时间是这个。我怎样才能做到这一点?我在这里经历了很多帖子,但无法让它发挥作用。
答案 0 :(得分:1)
您可以使用此代码选择D43Rice选项值。不要使用D43 Rice并合并D43Rice,有时文本空间会添加
<?php
while (true) {
$r = $stmt->fetch();
if (is_bool($r)) {
break;
}
$selected = '';
if ($r['code'].$r['descr'] == 'D43Rice') {
$selected = 'selected="selected"';
}
$options .= '<option "' . $selected . '" value="' . $r['code'] . ' ' . $r['descr'] . '">' . $r['code'] . ' ' . $r['descr'] . '</option>';
}
?>
这是检查代码值的更新条件
<?php
while (true) {
$r = $stmt->fetch();
if (is_bool($r)) {
break;
}
$selected = $r['code'].$r['descr'] = 'D43Rice' ? 'selected="selected"' : '';
$options .= '<option "' . $selected . '" value="' . $r['code'] . ' ' . $r['descr'] . '">' . $r['code'] . ' ' . $r['descr'] . '</option>';
}
?>
答案 1 :(得分:1)
function cos_options()
{
$dbh = dbh_get();
$options = '';
$sql = 'select code, descr FROM cos';
$stmt = $dbh->prepare($sql);
$stmt->execute();
while (true) {
$r = $stmt->fetch();
if (is_bool($r)) break;
$temp=$r['code']. ' '. $r['descr'];
if($temp=="D43 Rice") //check for whatever you want to set selected
{
$options .= '<option selected value="' . $r['code'] . ' ' . $r['descr'] . '">' . $r['code'] . ' ' . $r['descr'] . '</option>';
}
else
{
$options .= '<option value="' . $r['code'] . ' ' . $r['descr'] . '">' . $r['code'] . ' ' . $r['descr'] . '</option>';
}
}
dbh_free($dbh);
return $options;
}
答案 2 :(得分:1)
<?php
if ($whateveryourneededcodeis == "D43")
{
$selected = 'selected="selected"';
} else {
$selected = '';
}
$options .= '<option value="'.$r['code'].''.$r['descr'].'" $selected>'
.$r['code'].''.$r['descr'].'
</option>';
?>