0

我正在尝试创建一个从选择框中保存值的块。但是,重新加载后, variable_get 不会返回保存的值......所以 variable_set 接缝不起作用。

我在这里做错了什么?(Drupal 6)

function get_courses(){
   global $user;     
    $my_items_sql = 'SELECT course_node.uid, ';
                     //get the course nid
                     $my_items_sql .='course_node.nid as course_nid, ';
                     //get the course title
                     $my_items_sql .='course_node.title as course_title ';
                     $my_items_sql .=' from node as course_node where course_node.type="course" and course_node.uid = "'.$user->uid.'"';
                     $my_items_sql .=' order by course_node.nid; ';
                     $my_items_data= db_query($my_items_sql);

                     $my_courses = array();
                     while ($row = db_fetch_array($my_items_data)) {

                            //$course_node = node_load($row["course_nid"]);
                            $my_courses[$row["course_nid"]]=$row["course_title"];
                     }
                     //$variables['courses']=$my_courses;
                     //drupal_add_js(array('courses'=>$my_courses), "setting");

         return $my_courses;
}
function front_page_block($op='list',$delta = 0, $edit = array()) {

    $course_options = array();
    $courses = get_courses();
    $tmp = $courses;
    $first_course = $tmp;
    reset($first_course );
    $first_course_nid = key($first_course);

    $first_course_nid = key($first_course);
    switch($op){
        case 'list':
            $blocks[0]['info']= t('Course Data Loader');
            $blocks[0]['cache']= BLOCK_NO_CACHE;
            return $blocks;
        case 'configure':
            $form['course_to_display'] = array(
            '#type' => 'select',
            '#description' => t('Display flashcards from which deck on the front page?'),
            '#options' => $courses,
            '#default_value'=>variable_get('front_page_deck_to_load', $first_course_nid)
            );
            return $form;
        case 'save': 
            variable_set('front_page_deck_to_load', 
                $edit['front_page_deck_to_load']);

            break;


    }
4

1 回答 1

1

首先检查,在调用之前为$edit['front_page_deck_to_load']设置了什么值

variable_set('front_page_deck_to_load', $edit['front_page_deck_to_load']); 功能。

如果$edit['front_page_deck_to_load']的值设置正确,则 variable_set 正在工作。

现在的问题是,$ form['course_to_display']'#default_value'设置不正确。请修改get_courses()函数如下,然后重试:

function get_courses(){
   global $user;     
    $my_items_sql = 'SELECT course_node.uid, ';
                     //get the course nid
                     $my_items_sql .='course_node.nid as course_nid, ';
                     //get the course title
                     $my_items_sql .='course_node.title as course_title ';
                     $my_items_sql .=' from node as course_node where course_node.type="course" and course_node.uid = "'.$user->uid.'"';
                     $my_items_sql .=' order by course_node.nid; ';
                     $my_items_data= db_query($my_items_sql);

                     $my_courses = array();
                     while ($row = db_fetch_array($my_items_data)) {

                            //$course_node = node_load($row["course_nid"]);
                            $my_courses[$row["course_title"]]=$row["course_title"];
                     }
                     //$variables['courses']=$my_courses;
                     //drupal_add_js(array('courses'=>$my_courses), "setting");

         return $my_courses;
}

仅供参考:请关注 http://drupal.org/node/240783 上的评论# 1

于 2013-03-28T06:05:52.320 回答