2012-01-19 47 views
0

填充我想填充從環Drupal的形式從選項循環

$form['vote']['selected'] = array(
'#type' => 'select', 
'#title' => 'Select', 
'#name' => 'name', 
); 

foreach($loop as $row) 
$form['vote']['selected']['#options'] = array($row->id => $row->name); 
} 

return $form; 

選擇選項需要一些幫助?

回答

2

這是做它的標準方式:

$options = array(); 
foreach($loop as $row) 
    $options[$row->id] = $row->name; 
} 

$form['vote']['selected'] = array(
    '#type' => 'select', 
    '#title' => 'Select', 
    '#name' => 'name', 
    '#options' => $options 
); 

你也可以看看數據庫查詢的fetchAllKeyed方法,它是一個方便的快捷方式從數據庫中獲取的數據爲適合選擇一鍵陣列列表:

$options = db_query('SELECT id, name FROM {table}')->fetchAllKeyed(); 

上面將產生完全相同的與上述foreach循環。

+0

謝謝,爲我工作 – Laky