发布新闻,实现新闻页面静态化,真静态
add.php
<BR> 添加新闻<br><br> <body><BR> <BR> 新闻标题:<br><BR> 新闻内容:<textarea name="content" cols="100" rows="25"></textarea><br><BR> <BR> <BR> <BR><BR>
config.php
<?php <BR> define("HOST", "localhost");<BR> define("USER", "justfan");<BR> define("PWD", "justfan");<BR> define("DB", "justfanDB");<BR> define("PORT", "3360");<BR>?><BR>
DB_class.php
<?php<BR> class DB<BR> {<BR> private $host = '';<BR> private $uname = '';<BR> private $pwd = '';<BR> private $port = '';<BR> private $db = '';<BR><mark style="color:transparent">本%文来源gaodaimacom搞#^代%!码网@</mark>搞代gaodaima码 public static $instance = null; <br><br> private function __construct($host , $uname , $pwd , $port , $db)<BR> {<BR> $this->host = $host;<BR> $this->uname = $uname;<BR> $this->port = $port;<BR> $this->pwd = $pwd;<BR> $this->db = $db;<br><br> mysql_connect($host,$uname,$pwd);<BR> mysql_select_db($this->db);<BR> }<br><br> public static function Instance()<BR> {<BR> if(Db::$instance==null){<BR> include 'config.php';<BR> return Db::$instance = new DB(HOST, USER, PWD, PORT, DB);<BR> } <BR> else <BR> return Db::$instance;<BR> }<br><br> public function query($sql)<BR> {<BR> mysql_query("SET NAMES UTF8");<BR> $query = mysql_query($sql) or die($sql." error");<BR> if(!$query) return false;<BR> else return $query;<BR> }<br><br> <BR> public function getAll($sql)<BR> {<BR> $query = $this->query($sql);<BR> if($query)<BR> {<BR> while($ret = mysql_fetch_assoc($query))<BR> {<BR> $result[] = $ret;<BR> }<BR> } <BR> return $result;<BR> }<br><br> <BR> }<BR>?><BR>
doadd.php
<?php <BR>include 'DB_class.php';<BR>$db = DB::Instance();</P><P>$title=$_POST["title"];<BR>$content=$_POST["content"]; </P><P>$num = uniqid();<BR>$houzui=".html";<BR>$filename=date('Ymd').'/'.$num.$houzui;</P><P>$sql="insert into news(title,content,path) values ('{$title}' , '{$content}' , '{$filename}')";<BR>$query = $db->query($sql);</P><P>$fp=fopen("model.htm","r");<BR>$str=fread($fp,filesize("model.htm"));<BR>$str=str_replace("{title}",$title,$str);<BR>$str=str_replace("{content}",$content,$str);<BR>fclose($fp);</P><P>$dir = dirname($filename);<BR>if(!is_dir($dir)){<BR> mkdir($dir);<BR>}</P><P>$handle=fopen($filename,"w"); <BR>fwrite($handle,$str); <BR>fclose($handle);</P><P> </P><P>echo "查看刚才添加的新闻";<BR>echo "添加新闻";<BR>?><BR>
model.htm
<BR><BR><BR> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><BR> <script type="text/javascript" src="//code.jquery.com/jquery-1.10.2.min.js"></script><BR> <script type="text/javascript" src="../bootstrap/js/bootstrap.min.js"></script><BR> <link rel="stylesheet" href="../bootstrap/css/bootstrap.min.css"><BR> <link rel="stylesheet" href="../bootstrap/css/common.css"><BR> <title>{title}</title><BR><BR> <body></P><P> <div class="container"><BR> <div class="jumbotron"><BR> <h1>{title}</h1><BR> <p>{content}</p><BR>