我想从循环中填充选择选项
$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;
需要一些帮助?
我想从循环中填充选择选项
$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;
需要一些帮助?
这是执行此操作的标准方法:
$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
以上将产生与上面的循环完全相同的内容。