新聞文件以文本文件.txt的格式存放在一個固定的目錄下,新聞的發布可以自動完成,該過程由兩部分組成: 第一是新聞的顯示,由shownews.php腳本實現,代碼如下:
- 〈table border="0" width="90%"〉
- 〈?php
- //在新聞文件中的第一行放新聞的標題
- //新聞文件必須是文本文件(.txt)
- $newspath = "./news/"; // 修改新聞文件存放的目錄
- $newsfile = array();
- $hd = dir($newspath);
- while( $filename = $hd-〉read() ) {
- $s=strtolower($filename);
- if (strstr($s,".txt")) {
- $lastchanged=filemtime($newspath.$filename);
- $newsfile[$filename] = $lastchanged;
- }
- }
- arsort($newsfile);
- for(reset($newsfile); $key = key($newsfile); next($newsfile)) {
- print "〈tr〉〈td〉n";
- $fa = file($newspath.$key);
- $s=trim($fa[0]);
- $s=htmlspecialchars($s);
- $lk=strlen($key);
- $a=substr($key,0,$lk-4);
- $s="〈a href="./pubnews.php?id=".$a."" target=_blank〉".$s."〈/a〉";
- print $s." n";
- print "(".date("Y年m月d日 - H:i:s",$newsfile[$key]).")
- n";
- print "〈/td〉〈/tr〉";
- }
- $hd-〉close();
- ?〉
- 〈/table>
在顯示新聞的地方放入代碼:
- <?php
- require "./shownews.php";
- ?>
第二部分為新聞的發布,由pupnews.php腳本實現,代碼如下:
- 〈?php
- if ($id=="")
- {
- Header("Location: ./shownews.php");
- }
- ?〉
- 〈html〉
- 〈head〉
- 〈meta content="chenqiang" name=Author〉
- 〈?php
- $filename="./news/".$id.".txt";
- $fa=file($filename);
- $n=count($fa);
- $s=trim($fa[0]);
- $s=htmlspecialchars($s);
- $t=" - news by waterwall";
- print "〈title〉".$s.$t."〈/title〉n";
- ?〉
- 〈/head〉
- 〈body〉
- 〈?php
- //輸出文本標題
- print "〈blockquote〉n";
- print "〈b〉〈center〉".$s."n";
- print "〈/center〉〈/b〉
- 〈p〉n";
- //輸出文本正文
- for ($i=1;$i〈$n;$i+=1)
- {
- $s=chop($fa[$i]);
- $s=htmlspecialchars($s);
- $s=trim($s);
- print " ".$s."
- n";
- }
- print "〈/p〉〈/blockquote〉n";
- ?〉
- 〈/body〉
- 〈/html〉
用數據庫也可實現,這只是文件形式的實現方式。
新聞熱點
疑難解答