0

我想从循环中填充选择选项

$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;

需要一些帮助?

4

1 回答 1

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以上将产生与上面的循环完全相同的内容。

于 2012-01-19T12:36:00.567 回答