如何将数据库内容加载到下拉选项(选择)

发布于 2024-12-21 09:09:50 字数 1915 浏览 1 评论 0原文

我仍在开发自己的 drupal 7 模块,但遇到了一些麻烦。我正在尝试将数据库内容加载到下拉选项(选择),我已从 drupal 示例中读取和写入相同的代码,但我的数据库仍未加载,只有空选项。 我要问的是我的代码有什么问题吗?或者除了 drupal 示例之外,还有其他更快的方法将数据库加载到下拉选项吗???

这是我正在处理的代码

function prod_entry_load($entry = array()) {
  $select = db_select('aa_1122','aa');
  $select->fields('aa');

  foreach ($entry as $field => $value) {
    $select->condition($field, $value);
  }
  return $select->execute()->fetchAll();
}

function prod(){
    return drupal_get_form('prod_form');
}
function prod_form($form_state){
  $form = array(
      '#prefix' => '<div id="updateform">',
      '#suffix' => '</div>',
  );

  $entries = prod_entry_load();
  $keyed_entries = array();
  if (empty($entries)) {
    $form['no_values'] = array(
      '#value' => t("No entries exist in the table dbtng_example table."),
    );
    return $form;
  }

  foreach ($entries as $entry) {
    $options[$entry->no] = t("@no: @name ", array('@no' => $entry->no, '@name' => $entry->name));
    $keyed_entries[$entry->no] = $entry;
  }
  $default_entry = !empty($form_state['values']['no']) ? $keyed_entries[$form_state['values']['no']] : $entries[0];

  $form_state['entries'] = $keyed_entries;

  $form['no'] = array(
    '#type' => 'select',
    '#title' => t('Choose'),
    '#default_value' => $default_entry->no,
    '#ajax' => array(
        'wrapper' => 'updateform',
        'callback' => 'prod_form_update_callback',
    ),
  );
  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => 'Submit',
    '#ajax' => array(
      'callback' => 'ajax_alert',
    ),
  );
  return $form;
}

function prod_form_update_callback($form, $form_state) {
  $entry = $form_state['entries'][$form_state['values']['no']];  
  foreach (array('name') as $item) {
    $form[$item]['#value'] = $entry->$item;
  }
  return $form;
}

i'm still working on my own drupal 7 module, and i'm having some trouble. I'm trying to load database content to dropdown option (select), i have read and write the same code from drupal example, but my database still not loading, only empty option.
what i'm asking is there anything wrong on my code or is there any faster way to load database to dropdown option other than the drupal example???

here the code i'm working on

function prod_entry_load($entry = array()) {
  $select = db_select('aa_1122','aa');
  $select->fields('aa');

  foreach ($entry as $field => $value) {
    $select->condition($field, $value);
  }
  return $select->execute()->fetchAll();
}

function prod(){
    return drupal_get_form('prod_form');
}
function prod_form($form_state){
  $form = array(
      '#prefix' => '<div id="updateform">',
      '#suffix' => '</div>',
  );

  $entries = prod_entry_load();
  $keyed_entries = array();
  if (empty($entries)) {
    $form['no_values'] = array(
      '#value' => t("No entries exist in the table dbtng_example table."),
    );
    return $form;
  }

  foreach ($entries as $entry) {
    $options[$entry->no] = t("@no: @name ", array('@no' => $entry->no, '@name' => $entry->name));
    $keyed_entries[$entry->no] = $entry;
  }
  $default_entry = !empty($form_state['values']['no']) ? $keyed_entries[$form_state['values']['no']] : $entries[0];

  $form_state['entries'] = $keyed_entries;

  $form['no'] = array(
    '#type' => 'select',
    '#title' => t('Choose'),
    '#default_value' => $default_entry->no,
    '#ajax' => array(
        'wrapper' => 'updateform',
        'callback' => 'prod_form_update_callback',
    ),
  );
  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => 'Submit',
    '#ajax' => array(
      'callback' => 'ajax_alert',
    ),
  );
  return $form;
}

function prod_form_update_callback($form, $form_state) {
  $entry = $form_state['entries'][$form_state['values']['no']];  
  foreach (array('name') as $item) {
    $form[$item]['#value'] = $entry->$item;
  }
  return $form;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

百变从容 2024-12-28 09:09:50

您刚刚忘记将 #options 键添加到您的选择元素中,代码应如下所示:

$form['no'] = array(
  '#type' => 'select',
  '#title' => t('Choose'),
  '#default_value' => $default_entry->no,
  '#options' => $options, // This is the bit that was missing
  '#ajax' => array(
    'wrapper' => 'updateform',
    'callback' => 'prod_form_update_callback',
  ),
);

您可以使用 MySQL 和数据库中的字符串连接组合稍微缩短您的查询/选项代码fetchAllKeyed() 方法:

$query = db_select('aa_1122', 'aa')->fields('aa', array('no'));
$query->addExpression("CONCAT(no, ': ', name)", 'display');
$options = $query->execute()->fetchAllKeyed();

You've just forgotten to add the #options key to your select element, the code should read like this:

$form['no'] = array(
  '#type' => 'select',
  '#title' => t('Choose'),
  '#default_value' => $default_entry->no,
  '#options' => $options, // This is the bit that was missing
  '#ajax' => array(
    'wrapper' => 'updateform',
    'callback' => 'prod_form_update_callback',
  ),
);

You could shorten your query/options code slightly using a combination of string concatenation in MySQL and the db fetchAllKeyed() method:

$query = db_select('aa_1122', 'aa')->fields('aa', array('no'));
$query->addExpression("CONCAT(no, ': ', name)", 'display');
$options = $query->execute()->fetchAllKeyed();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文