PHP簡易產生靜態頁面

來源:互聯網
上載者:User

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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.