Drupal 表单选项从循环填充

发布于 2024-12-27 22:54:01 字数 299 浏览 2 评论 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;

需要帮助吗?

I want to populate select option from loop

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

Need some help?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

丢了幸福的猪 2025-01-03 22:54:01

这是执行此操作的标准方法:

$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 循环完全相同。

This is the standard way to do it:

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

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

You might also look at the fetchAllKeyed method of the database query which is a handy shortcut to get data from the database into a keyed array suitable for select lists:

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

The above will produce exactly the same as the foreach loop above.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文