2012-01-10 38 views
0

我無法讓數組顯示出來。從查詢中拆分數組

$womensLevels = array('WEXHIB' => 'Exhib Camp/Test', 'WLEVEL01' => 'Level 1', 'WLEVEL02' => 'Level 2', 'WLEVEL03' => 'Level 3', 'WLEVEL04' => 'Level 4', 'WLEVEL05' => 'Level 5', 'WLEVEL06' => 'Level 6', 'WLEVEL07' => 'Level 7', 'WLEVEL08' => 'Level 8', 'WLEVEL09' => 'Level 9', 'WLEVEL10' => 'Level 10', 'WOPEN' => 'Open', 'WPREPOPT' => 'Prep OPT', 'WTOPS' => 'TOPS'); 

它結束了像這樣,當我做我的查詢,並將其放入我的觀點(HTML)。我只想讓代碼鍵和描述鍵顯示值。

[0]=> 
    array(2) { 
    ["Code"]=> 
    string(4) "IW01" 
    ["Description"]=> 
    string(13) "Intro Level 1" 
    } 
    [1]=> 
    array(2) { 
    ["Code"]=> 
    string(4) "IW02" 
    ["Description"]=> 
    string(13) "Intro Level 2" 
    } 
    [2]=> 
    array(2) { 
    ["Code"]=> 
    string(4) "IW03" 
    ["Description"]=> 
    string(13) "Intro Level 3" 
    } 

這是我的疑問。

這是數組來自的地方。

$womensLevels = sprintf("SELECT `Code`,`Description` FROM `mb_ro_type_codes` where `Program` = '%s'", $sanction->Program); 

$smarty->assign('womensLevels', $db->query($womensLevels)); 

這是我的html我把我的變量從我的控制器。

{html_checkboxes name='code' options=$womensLevels selected=$checked} 

我想我想問的是如何分解我的數組?

回答

2

您必須循環db查詢結果並以所需格式構建另一個數組,而不是直接將查詢結果傳遞給模板。像這樣:

$results = $db->query($womensLevels); 
$formattedResults = array(); 
foreach($results as $result) { 
    formattedResults[$result['Code']] = $result['Description']; 
} 
$smarty->assign('womensLevels', $formattedResults); 
+0

這是正確的答案。謝謝! :) – wowzuzz 2012-01-10 21:09:11