我有一組項目,我可以使用表單進行編輯和刪除。現在,所有itms都以字段集的形式顯示在列表中。Drupal刪除/更新數據庫中的錯誤行
$venues = db_query('SELECT vid, name, address, postcode, city FROM venues v WHERE v.uid = :uid', array(':uid' => $user->uid));
if($venues->rowCount() != 0) {
foreach($venues as $venue) {
$page['venue_editing_form'][] = drupal_get_form('venue_editor_form', $venue->vid, $venue->name, $venue->address, $venue->postcode, $venue->city);
}
}
然後形式與此函數創建:
function venue_editor_form($form, &$form_state, $vid, $vname, $vadd, $vpostc, $vcity) {
$form['#prefix'] = '<div class="vedit">';
$form['#suffix'] = '</div>';
$form['edit_fieldset_'.$vname] = array(
'#type' => 'fieldset',
'#title' => t($vname),
'#collapsible' => TRUE,
'#collapsed' => TRUE,
'#attributes' => array('class' => array('venue-edit')),
);
$form['edit_fieldset_'.$vname]['venue_name'] = array(
'#type' => 'textfield',
'#title' => t('Venue name:'),
'#description' => t('Enter the venue name.'),
'#default_value' => $vname,
'#attributes' => array('onfocus' => 'this.value=""')
);
$form['edit_fieldset_'.$vname]['address'] = array(
'#type' => 'textfield',
'#title' => t('Address:'),
'#description' => t('The address of this venue.'),
'#default_value' => $vadd,
'#attributes' => array('onfocus' => 'this.value=""')
);
$form['edit_fieldset_'.$vname]['postcode'] = array(
'#type' => 'textfield',
'#title' => t('Postcode'),
'#description' => t('Enter the postcode for this event, so planB can position it on the map'),
'#default_value' => $vpostc,
'#attributes' => array('onfocus' => 'this.value=""', 'onblur' => 'getLatLong(this)')
);
$form['edit_fieldset_'.$vname]['city'] = array(
'#type' => 'textfield',
'#title' => t('City'),
'#description' => t('The city this venue is in.'),
'#default_value' => $vcity,
'#attributes' => array('onfocus' => 'this.value=""')
);
$form['edit_fieldset_'.$vname]['edit_submit'] = array(
'#type' => 'submit',
'#value' => t('Submit changes'),
'#validate' => array('venue_creation_form_validate')
);
$form['edit_fieldset_'.$vname]['delete_venue'] = array(
'#type' => 'submit',
'#value' => t('Delete venue'),
'#submit' => array('venue_form_delete')
);
$form['edit_fieldset_'.$vname]['venueid'] = array(
'#type' => 'value',
'#value' => $vid
);
return $form;
}
並提交處理程序之一:
function venue_form_delete($form, &$form_state) {
$deleted = db_delete('venues')
->condition('vid', $form['#venueid'], '=')
->execute();
drupal_set_message('Venue deleted successfully.');
}
的字段集用這種方法創建現在,問題是無論按下哪個場所的刪除按鈕,Drupal都會刪除第一個列表中的那些人。 $form['#venueid']
總是設置爲提交處理程序列表中的第一個場所,但出於某種原因,如果在生成表單時打印消息,則每個表單都應有不同的ID。
有沒有人有過這個問題?
vid從哪裏來?你的意思是NID嗎? – SpaceBeers 2012-02-14 07:32:46
vid是它來自數據庫查詢以獲取所有場所的詳細信息的場所ID。然後,我使用一個foreach循環遍歷所有結果和drupal_get_form('venue_editor_form',$ venue-> vid ...) – KerrM 2012-02-14 13:43:10
重讀這個問題,難道是你有一個表單,有多個字段集和多個Submit /刪除按鈕(即每個場地一個字段集)? – 2012-02-14 17:23:54