开发者

Drupal Form API - populate fields from Database

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

with:

    function mytopfive() {

  $form['mytop_header'] = array(
    '#type' => 'markup',
    '#value' => t(开发者_StackOverflow社区'<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.


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 :)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜