Drupal Form API - 从数据库填充字段

发布于 2024-10-02 16:21:50 字数 1137 浏览 4 评论 0原文

我希望用数据库中的记录填充 drupals 表单 api 字段。

with:

    function mytopfive() {

  $form['mytop_header'] = array(
    '#type' => 'markup',
    '#value' => t('<h2>Your favourite Jobs</h2>'),
  );


    $result = mysql_query('SELECT * FROM topfive WHERE uid = 1 ORDER BY order_value ASC');

  while ($node = db_fetch_object($result)) {
    $rid = $node->rid;
    $order = $node->order_value;
    $title= $node->title;
  $form['rid'][$node->rid] = array(
    '#type' => 'textfield',
    '#size' => 2,
    '#maxlength' => 1,
    '#default_value' => $rid,
  );

  $form['job_name'][$node->rid] = array(
    '#type' => 'textfield',
    '#title' => t('To'),
    '#size' => 40,
    '#maxlength' => 42,
    '#value' => $title,
  );
  $form['job_order'][$node->rid] = array(
    '#type' => 'textfield',
    '#size' => 2,
    '#maxlength' => 1,
    '#default_value' => $order,
  );

  }

  $form['submit'] = array('#type' => 'submit', '#value' => t('Submit'));



return $form;
}

它返回数组。

我觉得答案是 foreach 循环。

非常感谢任何帮助。

Im looking to populate drupals form api fields with records from the database.

with:

    function mytopfive() {

  $form['mytop_header'] = array(
    '#type' => 'markup',
    '#value' => t('<h2>Your favourite Jobs</h2>'),
  );


    $result = mysql_query('SELECT * FROM topfive WHERE uid = 1 ORDER BY order_value ASC');

  while ($node = db_fetch_object($result)) {
    $rid = $node->rid;
    $order = $node->order_value;
    $title= $node->title;
  $form['rid'][$node->rid] = array(
    '#type' => 'textfield',
    '#size' => 2,
    '#maxlength' => 1,
    '#default_value' => $rid,
  );

  $form['job_name'][$node->rid] = array(
    '#type' => 'textfield',
    '#title' => t('To'),
    '#size' => 40,
    '#maxlength' => 42,
    '#value' => $title,
  );
  $form['job_order'][$node->rid] = array(
    '#type' => 'textfield',
    '#size' => 2,
    '#maxlength' => 1,
    '#default_value' => $order,
  );

  }

  $form['submit'] = array('#type' => 'submit', '#value' => t('Submit'));



return $form;
}

it returns Array.

I feel the answer is a foreach loop.

any help much appreciated.

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

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

发布评论

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

评论(1

朮生 2024-10-09 16:21:50

当你说:“it returns Array”...是Echo的输出,还是drupal页面的输出?

如果是后者,您需要从 drupal_get_form 调用此函数才能正确呈现表单。因此,在您的情况下,您需要一个带有 'pagecallback' => 的菜单项'drupal_get_form''页面参数' =>数组('mytop Five')

如果这没有意义,请告诉我:)

When you say: "it returns Array"... is that the output from an Echo, or the output to the drupal page?

if its the latter, you need to be calling this function from drupal_get_form in order to have the form rendered correctly. so in your case you need a menu item with 'page callback' => 'drupal_get_form' and 'page arguments' => array('mytopfive').

Let me know if that makes no sense :)

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