duanaoshu1989 2016-06-22 11:06
浏览 36
已采纳

自定义URL在drupal 7中以自定义形式显示数据库中的内容

I created custom form in drupal 7 and data is saved in a table called "person" in database when i am trying to retrieve data in custom url data is displaying but when loading the form page "page not found" message is displaying not getting that form, if the code for retrieve data in custom url is commented then i get form page. this is the code which i used:

<?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>";
    }
}
  • 写回答

1条回答 默认 最新

  • douxing2652 2016-06-23 05:15
    关注

    $items = array(); this code should be come under the main function function form_test_menu() { the code should be like this

    function form_test_menu() {
     $items = array();
    

    no other modification it will work.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 echarts动画效果失效的问题。官网下载的例子。
  • ¥60 许可证msc licensing软件报错显示已有相同版本软件,但是下一步显示无法读取日志目录。
  • ¥15 Attention is all you need 的代码运行
  • ¥15 一个服务器已经有一个系统了如果用usb再装一个系统,原来的系统会被覆盖掉吗
  • ¥15 使用esm_msa1_t12_100M_UR50S蛋白质语言模型进行零样本预测时,终端显示出了sequence handled的进度条,但是并不出结果就自动终止回到命令提示行了是怎么回事:
  • ¥15 前置放大电路与功率放大电路相连放大倍数出现问题
  • ¥30 关于<main>标签页面跳转的问题
  • ¥80 部署运行web自动化项目
  • ¥15 腾讯云如何建立同一个项目中物模型之间的联系
  • ¥30 VMware 云桌面水印如何添加