Drupal中以前都是用node方式來呈現數據,content type設置好字段後,有現成的辦法來實現内容呈現和編輯。但後來比較多用到自建的數據表,呈現一般還是用Views來實現,也可以用SQL語句來實現,而修改數據表中内容最開始用阿裡雲RDS後台,後來改用自己寫的form展示和輸入修改提交。
昨天同事又說到另外一個類似的需求,我花了幾個小時來做,這次幹脆把以前呈現表單、提交結果兩個頁面合并為一個頁面,通過判斷這個頁面的$_GET和$_POST參數來做适當的操作,在Drupal的自定義模塊中的PHP程序代碼如下:
/**
* 函數:專題編輯頁面
* 輸入:id
* 輸出:表單、提交結果
* 數據表:topic
* 字段:id, name, note, title, keywords, description
*/
function edit_topic() {
$output = '';
if (isset($_POST['id'])) {//有POST數據,進行更新處理
//獲取POST數據
$id = $_POST['id'];
$name = $_POST["name"];
$note = $_POST["note"];
$title = $_POST["title"];
$keywords = $_POST["keywords"];
$description = $_POST["description"];
//組成數據
$topic_array['id'] = $id;
$topic_array['name'] = $name;
$topic_array['note'] = $note;
$topic_array['title'] = $title;
$topic_array['keywords'] = $keywords;
$topic_array['description'] = $description;
//調用更新函數
$update_topic_num = update_topic($topic_array);
if ($update_topic_num == 0) {
drupal_set_message("未更新數據",'warning');
} else {
drupal_set_message("更新一條數據",'status');
}
}
if (isset($_GET['id'])) {//有GET數據,展示編輯表單
$id = $_GET['id'];
//獲取數據表内容到數組
$get_topic_array = get_topic($id);
//獲取各個字段内容
$name = $get_topic_array['name'];
$note = $get_topic_array['note'];
$title = $get_topic_array['title'];
$keywords = $get_topic_array['keywords'];
$description = $get_topic_array['description'];
//組成表單
$form = "
<form class='topic_edit_form' action='' method='post'>
<div class='topic_input_wrap'>
<label>專題ID (此處不可修改): </label>
<input type='text' name='id' value='$id' disabled='true' />
</div>
<div class='topic_input_wrap'>
<label>專題名稱: </label>
<input type='text' name='name' value='$name' size='80' />
</div>
<div class='topic_input_wrap'>
<label>備注: </label>
<input type='text' name='note' value='$note' size='80' />
</div>
<div class='topic_input_wrap'>
<label>網頁标題: </label>
<input type='text' name='title' value='$title' size='80' />
</div>
<div class='topic_input_wrap'>
<label>網頁關鍵詞: </label>
<input type='text' name='keywords' value='$keywords' size='80' />
</div>
<div class='topic_input_wrap'>
<label>網頁描述: </label>
<textarea cols='80' rows='5' name='description'>$description</textarea>
</div>
<div class='topic_input_wrap submit_wrap'>
<input type='submit' value='提交修改' />
</div>
</form>
";
$output .= str_replace("\n","<br />\n",$form);
} else {
drupal_set_message("無id參數。",'error');
}
return $output;
}
/**
* 函數:獲取專題數據
* 輸入:專題id
* 輸出:各字段組成的數組
*/
function get_topic($id) {
$database = 'center';
db_set_active($database);
$table = 'topic';
//調用數據庫查詢語句
$result = db_select($table)
->fields($table, array('id','name','note','title','keywords','description'))
->condition('id',$id,'=')
->execute()
->fetchAssoc();
db_set_active();
//從result數組轉為output數組,實際使用的時候可能key不一樣,下面的例子是key一樣的
$output = array(
'id' => $result['id'],
'name' => $result['name'],
'note' => $result['note'],
'title' => $result['title'],
'keywords' => $result['keywords'],
'description' => $result['description'],
);
return $output;
}
/**
* 函數:更新專題數據
* 輸入:專題數組
* 輸出:更新條數(0表示未更新,1表示已更新)
*/
function update_topic($topic_array) {
$database = 'center';
db_set_active($database);
$table = 'topic';
//從輸入數組讀出變量,實際使用的時候可能需要進行一些變換
$id = $topic_array['id'];
$name = $topic_array['name'];
$note = $topic_array['note'];
$title = $topic_array['title'];
$keywords = $topic_array['keywords'];
$description = $topic_array['description'];
//調用數據庫更新的SQL語句
$num_updated = db_update($table)
->fields(array(
'name' => $name,
'note' => $note,
'title' => $title,
'keywords' => $keywords,
'description' => $description,
))
->condition('id', $id, '=')
->execute();
db_set_active();
return $num_updated;
}
本來和同事說2個小時就可以把程序寫好的,實際上用了3個多小時,加上後面修改Views來展示以及第二天完善以上程序的一些時間,也是用了4個多小時。這次記錄下來以後,下次應該隻需要2個小時了。
自己感覺還是比較好地實現了預定的功能,當然還有一些表單數據驗證等如果有必要可以繼續完善。
评论