:::

8-1 index.php

<?php
// 引入頁首
require_once 'header.php';
// include_once 'header.php';

/*****************主內容區***********************/
// $smarty->assign('now' 相當於php $now
$smarty->assign('now', date("Y-m-d"));

function list_all()
{
    global $smarty;
    // 初始化
    $content                = [];
    $content[1]['done']     = "完成";
    $content[1]['title']    = "撰寫程式";
    $content[1]['end']      = "2020/06/08";
    $content[1]['priority'] = "低";
    $content[1]['assign']   = "李大頭";
    $content[2]['done']     = "未完成";
    $content[2]['title']    = "開會";
    $content[2]['end']      = "2020/06/08";
    $content[2]['priority'] = "高";
    $content[2]['assign']   = "李大頭、主任、XX科";
    $content[3]['done']     = "完成";
    $content[3]['title']    = "撰寫程式";
    $content[3]['end']      = "2020/06/08";
    $content[3]['priority'] = "低";
    $content[3]['assign']   = "李大頭";

    // var_dump($content);
    $smarty->assign('content', $content);

}

// 表單
function post_form()
{
    global $smarty;
    // die(var_dump($_POST));
    if (isset($_POST['send'])) {
        if (isset($_POST['next_op'])) {
            $sn = add();
            if (empty($sn)) {
                header("location: index.php");
            } else {
                header("location: index.php?sn={$sn}");
            }
        }

    }

    $next_op = 'insert';
    $smarty->assign('next_op', $next_op);
}

//新增清單
function add()
{
    global $db;

    //過濾變數
    $title      = $db->real_escape_string($_POST['title']);
    $directions = $db->real_escape_string($_POST['directions']);
    $end        = $db->real_escape_string($_POST['end']);
    $priority   = $db->real_escape_string($_POST['priority']);
    $done       = (int) $_POST['done'];
    $assign     = $db->real_escape_string(implode(';', $_POST['assign']));

    // 連線資料庫
    $sql = "INSERT INTO `list` (`title`, `directions`, `end`, `priority`, `assign`, `done`,`create_time`,`update_time`)
    VALUES ('{$title}', '{$directions}', '{$end}', '{$priority}', '{$assign}', '{$done}',now(),now())";

    // die($sql);

    if (!$db->query($sql)) {
        throw new Exception($db->error);
    }

    $sn = $db->insert_id;

    return $sn;

}

/*****************流程判斷***********************/
$op = isset($_REQUEST['op']) ? filter_var($_REQUEST['op'], FILTER_SANITIZE_SPECIAL_CHARS) : "";

switch ($op) {
    case 'post_form':
        post_form();
        break;

    default:
        list_all();
        break;
}

/*****************頁尾***********************/

// 呈現在哪個檔案 templates/xxx.tpl
$tpl = 'index.tpl';
// 引入頁尾
require_once 'footer.php';