使用PHP从MySQL条目动态生成一个select标记


Using PHP to dynamically generate a select tag from MySQL entry

我有一个简单的设备跟踪表单,在我的编辑表页面中,我使用value="<?php echo $svc_tag;填充现有的文本值。我最近添加了一个select标记,但我一直在思考如何让select框中包含我从MySQL中检索到的当前值。通过研究,我发现价值属性在这里不适用。有人知道我用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>';
?>