You are here

新手,如何在我的模块上显示一个列表

Yes1000 的头像
Submitted by Yes1000 on 星期二, 2013-01-22 07:40

刚接触drupal没2周,作为学习自己新建了一个留言本模块,现在留言能提交和写进数据库,但如何在页面上显示个列表呢?

直接从库里读出来循环拼成HTML代码再赋值给#markup,这样做不太合适也太麻烦了吧?

 

附module文件全部代码:

function guestbook_menu() {

    $items['guestbook'] = array(
        'title' => '留言本',
        'page callback' => 'guestbook_show',
        'access callback' => TRUE,
        );
    return $items;
}

function guestbook_show(){
    $output["#markup"] = t('这里是留言本');
    $output["#markup"] .= drupal_render(drupal_get_form('guestbook_form'));
    return $output;
}

function guestbook_form(){
    $form['content'] = array(
        '#title' => t('留言内容'),
        '#type' => 'textfield',
        '#description' => t('请输入留言内容.'),
        );
    $form['submit'] = array(
        '#type' => 'submit',
        '#value' => t('提交.'),
        );
    return $form;
}

function guestbook_form_submit($form, &$form_state) {
    $content = $form_state['values']['content'];
    $time = time();
    $ip = $_SERVER["REMOTE_ADDR"];
    db_insert('guestbook')->fields(array('content'=>$content,'time'=>$time,'ip'=>$ip))->execute();
    drupal_set_message(t('Thanks for filling out the form, %name',
        array('%name' => $content)));
}

function guestbook_form_validate($form, &$form_state) {
    if ($form_state['values']['content'] == 'King Kong') {
        // We notify the form API that this field has failed validation.
        form_set_error('user_name',
            t('King Kong is not allowed to use this form.'));
    }
}

论坛:

Drupal版本: