添加数据保存到数据库以及生成html页面

<?php
//接收前台的值
$title=$_POST["title"];
$catagory=$_POST["catagory"];
$content=$_POST["content"];

//写入到数据库中
$connAdd=mysql_connect("localhost", "", "");
@mysql_query("SET character_set_connection=utf8, character_set_results=utf8, character_set_client=binary",$connAdd);
$con=mysql_select_db("mzdly",$connAdd);
$sql="INSERT INTO article(title,catagory,content,sendDate) values('$title','$catagory','$content',now())";
mysql_query($sql);
$inset_id=mysql_insert_id();
mysql_close($conn);
//生成html页面
$top_file_path="../common/top.html";
$bottom_file_path="../common/bottom.html";
$article_file_path="../templ/article-t.html";
//读取头部和底部模板
$top_str= file_get_contents($top_file_path);
$bottom_str=file_get_contents($bottom_file_path);
//读取文章页模板
$article_str=file_get_contents($article_file_path);
//替换文章页内容
if(strpos($article_str,'{hoz:top_html_template}')!=false)
{
$html_top_complete= str_replace('{hoz:top_html_template}',$top_str,$article_str);
}
if(strpos($html_top_complete,'{hoz:bottom_html_template}')!=false)
{
$html_bottom_complete= str_replace('{hoz:bottom_html_template}',$bottom_str,$html_top_complete);
}
if(strpos($html_bottom_complete,'{hoz:title}')!=false)
{
$html_title_complete= str_replace('{hoz:title}',$title,$html_bottom_complete);
}
if(strpos($html_title_complete,'{hoz:create_time}')!=false)
{
$html_createTime_complete= str_replace('{hoz:create_time}',date('Y-m-d H:i:s',time()),$html_title_complete);
}
if(strpos($html_createTime_complete,'{hoz:content}')!=false)
{
$html_content_complete= str_replace('{hoz:content}',$content,$html_createTime_complete);
}
//写入到指定catagory下
$arr=array(
"旅游专区"=>"wxpp",
"海产品专区"=>"wxyw",
"纪念品专区"=>"mbby",
"游客地带"=>"jswd",
"游客须知"=>"wxsb",
"联系我们"=>"lxwm"
);
//遍历指定文件夹下的所有文件获得值最大的文件名,然后加1
$hostdir=dirname(dirname(__FILE__))."\\".$arr[$catagory]."\\article\\";
$filesnames = scandir($hostdir);
$num=count($filesnames)-2;
$arr_file_name=array();
if($num>0)
{
foreach($filesnames as $name)
{
preg_match_all("/\d+/",$name,$out);
$arr_file_name[]=$out[0][0];
}
$article_id=max($arr_file_name)+1;
}else{
$article_id=1;
}
file_put_contents("..\\".$arr[$catagory]."\\article\\".$article_id.".html",$html_content_complete);
//返回前端
if($inset_id!=0)
{
$msg=array(
'content'=>'yes',
);
echo json_encode($msg);
}else{
$msg=array(
'content'=>'no',
);
echo json_encode($msg);
}

?>

你可能感兴趣的:(html)