title: Drupal: Render form including results, but don't duplicate resultset query
body: <p>Aim is to display a form, with a default resultset on the page, and allow the viewer to filter the results on the page by form submission. This part works.</p>
  
  <p>However: <strong>If a default set of results is included, then the initial submission builds two resultsets for display. Subsequent submissions build only one resultset.</strong> Apart from this additional resultset build, it works as intended.</p>
  
  <p>A <a href="https://stackoverflow.com/questions/2733306/drupal-how-to-render-results-of-form-on-same-page-as-form">couple</a> of <a href="https://stackoverflow.com/questions/4454938/including-results-along-with-a-form-on-the-same-page-and-theming-the-results-usin">questions</a> have asked about displaying forms and results on the same page before, and there's a <a href="http://www.lullabot.com/articles/drupal-5-making-forms-that-display-their-own-results" rel="nofollow noreferrer">Lullabot article</a> for Drupal5.</p>
  
  <p>Here's the code I'm using currently (available on Github @ <a href="https://github.com/GiantRobot/drupal-examples/tree/master/example_formandresults/example_formandresults.module" rel="nofollow noreferrer">example_formandresults.module</a>)</p>
  
  <pre><code>&lt;?php
  
  /**
   * Implementation of hook_menu().
   */
  function example_formandresults_menu() {
    $items['example_formandresults'] = array(
      'title' =&gt; 'Example: Form and Results',
      'description' =&gt; 'Show a form and its results on the same page.',
      'page callback' =&gt; 'drupal_get_form',
      'page arguments' =&gt; array( 'example_formandresults_form' ),
      'access arguments' =&gt; array('access content'),
      'type' =&gt; MENU_NORMAL_ITEM,
    ) ;
    return $items ;
  }
  
  /**
   * Form builder function.
   */
  function example_formandresults_form(&amp;$form_state = NULL) {
    /* Setting rebuild to true should prevent the results table being built twice. */
    $form_state['rebuild'] = TRUE ;
  
    if ( is_null($form_state) || !$form_state['submitted'] ) {
      drupal_set_message('Form has not been submitted.');
      /* set default 'since' value to unix timestamp of "one week ago" */
      $form_state['storage']['since'] = strtotime('-1 week');
    }
    else {
      $form_state['storage']['since'] = strtotime($form_state['values']['since']['year'] .'-'. $form_state['values']['since']['month'] .'-'. $form_state['values']['since']['day']);
    }
  
    $form['since'] = array(
      '#type' =&gt; 'date',
      '#title' =&gt; t('Since'),
      '#description' =&gt; t('Show entries since the selected date.'),
      '#default_value' =&gt; array(
        'month' =&gt; format_date($form_state['storage']['since'], 'custom', 'n'),
        'day'   =&gt; format_date($form_state['storage']['since'], 'custom', 'j'),
        'year'  =&gt; format_date($form_state['storage']['since'], 'custom', 'Y'),
      ),
      '#weight' =&gt; 5,
    ) ;
  
    $form['submit'] = array(
      '#type' =&gt; 'submit',
      '#value' =&gt; 'Submit',
      '#weight' =&gt; 10,
    ) ;
  
    if ( $form_state['submitted'] ) {
      $form['results'] = array(
        '#value' =&gt; example_formandresults_resultstable($form_state),
        '#type' =&gt; 'markup',
        '#weight' =&gt; 10, // bottom of form
      ) ;
    }
  
    return $form ;
  }
  
  /**
   * Build results table.
   */
  function example_formandresults_resultstable($form_state) {
    drupal_set_message('Building results table.', 'status', TRUE);
    dpm($form_state, 'form state');
    $sql = "SELECT uid FROM {users} WHERE login &gt;= %d ORDER BY login";
    $qry = db_query($sql, $form_state['storage']['since']);
    while ( $uid = db_result($qry) ) {
      $account = user_load($uid);
      /* there are plenty of good examples on how to theme tables in
       * forms; this isn't one. 
       */
      $rows[] = array(
        $account-&gt;name,
        format_date($account-&gt;login),
      ) ;
    }
    // dpm($rows, 'rows');
    $headers = array( 'Name', 'Last Login' );
    $result = t("&lt;h3&gt;Accounts logged in since: %since&lt;/h3&gt;", array('%since' =&gt; format_date($form_state['storage']['since']))) ;
    $result .= theme('table', $header, $rows) ;
    return $result ;
  }
  </code></pre>