欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2022-07-21 22:48:32
发布新闻,实现新闻页面静态化,真静态 add.php复制代码 代码如下: 添加新闻&n...

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


add.php

复制代码 代码如下:

<html>
 <head>添加新闻</head>

 <body>
   <form method="post" action="doadd.php">
     新闻标题:<input type="text" name="title" size="100"><br>
     新闻内容:<textarea name="content" cols="100" rows="25"></textarea><br>
     <input type="submit" name="提交">
   </form>
 </body>
</html>

config.php

复制代码 代码如下:

<?php
 define("host", "localhost");
 define("user", "justfan");
 define("pwd", "justfan");
 define("db", "justfandb");
 define("port", "3360");
?>

db_class.php

复制代码 代码如下:

<?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

复制代码 代码如下:

<?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 "<a href={$filename} target=_blank>查看刚才添加的新闻</a>";
echo "<a href='add.php'>添加新闻</a>";
?>

model.htm

复制代码 代码如下:

<!doctype html public "-//w3c//dtd html 4.01 transitional//en" "http://www.w3.org/tr/html4/loose.dtd">
<html>
<head>
 <meta http-equiv="content-type" content="text/html; charset=utf-8">
 <script type="text/javascript" src="//code.jquery.com/jquery-1.10.2.min.js"></script>
 <script type="text/javascript" src="../bootstrap/js/bootstrap.min.js"></script>
 <link rel="stylesheet" href="../bootstrap/css/bootstrap.min.css">
 <link rel="stylesheet" href="../bootstrap/css/common.css">
 <title>{title}</title>
</head>
  <body>

    <div class="container">
      <div class="jumbotron">
        <h1>{title}</h1>
        <p>{content}</p>
      </div>
    </div>
  </body>
</html>