2014-09-19 112 views
0

我有一個簡單的設備跟蹤表單,並在我的編輯表格頁面中使用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>'; 
    ?> 

回答

0

只是想更新和發佈,這是我如何解決了這個問題:

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>'; 
?>