1

我要做的是使用表单 API 创建一个选择列表,用户可以在其中选择行并按下删除按钮,该按钮从数据库中删除选定的行,并重新加载具有已删除选定行的配置的表。使用我当前的代码,当我点击删除按钮时,我收到此错误:

警告:为 form_execute_handlers() 中的 foreach() 提供的参数无效(C:\xampp\htdocs\amaware\includes\form.inc 的第 1431 行)。

这是代码:

            /**
             * Implements hook_menu().
             */
            function smsfeed_menu() {
              $items = array();

              $items['admin/config/content/smsfeed'] = array(
                'title' => 'SMS Messages and Newsletters',
                'description' => 'Edit the Setting for the SMS Feed and Newsletter',
                'page callback' => 'drupal_get_form',
                'page arguments' => array('smsfeed_form'),
                'access arguments' => array('access administration pages'),
                'type' => MENU_NORMAL_ITEM,
                );

              $items['admin/config/content/smsfeed/settings'] = array(
                'title' => 'Settings',
                'type' => MENU_DEFAULT_LOCAL_TASK,
                    'weight' => 1,
                );  

              $items['admin/config/content/smsfeed/newsletter'] = array(
                'title' => 'Newsletter',
                'page callback' => 'drupal_get_form',
                    'page arguments' => array('newsletter_table_form'),
                'access arguments' => array('access administration pages'),
                'type' => MENU_LOCAL_TASK,
                    'weigth' => 2,
                    );

              $items['admin/config/content/smsfeed/feed'] = array(
                'title' => 'SMS Feed',
                'page callback' => 'drupal_get_form',
                'page arguments' => array('feed_form'),
                'access arguments' => array('access administration pages'),
                'type' => MENU_LOCAL_TASK,
                    'weight' => 3,
                    );

              $items['admin/config/content/smsfeed/questions'] = array(
                'title' => 'Questions',
                'page callback' => 'drupal_get_form',
                'page arguments' => array('questions_form'),
                'access arguments' => array('access administration pages'),
                'type' => MENU_LOCAL_TASK,
                    'weigth' => 4,
                    );

              return $items;
            }

            /**
             * Page callback: SMS Settings
             *
             * @see smsfeed_menu()
             */
            function smsfeed_form($form, &$form_state) {
              $form['response_time'] = array(
                '#type' => 'textfield',
                '#title' => t('Response Time (In Days)'),
                '#default_value' => variable_get('response_time', 3),
                '#size' => 2,
                '#maxlength' => 2,
                '#description' => t('The response time, in days, that users should expect.'),
              );

                $form['feed_keyword'] = array(
                  '#type' => 'textfield',
                '#title' => t('Feed Keyword'),
                '#default_value' => variable_get('feed_keyword', 'FEED'),
                '#size' => 10,
                '#maxlength' => 10,
                '#description' => t('The keyword users should text to have their message moderated and added to the website feed.'),
              );

                $form['join_keyword'] = array(
                  '#type' => 'textfield',
                '#title' => t('Join Keyword'),
                '#default_value' => variable_get('join_keyword', 'JOIN'),
                '#size' => 10,
                '#maxlength' => 10,
                '#description' => t('The keyword users should text to be added to the newsletter list.'),
              );

                $form['quit_keyword'] = array(
                  '#type' => 'textfield',
                '#title' => t('Quite Keyword'),
                '#default_value' => variable_get('quit_keyword', 'QUIT'),
                '#size' => 10,
                '#maxlength' => 10,
                '#description' => t('The keyword users should text to be removed from the newsletter list.'),
              );    

              return system_settings_form($form);
            }

            /**
             * Page Callback Newsletter
             */

            function newsletter_table_form($form, $form_state) {
                $rows = array();
                $header = array(t('User Name'), t('User Number'), t('User E-mail'),);

                $query = db_select('sms_newsletter');
                $query->fields('sms_newsletter', array('name', 'number', 'email',))
                    ->orderBy('name', 'ASC');
                $results = $query->execute();

                foreach ($results as $line) {
                    $rows[] = array(
                        $line->name,
                        $line->number,
                        $line->email,
                    );
                }

              $form['table'] = array(
                '#type' => 'tableselect',
                '#header' => $header,
                '#options' => $rows,
                '#empty' => t('No users found'),
              );

              $form['delete'] = array(
                '#type' => 'submit',
                '#value' => t('Delect Selected'),
                    '#submit' => 'newsletter_table_form_delete'
              );
              return $form;
            }

            function newsletter_table_form_delete ($form, &$form_state) {
                foreach ($form_state['values']['collections'] as $key => $value) {
                    if ($value) {
                            $delete = db_delete('sms_newsletter')
                            -> execute();
                    }
                }   
            }

任何帮助都深表感谢。

编辑:我编辑了我的 news_letter_table_form 以阅读:

function newsletter_table_form_delete ($form, $form_state) {
    foreach (($form_state['values']['table']) as $rows) {
            //$delete = db_delete('sms_newsletter')
            //-> execute();
            drupal_set_message ('' . $rows  . '');
    //drupal_set_message ('' . $form_state['values']['table']       . '');
    }
}

新的输出让我对如何识别选定的行并从数据库中删除它们感到困惑。使用上面的 newsletter_table_form_delete 我得到以下输出:

  1. 如果我选择第一行没有任何反应
  2. 如果我选择第二行输出为 1
  3. 如果我选择第三行,则输出为 2,依此类推
  4. 如果我选择多行,则输出是所选行的无序 html 列表。

我现在很好奇如何获取我现在拥有的内容并对其进行更改,以便我可以从数据库中删除选定的行。

再次感谢!

4

2 回答 2

2

尝试改变

  $form['delete'] = array(
    '#type' => 'submit',
    '#value' => t('Delect Selected'),
        '#submit' => 'newsletter_table_form_delete'
  );

  $form['delete'] = array(
    '#type' => 'submit',
    '#value' => t('Delect Selected'),
        '#submit' => array('newsletter_table_form_delete') // <-- make this an array
  );

根据您的问题编辑中添加的附加信息,我注意到您的 tableselect 是在没有指定数组键的情况下构建的,例如:

foreach ($results as $line) {
    $rows[] = array(  // <-- No array keys specified here 
        $line->name,
        $line->number,
        $line->email,
    );
}

由于没有指定键,PHP 的默认值是从 0 开始的索引。这就是为什么第一行没有结果,第二行没有结果,以此类推。

有关示例,请参阅元素的文档。tableselect

我不知道您的数据库表结构是什么样的,但您可能需要查询表的主键并将其作为数组键包含到您的$rows数组中。这样,当您在删除函数中处理数据时,您就有了要删除的行的主键并将其删除。

于 2012-08-01T21:35:57.250 回答
0

对我来说,这并不清楚。

我只想补充一点,最后 nmc 说的是你应该像这样构造你的行,在其中添加一个键到行。

foreach ($results as $line) {
                    $rows[$line->name] = array(
                        $line->name,
                        $line->number,
                        $line->email,
);
于 2014-03-28T10:15:01.327 回答