首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> 網頁程式設計 >> PHP 程式設計 >> PHP 綜合知識

PHP 實例教學(一):輕鬆打造 Webzine 發佈系統 - 基礎架構與模板混合技巧

     編輯:PHP 綜合知識

專案簡介

Webzine 是一個極簡線上雜誌系統,包含前台閱讀與後台投稿,總代碼不超 3KB。展示 PHP 混合 HTML 的靈活語法與基本檔案儲存機制。

PHP 混合 HTML 核心技巧

PHP 允許在流程控制結構中穿插 HTML,例如:

<table border=2>
<tr><th>Name</th><th>Days</th></tr>
<?php
for ($i = 0; $i < 12; $i++) {
?>
<tr><td><?php echo $names[$i]; ?></td>
<td><?php echo $days[$i]; ?></td></tr>
<?php
}
?>
</table>

這特性讓模板邏輯與呈現自然融合,無需模板引擎即可開發動態頁面。

Webzine 發佈模組架構

  • 主題選單:各主題對應一個文字檔(如 TradeShow.txt),每行一篇文章摘要,以 ^ 分隔欄位。
  • 文章內容:獨立文字檔,檔名為文章編號(如 33.txt)。
  • 首頁:讀取所有主題檔,顯示最新文章摘要與連結。

主題選單檔案格式

33^Great New Products This Year^/images/proddemo.jpg^Thursday's product demo ...
12^Opening Event Well Attended^/images/opnfoto.jpg^Ticket sales to the ...
5^Trade Show Opens^/images/tradelogo.gif^The Fourth Annual Trade Show ...

欄位:文章編號^標題^圖片URL^摘要

前台顯示邏輯(節錄)

<?php
$stories = file("TradeShow.txt");
$numstories = count($stories);

for ($i = 0; $i < $numstories; $i++) {
    $storyinfo = split("\^", $stories[$i]);
    $storynum  = $storyinfo[0];
    $storydesc = $storyinfo[1];
    $storyimg  = $storyinfo[2];
    $dtext     = $storyinfo[3];

    $url = "<a href=\"index.php?topic=$topic&story=$storynum\">$storydesc</a><br>";
    if ($i < 10) {
        $url = "<h2>$url</h2>";
        if ($storyimg != "") {
            $align = ($i % 2 == 1) ? "right" : "left";
            $url = "<p><img align=$align src=\"$storyimg\"></p>\n$url";
        }
        $url = "<hr>$url";
    } else {
        $url = "<h3>$url</h3>";
    }
    echo $url;
    echo "<p>$dtext</p>\n";
}
?>

安全處理

投稿內容經 htmlspecialchars() 轉義,僅保留少數安全標籤,防止惡意 HTML/JS 執行。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved