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个小时了。
自己感觉还是比较好地实现了预定的功能,当然还有一些表单数据验证等如果有必要可以继续完善。
评论