栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > PHP

php生成静态页面的简单示例

PHP 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

php生成静态页面的简单示例

发布新闻,实现新闻页面静态化,真静态


add.php
复制代码 代码如下:

 添加新闻

 
  
 

config.php
复制代码 代码如下:
 define("HOST", "localhost");
 define("USER", "justfan");
 define("PWD", "justfan");
 define("DB", "justfanDB");
 define("PORT", "3360");
?>

DB_class.php
复制代码 代码如下:
 class DB
 {
  private $host = '';
  private $uname = '';
  private $pwd = '';
  private $port = '';
  private $db = '';
     public static $instance = null;

  private function __construct($host , $uname , $pwd , $port , $db)
  {
   $this->host = $host;
   $this->uname = $uname;
   $this->port = $port;
   $this->pwd = $pwd;
   $this->db = $db;

   mysql_connect($host,$uname,$pwd);
   mysql_select_db($this->db);
  }

  public static function Instance()
  {
   if(Db::$instance==null){
    include 'config.php';
    return Db::$instance = new DB(HOST, USER, PWD, PORT, DB);
   } 
   else
    return Db::$instance;
  }

  public function query($sql)
  {
   mysql_query("SET NAMES UTF8");
   $query = mysql_query($sql) or die($sql." error");
   if(!$query) return false;
   else   return $query;
  }

  
  public function getAll($sql)
  {
   $query = $this->query($sql);
   if($query)
   {
    while($ret = mysql_fetch_assoc($query))
    {
     $result[] = $ret;
    }
   }   
   return $result;
  }

  
 }
?>

doadd.php
复制代码 代码如下:
include 'DB_class.php';
$db = DB::Instance();

$title=$_POST["title"];
$content=$_POST["content"];

$num = uniqid();
$houzui=".html";
$filename=date('Ymd').'/'.$num.$houzui;

$sql="insert into news(title,content,path) values ('{$title}' , '{$content}' , '{$filename}')";
$query = $db->query($sql);

$fp=fopen("model.htm","r");
$str=fread($fp,filesize("model.htm"));
$str=str_replace("{title}",$title,$str);
$str=str_replace("{content}",$content,$str);
fclose($fp);

$dir = dirname($filename);
if(!is_dir($dir)){
 mkdir($dir);
}

$handle=fopen($filename,"w");
fwrite($handle,$str);
fclose($handle);

 

echo "查看刚才添加的新闻";
echo "添加新闻";
?>

model.htm
复制代码 代码如下:



 
 
 
 
 
 {title}

 

   
     
        {title}
       

{content}


     
   
 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/48339.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号