使用PHP动态生成MySQL条目中的选择标记
问题描述:
我有一个简单的设备跟踪表单,并在我的编辑表格页面中使用value="<?php echo $svc_tag;
填充现有文本值。我最近添加了一个选择标签,但是我坚持如何让选择框具有我从选择框中选择的MySQL中检索的当前值。通过研究,我发现价值属性在这里不适用。有谁知道有一种方法可以让我用MySQL中的当前值填充选择框。我在想,我需要一些PHP代码来动态生成选择代码。这是我所拥有的,但它不会返回存储在MySQL中的值;使用PHP动态生成MySQL条目中的选择标记
Equipment Status:
<?php
$option_to_preselect = $rsn_brwd;
echo $option_to_preselect;
$options = array
(
1 => 'Borrowed',
2 => 'In for Repair',
3 => 'Replacement',
4 => 'Returned',
5 => 'Other'
);
print '<select name="Borrwd_Rsn_val" id="input_select">';
foreach ($options as $index => $value)
{
print ' <option value="' . $index . '"';
if ($index == $option_to_preselect)
{
print ' selected ';
}
print '>' . $value . '</option>
';
}
print '</select>';
?>
答
只是想更新和发布,这是我如何解决了这个问题:
Equipment Status:
<?php
$option_to_preselect = $rsn_brwd;
$ddown_options = array
(
'Borrowed',
'In for Repair',
'Replacement',
'Returned',
'Other'
);
$arrlength=count($ddown_options);
print '<select name="ud_Borrwd_Rsn" id="input_select">';
for ($x=0;$x<$arrlength;$x++)
{
if ($ddown_options[$x] == $option_to_preselect)
{
print ' <option value="' . $ddown_options[$x] . '"' . ' selected="selected">' . $ddown_options[$x] . '</option>';
}
else {
print ' <option value="' . $ddown_options[$x] . '">' . $ddown_options[$x] . '</option>';
}
}
print '</select>';
?>