2013-03-27 69 views
1

我試圖修改一個函數,我一直在使用動態填充<select>元素來使用數據庫中的數組。原始函數使用硬編碼數組來填充元素,並預先選擇了與db值相匹配的選項。修改函數動態填充選擇要使用數組的數據庫

修改後的函數創建元素,但它只是從db中添加第一個值。我怎樣才能修改它,以便循環遍歷所有應該添加到<select>元素的值?

PHP函數和查詢

<?php 
function printSelectOptions($dataArray, $currentSelection) { 
    foreach ($dataArray as $key => $value) { 
     echo '<option ' . (($key == $currentSelection)) . ' value="' . $key . '">' . $value . '</option>'; 
    } 
} 
try { 
    $stmt = $conn->prepare("SELECT * FROM student"); 
    $stmt->execute(); 
    }catch(PDOException $e) { 
    echo $e->getMessage(); 
} 
$row = $stmt->fetch(); 
?> 

填充選擇元素

<select name="fname"> 
    <?php 
     echo printSelectOptions(array($row['fname'])); 
    ?> 
</select> 

原有功能&代碼填入一元

function printSelectOptions($dataArray, $currentSelection) { 
    foreach ($dataArray as $key => $value) { 
     echo '<option ' . (($key == $currentSelection) ? 'selected="selected"' : '') . ' value="' . $key . '">' . $value . '</option>'; 
    } 
} 

<select name="fname"> 
    <?php 
     $options = array("John"=>"John", "Mary"=>"Mary", "Elizabeth"=>"Elizabeth"); 
     $selected = $row['fname']; 
     echo printSelectOptions($options, $selected); 
    ?> 
</select> 
+0

無需改變功能即可。只需在數據庫查詢後準備數組列表。 ' - > fetchAll'可能會有所幫助。 – mario 2013-03-27 01:32:16

+0

@mario - 謝謝你讓我知道' - > fetchAll'。不幸的是這個功能需要比多一點,但值得慶幸的是邁克爾的回答得到它:-)工作 – 2013-03-27 01:59:38

回答

1

由於您只通過fetch()獲取了單個行,因此只有一個值傳入您的函數printSelectOptions()。取而代之,通過fetchAll() 獲取所有行並修改您的函數以接收完整陣列,再加上字符串,這是您要打印的列名稱(數組鍵)。

// All rows into $rows... 
$rows = $stmt->fetchAll(); 

// Make the function accept the full 2D array, and 
// a string key which is the field name to list out: 
function printSelectOptions($dataArray, $currentSelection, $fieldname) { 
    // String to hold output 
    $output = ''; 
    foreach ($dataArray as $key => $value) { 
     // Rather than echo here, accumulate each option into the $output string 
     // Use the $fieldname as a key to $value which is now an array... 
     $output .= '<option ' . (($key == $currentSelection)) . ' value="' . $key . '">' . htmlspecialchars($value[$fieldname], ENT_QUOTES) . '</option>'; 
    } 
    return $output; 
} 

然後調用該函數爲:

echo printSelectOptions($rows, $currentSelection, 'fname'); 

它是現在,期權的價值屬性由數組鍵,這將從零開始編號填充的方式。這與您的原始數組版本類似,但指定另一個列名稱(如id)作爲關鍵列可能更有用。

// This one also takes a $valuename to use in place of $key... 
function printSelectOptions($dataArray, $currentSelection, $valuename, $fieldname) { 
    // String to hold output 
    $output = ''; 
    foreach ($dataArray as $key => $value) { 
     // Rather than echo here, accumulate each option into the $output string 
     // Use the $fieldname as a key to $value which is now an array... 
     $output .= '<option ' . (($value[$valuename] == $currentSelection)) . ' value="' . $value[$valuename] . '">' . htmlspecialchars($value[$fieldname], ENT_QUOTES) . '</option>'; 
    } 
    return $output; 
} 

,並會被稱爲:

echo printSelectOptions($rows, $currentSelection, 'id', 'fname'); 
+0

這些方法都非常完美:-D(我假設你抓到你前面貼;-)的一個問題) 2個問題 - PHP中是否有類似於jQuery(this)的方法,可以在函數被調用時用來代替'fname'?另外,出於好奇,指定另一個列名(如id)作爲關鍵列的好處是什麼? – 2013-03-27 01:57:29

+1

@ChayaCooper傳入''fname''的原因是爲了使它成爲通用的,所以無論'$ rows'數組中的列是什麼,都可以選擇輸出的列。所以與jQuery'$(this)'沒有什麼關係,因爲在PHP數組上下文中,$(this)指向foreach循環中的$ value。 – 2013-03-27 02:02:17

+1

@ChayaCooper指定列名稱只是增加了靈活性。由於該函數已經被構建爲通用的,並且允許您選擇'