0
我在Drupal 7中創建自定義表單,數據保存在數據庫中的一個名爲「person」的表中,當我試圖檢索自定義URL數據中的數據時顯示但當加載表單頁面「找不到頁面」消息顯示沒有得到該表單時,如果用於檢索自定義url中的數據的代碼被評論,那麼我會得到表單頁面。 這是我使用的代碼:自定義URL顯示內容從數據庫中的自定義形式在Drupal 7
<?php
/**
* @file
* Provides a custom form, data are saved in database and can retrieve data in table format in custom url.
*/
//Implementation of hook_menu()
function form_test_menu() {
$items['formtest'] = array(
'title' => 'Form Test',
'page callback' => 'drupal_get_form',
'page arguments' => array('form_test_form'),
'access callback' => TRUE,
);
$items = array();
$items['results'] = array(// change to the url you want
'title' => 'results',
'type' => MENU_CALLBACK,
'page callback' => 'results',
'access arguments' => array('access content'),
);
return $items;
}
//function to describe field in the form
function form_test_form($form,&$form_submit) {
$form['name'] = array(
'#title' => t('name'),
'#type' => 'textfield',
'#size' => 20,
'#maxlength' => 20,
'#required' => TRUE,
'#default_value' => 'Enter Your Name',//make this field required
);
$form['address']['email'] = array(
'#type' => 'textfield',
'#title' => t('E-mail'),
'#required' => TRUE,
'#maxlength' => 255,
);
$form['price'] = array(
'#type' => 'textfield', //you can find a list of available types in the form api
'#title' => 'What is Your Price?',
'#size' => 10,
'#maxlength' => 10,
'#required' => TRUE, //make this field required
);
$form['attending'] = array(
'#type' => 'radios',
'#title' => t('Will you be attending?'),
'#options' => array(
'1' => t('Yes'),
'0' => t('No'),
),
'#required' => TRUE,
'#default_value' => isset($attending) ? $attending : NULL,
);
$form['submit'] = array(
'#value' => 'Submit',
'#type' => 'submit',
);
return $form;
}
//validation code for integer
function form_test_form_validate($form, &$form_state) {
if (!($form_state['values']['price'] > 0)){
form_set_error('price', t('Price must be a positive number.'));
}
}
//inserting data into database
drupal_write_record('person', $data);
function form_test_form_submit($form, &$form_state) {
$id = db_insert('person')
->fields(array(
'name' => $form_state['values']['name'],
'email' => $form_state['values']['email'],
'price' => $form_state['values']['price'],
'attending' => $form_state['values']['attending'],
// 'ios' => $form_state['values']['ios'],
))
->execute();
drupal_set_message(t('data saved successfully.'));
}
//Retrieving data from database
function results() {
print "CUSTOM TABLE CONTENT";
print "<br /> ";
print "<br /> ";
$query = db_select('person', 'u');
$query->fields('u', array('name')); //mention the field that you want to display
$query->fields('u', array('email'));
$query->fields('u', array('price'));
$query->fields('u', array('attending'));
$result = $query->execute();
while($record = $result->fetchAssoc()) { //while loop mention how the data want to display
echo "<li>";
print_r($record['name']);
print " ";
print_r($record['email']);
print " ";
print_r($record['price']);
print " ";
print_r($record['attending']);
echo "</li>";
}
}