目标是在页面上显示带有默认结果集的表单,并允许查看者通过表单提交过滤页面上的结果。这部分有效。
但是:如果包含一组默认结果,则初始提交会构建两个结果集以供显示。后续提交只构建一个结果集。除了这个额外的结果集构建之外,它还可以按预期工作。
couple的questions已经询问过在同一页面上显示表单和结果,并且Drupal5有Lullabot article。
以下是我目前正在使用的代码(可在Github @ example_formandresults.module上找到)
<?php
/**
* Implementation of hook_menu().
*/
function example_formandresults_menu() {
$items['example_formandresults'] = array(
'title' => 'Example: Form and Results',
'description' => 'Show a form and its results on the same page.',
'page callback' => 'drupal_get_form',
'page arguments' => array( 'example_formandresults_form' ),
'access arguments' => array('access content'),
'type' => MENU_NORMAL_ITEM,
) ;
return $items ;
}
/**
* Form builder function.
*/
function example_formandresults_form(&$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' => 'date',
'#title' => t('Since'),
'#description' => t('Show entries since the selected date.'),
'#default_value' => array(
'month' => format_date($form_state['storage']['since'], 'custom', 'n'),
'day' => format_date($form_state['storage']['since'], 'custom', 'j'),
'year' => format_date($form_state['storage']['since'], 'custom', 'Y'),
),
'#weight' => 5,
) ;
$form['submit'] = array(
'#type' => 'submit',
'#value' => 'Submit',
'#weight' => 10,
) ;
if ( $form_state['submitted'] ) {
$form['results'] = array(
'#value' => example_formandresults_resultstable($form_state),
'#type' => 'markup',
'#weight' => 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 >= %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->name,
format_date($account->login),
) ;
}
// dpm($rows, 'rows');
$headers = array( 'Name', 'Last Login' );
$result = t("<h3>Accounts logged in since: %since</h3>", array('%since' => format_date($form_state['storage']['since']))) ;
$result .= theme('table', $header, $rows) ;
return $result ;
}
答案 0 :(得分:0)
尝试检查计数($ form_state ['values'])甚至$ form_state ['post']以判断表单是否已提交。