简单教程

  1. 创建目录结构:

    • 在你的 Web 服务器中创建一个名为 Website 的目录。
    • 将 Smarty 的 libs 目录复制到 Website 目录中。
    • Website 目录下创建两个目录:compiletemplates
  2. 创建文件:

    • Website 目录下,创建两个文件:index.phpWeb.class.php,并确保它们为空。
  3. Web.class.php 文件内容:

    <?php
    class Web {
        function db_connect($db_host, $db_user, $db_pass, $db_db) {
            $this->link = @mysql_connect($db_host, $db_user, $db_pass) 
                or die("无法连接到数据库");
            @mysql_select_db($this->link, $db_db) 
                or die("已连接,但无法选择数据库");
        }
    
        function db_query($sql) {
            return @mysql_query($this->link, $sql);
        }
    
        function db_close() {
            mysql_close($this->link);
        }
    }
    ?>
    
  4. index.php 文件内容:

    <?php
    error_reporting(E_ALL);
    $db = array(
        "host" => "localhost",
        "user" => "root",
        "pass" => "",
        "db" => "database"
    );
    $tables['content'] = "test_content";
    
    require_once("Web.class.php");
    $web = new Web();
    $web->db_connect($db['host'], $db['user'], $db['pass'], $db['db']);
    
    require_once("libs/Smarty.inc.php");
    $smarty = new Smarty();
    $smarty->template_dir = "templates";
    $smarty->compile_dir = "compile";
    
    if (isset($_GET['content_id']) && is_numeric($_GET['content_id'])) {
        $sql = "SELECT * FROM {$tables['content']} WHERE content_id = '{$_GET['content_id']}' LIMIT 1";
        $result = $web->db_query($sql);
        $rows = array();
        while ($row = mysql_fetch_assoc($result)) {
            $rows[] = $row;
        }
        if (count($rows) == 1) {
            $smarty->assign("content_found", true);
            $smarty->assign("content_content", $rows[0]);
        } else {
            $smarty->assign("content_found", false);
        }
        $smarty->assign("section", "content");
    } else {
        $sql = "SELECT content_title, content_date, content_position, content_id FROM {$tables['content']} ORDER by content_position asc";
        $result = $web->db_query($sql);
        $rows = array();
        while ($row = mysql_fetch_assoc($result)) {
            $rows[] = $row;
        }
        $smarty->assign("section", "home");
        $smarty->assign("content_content", $rows);
    }
    
    $smarty->display("index.tpl");
    $web->db_close();
    ?>
    
  5. 创建模板文件:

    • 进入 templates 目录,创建一个名为 index.tpl 的文件,并确保它为空。
    • 在文件中添加 HTML 代码,并在内容部分使用 Smarty 语法,例如:
    {if $section == "home"}
        <ul>
        {foreach from=$content_content item="content_item"}
            <li><a href="./?content_id={$content_item.content_id}">{$content_item.content_title}</a></li>
        {/foreach}
        </ul>
    {elseif $section == "content"}
        <div><h1>{$content_content.content_title}</h1></div>
        <div>{$content_content.content_content}</div>
    {else}
        Sorry, there is no such page here!
    {/if}
    
  6. 创建 MySQL 数据表: 使用以下 SQL 语句创建 test_content 表:

    CREATE TABLE `test_content` (
        `content_id` INT(11) NOT NULL AUTO_INCREMENT, 
        `content_title` VARCHAR(255) NOT NULL, 
        `content_date` DATETIME NOT NULL, 
        `content_content` TEXT NOT NULL, 
        `content_position` INT(11) NOT NULL,
        PRIMARY KEY (`content_id`)
    ) TYPE = myisam;
    

    或者对于较新的 MySQL 版本,使用以下 SQL 语句:

    CREATE TABLE `test_content` (
        `content_id` INT(11) NOT NULL AUTO_INCREMENT, 
        `content_title` VARCHAR(255) NOT NULL, 
        `content_date` DATETIME NOT NULL, 
        `content_content` TEXT NOT NULL, 
        `content_position` INT(11) NOT NULL,
        PRIMARY KEY (`content_id`)
    );
    
  7. 设置数据库:

    • 修改 index.php 中的 $db 数组,确保与自己的数据库设置一致。
    • 使用 MySQL 客户端(如 phpMyAdmin 或 MySQL 命令行工具),向 test_content 表中添加几行数据(至少添加三行)。
  8. 运行网站:

    • 在浏览器中访问 Website 目录。如果你没有将其上传到服务器,可以在本地设置 Web 服务器来查看效果。
  9. 常见问题:

    • 如果遇到问题,可以访问 IRC 频道 irc://irc.freenode.org/php 寻求帮助,或联系作者。

参考资料:

最后修改: 2025年01月10日 星期五 02:06