Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/312.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Drupal 7 更新和删除D7中的数据_Drupal 7_Drupal Modules - Fatal编程技术网

Drupal 7 更新和删除D7中的数据

Drupal 7 更新和删除D7中的数据,drupal-7,drupal-modules,Drupal 7,Drupal Modules,我使用FormAPI在D7中制作了一个表单,注册了一些用户,并从数据库中检索了他们注册的数据;现在我想要的是在检索到的表中的每一行前面添加一个edit和delete链接;我已经在PHP中实现了这一点,但是如何在Drupal中实现它呢?;有人知道怎么做吗 我的检索代码是: function form_data_menu() { $items['formdata'] = array( 'title' => 'Form Data', 'page callbac

我使用FormAPI在D7中制作了一个表单,注册了一些用户,并从数据库中检索了他们注册的数据;现在我想要的是在检索到的表中的每一行前面添加一个edit和delete链接;我已经在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中运行更新和删除查询


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

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

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

然后,要创建编辑或删除链接,需要更改钩子菜单以传入该元素的唯一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_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;
}
函数nates\u模块\u列表(){
$output='';
//获取dfp_amobee2表中的所有条目。
如果($entries=nates\u module\u entry\u load\u all()){
$rows=array();
foreach($entries作为$entry){
//在将数据移交给主题层之前,先对其进行清理。
$entry->editlink=l('edit','admin/structure/nates自定义页/update/'。$entry->pid,数组('query'=>array('destination'=>'admin/structure/nates自定义页'));
$entry->deletelink=l('delete','admin/structure/nates自定义页/delete/'。$entry->pid,数组('query'=>array('destination'=>'admin/structure/nates自定义页'));
未设置($entry->pid);
如果(!空($entry->tid)){
$entry->alias=l($entry->alias,'taxonomy/term/'。$entry->tid);
}
else if(设置($entry->pages)){
if(drupal\u有效路径($entry->pages)){
$entry->alias=l(drupal\u查找路径('alias',$entry->pages),$entry->pages);
}否则{
$entry->alias=检查标记($entry->pages);
}
}否则{
$entry->alias=“”;
}
未设置($entry->pages);
如果(!$entry->tid){
$entry->tid=“”;
}
$rows[]=(数组)$entry;
}
//为他们做一张桌子。
$header=array(t('path')、t('tid')、t('dfp tag machine_name')、t('amobee_as')、'');
$output.=''.t('如果填写了“tid”,则将在分类法页面和节点页面(其中主要术语id匹配)上使用amobee_as。如果填充了路径,则将在该路径上使用它。发生冲突时,路径将替代“tid”。

'; $output.=theme('table',array('header'=>$header,'rows'=>$rows)); } 否则{ drupal_set_消息(t(“尚未添加任何条目”); } 返回$output; }
Node模块是我第一次开始学习Drupal时学习它的最佳场所。。。
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;
}