Php Drupal:渲染形式包括结果,但不';重复的结果集查询
目的是显示一个表单,页面上有一个默认的结果集,并允许查看器按表单提交过滤页面上的结果。这部分是有效的 但是:如果包含一组默认结果,则初始提交将生成两个结果集以供显示。后续提交仅生成一个结果集。除了此额外的结果集生成之外,它还可以按预期工作 很多人曾经询问过在同一页面上显示表单和结果的问题,而Drupal5有一个问题 以下是我当前使用的代码(可在Github@上找到)Php Drupal:渲染形式包括结果,但不';重复的结果集查询,php,drupal,drupal-6,drupal-fapi,Php,Drupal,Drupal 6,Drupal Fapi,目的是显示一个表单,页面上有一个默认的结果集,并允许查看器按表单提交过滤页面上的结果。这部分是有效的 但是:如果包含一组默认结果,则初始提交将生成两个结果集以供显示。后续提交仅生成一个结果集。除了此额外的结果集生成之外,它还可以按预期工作 很多人曾经询问过在同一页面上显示表单和结果的问题,而Drupal5有一个问题 以下是我当前使用的代码(可在Github@上找到) 尝试检查计数($form_state['values']),甚至$form_state['post'],以判断表单是否已提交。如果
尝试检查计数($form_state['values']),甚至$form_state['post'],以判断表单是否已提交。如果您的目标是显示表单,在页面上显示默认结果集,并允许查看者按表单提交筛选页面上的结果,并且如果此部分有效,问题是什么?在问题的第二段概述(也许我不清楚?)。它会运行两次resultset查询,这会增加负载—构建新表单时的查询是多余的。我正在玩弄的一个解决方案(*可能的异端)是在主题层中构建结果集。
<?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 ;
}