PHP简易生成静态页面

类别:编程语言 点击:0 评论:0 推荐:

<?php
/*
 * 文件名:index.php
 */
require "conn.php";
$query = "select * from news order by datetime desc";
$result = mysql_query($query);
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=??????">
<title>NEWS</title>
</head>

<body>
<table width="500" border="1" align="center">
  <tr>
    <td>标题</td>
    <td width="200">发布时间</td>
  </tr>
  <?
  while($re = mysql_fetch_array($result)){
  ?>
  <tr>
    <td><a href="<?= $re["newsid"].".html"?>"><?= $re["title"]?></a></td>
    <td><?= $re["datetime"]?></td>
  </tr>
  <?
  }
  ?>
  <tr>
    <td>&nbsp;</td>
    <td><a href="addnews.php">添加新闻</a></td>
  </tr>
</table>
</body>
</html>
--------------------------------------------------------------------------
<?php
/*
文件名:AddNews.php
简易动态添加生成静态新闻页面
#
# 表的结构 `news`
#

CREATE TABLE `news` (
  `newsid` int(11) NOT NULL auto_increment,
  `title` varchar(100) NOT NULL default '',
  `content` text NOT NULL,
  `datetime` datetime NOT NULL default '0000-00-00 00:00:00',
  KEY `newsid` (`newsid`)
) TYPE=MyISAM AUTO_INCREMENT=11 ;

 */
if(isset($_POST["title"])){
    $title = $_POST["title"];
    $content = $_POST["content"];
    //定义模版文件的内容,可用其它方法
    $filecontent = '<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=??????">
<title>{title}</title>
</head>

<body>
<table width="500" border="1" align="center">
  <tr>
    <td align="center"><strong>{title}</strong></td>
  </tr>
  <tr>
    <td>{content}</td>
  </tr>
  <tr>
    <td align="right">{datetime}</td>
  </tr>
</table>
</body>
</html>
';
    $datetime = date("Y-m-d H:i:s");
    $query = "insert into news values('','".$title."','".$content."','".$datetime."')";
    require "conn.php";//连接数据库
    $result = mysql_query($query) or die(mysql_error());
    if($result){
        $id = mysql_insert_id();
        $filename = $id.".html";
        if($fp = fopen($filename, "w")){//新建文件,成功后添加内容
            $filecontent = str_replace("{title}", $title, $filecontent);
            $filecontent = str_replace("{content}", $content, $filecontent);
            $filecontent = str_replace("{datetime}", $datetime, $filecontent);
            if(!fwrite($fp, $filecontent)){//把内容写入文件
                $query = "delete from news where newsid=".$id;
                $result = mysql_query($query) or die(mysql_error());
                fclose($fp);
                unlink($filename);
                echo "<script>alert('Add news failed!');location.href='index.php';</script>";
                exit;
            }else {
                echo "<script>alert('Add news successed!');location.href='index.php';</script>";
            }
        }
    }
}
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=??????">
<title>添加</title>
</head>

<body>
<form name="form1" method="post" action="">
  <table width="500" border="1" align="center">
    <tr>
      <td>标题</td>
      <td><input name="title" type="text" id="title" size="40"></td>
    </tr>
    <tr>
      <td>内容</td>
      <td><textarea name="content" cols="45" rows="6" id="content"></textarea></td>
    </tr>
    <tr>
      <td>&nbsp;</td>
      <td><input type="submit" name="Submit" value="提交"></td>
    </tr>
  </table>
</form>
</body>
</html>

本文地址:http://com.8s8s.com/it/it27434.htm