title: Drupal Form API - populate fields from Database
body: <p>Im looking to populate drupals form api fields with records from the database.</p>
  
  <p>with:</p>
  
  <pre><code>    function mytopfive() {
  
    $form['mytop_header'] = array(
      '#type' =&gt; 'markup',
      '#value' =&gt; t('&lt;h2&gt;Your favourite Jobs&lt;/h2&gt;'),
    );
  
  
      $result = mysql_query('SELECT * FROM topfive WHERE uid = 1 ORDER BY order_value ASC');
  
    while ($node = db_fetch_object($result)) {
      $rid = $node-&gt;rid;
      $order = $node-&gt;order_value;
      $title= $node-&gt;title;
    $form['rid'][$node-&gt;rid] = array(
      '#type' =&gt; 'textfield',
      '#size' =&gt; 2,
      '#maxlength' =&gt; 1,
      '#default_value' =&gt; $rid,
    );
  
    $form['job_name'][$node-&gt;rid] = array(
      '#type' =&gt; 'textfield',
      '#title' =&gt; t('To'),
      '#size' =&gt; 40,
      '#maxlength' =&gt; 42,
      '#value' =&gt; $title,
    );
    $form['job_order'][$node-&gt;rid] = array(
      '#type' =&gt; 'textfield',
      '#size' =&gt; 2,
      '#maxlength' =&gt; 1,
      '#default_value' =&gt; $order,
    );
  
    }
  
    $form['submit'] = array('#type' =&gt; 'submit', '#value' =&gt; t('Submit'));
  
  
  
  return $form;
  }
  </code></pre>
  
  <p>it returns Array. </p>
  
  <p>I feel the answer is a foreach loop.</p>
  
  <p>any help much appreciated.</p>