:::

9-5 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 $db, $smarty;
    $sql    = "select * from `list` order by end";
    $result = $db->query($sql);

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

    $content = [];
    $i       = 0;
    while (list($sn, $title, $directions, $end, $priority, $assign, $done, $create_time, $update_time) = $result->fetch_row()) {

        //過濾變數
        // 字串
        $title = filter_var($title, FILTER_SANITIZE_SPECIAL_CHARS);
        // 大量文字
        $directions = htmlspecialchars($directions);
        $priority   = filter_var($priority, FILTER_SANITIZE_SPECIAL_CHARS);

        $content[$i]['sn']          = $sn;
        $content[$i]['title']       = $title;
        $content[$i]['directions']  = $directions;
        $content[$i]['end']         = $end;
        $content[$i]['priority']    = $priority;
        $content[$i]['assign']      = $assign;
        $content[$i]['done']        = $done;
        $content[$i]['create_time'] = $create_time;
        $content[$i]['update_time'] = $update_time;
        $i++;
    }

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

}

// 表單
function post_form()
{
    global $smarty;
    if (isset($_GET['sn'])) {
        // 過濾
        $sn = (int) $_GET['sn'];
        // 去資料庫撈一筆
        $content = find_one($sn);
        $next_op = 'update';
        $smarty->assign('sn', $sn);

    } else {
        $next_op = 'add';

    }

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

// 列出單一資料
function find_one($sn = '')
{
    global $db, $smarty;
    if (empty($sn)) {
        return;
    }

    $sql = "select * from `list` where `sn`='{$sn}'";
    // die($sql);
    $result = $db->query($sql);

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

}
//新增清單
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;
    case 'add':
        $sn = add();
        header("location: index.php?sn={$sn}");

        break;
    default:
        list_all();
        break;
}

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

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