";
}else {
echo "您所查看的新闻不存在";
}
?>
newsAction.php(添加、修改新闻请求处理页面)
返回列表";
}else {
//事务回滚
mysql_query("ROLLBACK");
}
header("Location:news_list.php");
}else {
//事务回滚
mysql_query("ROLLBACK");
}
}else if ($oper == 'update') {
//接收修改的id
$id = $_POST['id'];
//开始事务
mysql_query("BEGIN");
$sql = "update news set title='$title', content='$content' where id=$id";
if (mysql_query($sql)) {
//修改数据库成功
$html_filename = "newsfile/news_id". $id .".html";
unlink($html_filename);
//创建HTML文件
createHTML($html_filename, $title, $content);
//事务提交
mysql_query("COMMIT");
echo "更新到数据库并重新创建html文件返回列表";
}
}
//关闭数据库
mysql_close($conn);
/**
* 替换函数
*/
function replace($row, $title, $content) {
$row = str_replace("%title%", $title, $row);
$row = str_replace("%content%", $content, $row);
return $row;
}
/**
* 创建HTML文件
*/
function createHTML($file, $title, $content) {
//创建HTML文件
$fp_tmp = fopen("template.tpl", "r");
$fp_html_file = fopen($file, "w");
//逐行读取模版,替换内容,写入静态页面
while (!feof($fp_tmp)) {
$row = fgets($fp_tmp);
//替换内容
$new_row = replace($row, $title, $content);
//把替换后的内容写进静态页面
fputs($fp_html_file, $new_row);
}
//关闭文件
fclose($fp_tmp);
fclose($fp_html_file);
}
?>
template.tpl(新闻展示页面的模版)
%title%
%title%
%content%
本文原创发布php中文网,转载请注明出处,感谢您的尊重!