在D7中更新和删除数据

时间:2014-09-08 12:36:41

标签: drupal-7 drupal-modules

我使用表单API在D7中创建了一个表单,注册了一些用户并从数据库中检索了他们的注册数据;现在我想要的是在检索到的表中的每一行前面添加一个编辑和删除链接;我有在PHP中完成了但是如何在Drupal中实现它?任何人都知道如何做到这一点?

我的检索代码是:

function form_data_menu() {
    $items['formdata'] = array(
       'title' => 'Form Data',
       'page callback' => 'form_data_form',
       'access callback' => TRUE,
    );
    return $items;
}

function form_data_form()
{
    $results = db_query('SELECT * FROM {drupal}');
    $header = array(t('Id'),t('Name'),t('College'),t('Education'),t('Percentage'),t('Application'));
    $rows = array();

    foreach($results as $result) {
        $rows[] = array(
             $result->id,
             $result->name,
             $result->college,
             $result->education,
             $result->percentage,
             $result->application,
        );
    }

    return theme('table',array('header'=>$header,'rows'=>$rows));
}

我可以添加链接,但主要问题是如何在D7中运行更新和删除查询?

任何帮助将不胜感激。谢谢!

1 个答案:

答案 0 :(得分:0)

从示例模块“dbtng_example.module”开始。

https://www.drupal.org/project/examples

这将给出一个如何设置模块以保存到数据库的示例。我最近使用它创建了一个自定义模块,用于将数据保存到drupal中的新数据库表中。要弄清楚如何设置hook_schema,你可以查看drupal核心中的“.install”文件并贡献代码库。

然后要创建编辑或删除链接,您需要更改hook_menu以传入该元素的唯一ID:

  $items['admin/structure/nates-custom-page/update/%'] = array(
    'title' => 'Update entry',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('nates_module_form_update', 5),
    'weight' => -5,
    'access arguments' => array('administer DFP'),
  );

  $items['admin/structure/nates-custom-page/delete/%'] = array(
    'title' => 'Delete entry',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('nates_module_form_delete', 5),
    'access arguments' => array('administer DFP'),
  );

上面是我添加参数的示例(由数组键中的百分号(“%”)表示,它将表示我想要编辑或删除的项目的唯一ID。

然后更改编辑表单以通过ID加载要编辑的项目。

并添加删除表单。

以下是删除表单的示例:

function nates_module_form_delete($form, &$form_state) {

  $entry = nates_module_entry_load_by_pid(arg(5));

  $form = array();
  $form['pid'] = array(
    '#type' => 'value',
    '#value' => arg(5),
  );

  $output = "";

  foreach($entry as $key => $value) {
    $output .= "<p><strong>$key</strong>: $value</p>";
  }

  $form['markup'] = array(
    '#type' => 'markup',
    '#markup' => $output,
  );

  return confirm_form(
    $form,
    t('Are you sure you want to delete this item? '),
    'example/list',
    t('This action cannot be undone.'),
    t('Delete'),
    t('Cancel')
  );



  return $form;
}

注意我先退回确认表。

这是提交处理程序函数,它在提交请求后对其进行处理:

function nates_module_form_delete_submit($form, &$form_state) {
  global $user;

  nates_module_entry_delete($form_state['values']['pid']);
  drupal_set_message(t("Deleted entry"));
}

然后,编辑您的列表页面以添加编辑链接并删除链接:

function nates_module_list() {
  $output = '';

  // Get all entries in the dfp_amobee2 table.
  if ($entries = nates_module_entry_load_all()) {
    $rows = array();
    foreach ($entries as $entry) {
      // Sanitize the data before handing it off to the theme layer.
      $entry->editlink = l('edit','admin/structure/nates-custom-page/update/' . $entry->pid, array('query' => array('destination' => 'admin/structure/nates-custom-page')));
       $entry->deletelink = l('delete','admin/structure/nates-custom-page/delete/' . $entry->pid, array('query' => array('destination' => 'admin/structure/nates-custom-page')));

      unset($entry->pid);
      if(!empty($entry->tid)) {
        $entry->alias = l($entry->alias, 'taxonomy/term/'.$entry->tid);  
      }
      else if(isset($entry->pages)) {

        if(drupal_valid_path($entry->pages)) {

          $entry->alias = l(drupal_lookup_path('alias', $entry->pages),$entry->pages);
        } else {
          $entry->alias = check_markup($entry->pages);  
        }


      } else {
        $entry->alias = "";
      }
      unset($entry->pages);

      if(!$entry->tid) {
        $entry->tid = "";
      }




      $rows[] =  (array) $entry;
    }
    // Make a table for them.
    $header = array(t('path'), t('tid'), t('dfp tag machine_name'),t('amobee_as'), '', '');

    $output .= '<p>' . t('If the "tid" is filled in, amobee_as will be used on taxonomy pages and on node pages (where the main term id matches).  If the path is filled it, will be used on that path.  Path supercedes "tid" when there is a conflict.') . '</p>';

    $output .= theme('table', array('header' => $header, 'rows' => $rows));
  }
  else {
    drupal_set_message(t('No entries have been added yet.'));
  }
  return $output;
}
相关问题