:::

6-1 index.php

<?php
require_once 'header.php';

$today = date("Y-m-d");
$smarty->assign('now', $today);

/****************** 函數區****************/
// 已完成清單
function done()
{
    global $db, $smarty;

    include_once "class/PageBar.php";
    //查詢語法
    $sql = "SELECT * FROM `list` where `done`=1 order by end";

    $PageBar = getPageBar($db, $sql, 2, 10);
    // die(var_dump($PageBar));
    $bar = $PageBar['bar'];
    //  ["sql"]=> string(44) "SELECT * FROM `list` order by end LIMIT 0, 5"
    // limit 0--第一筆從零開始數
    // 5--代表一次撈5筆
    $sql = $PageBar['sql'];
    $total = $PageBar['total'];
    $result = $db->query($sql);
    // 有錯誤輸出錯誤
    if (!$result) {
        throw new Exception($db->error);
    }
    // 讀出資料
    // 法一(以資料表欄位為索引)
    $content = [];
    while ($data = $result->fetch_assoc()) {
        // die(var_dump($data));
        $data['title'] = filter_var($data['title'], FILTER_SANITIZE_FULL_SPECIAL_CHARS);
        $data['assign'] = filter_var($data['assign'], FILTER_SANITIZE_SPECIAL_CHARS);
        $content[] = $data;

    }
    $smarty->assign('bar', $bar);
    $smarty->assign('total', $total);
    $smarty->assign('content', $content);
}
//列出所有未完成
function list_all()
{
    global $db, $smarty;

    include_once "class/PageBar.php";
    //查詢語法
    $sql = "SELECT * FROM `list` where `done`!=1 order by end";

    $PageBar = getPageBar($db, $sql, 2, 10);
    // die(var_dump($PageBar));
    $bar = $PageBar['bar'];
    //  ["sql"]=> string(44) "SELECT * FROM `list` order by end LIMIT 0, 5"
    // limit 0--第一筆從零開始數
    // 5--代表一次撈5筆
    $sql = $PageBar['sql'];
    $total = $PageBar['total'];
    $result = $db->query($sql);
    // 有錯誤輸出錯誤
    if (!$result) {
        throw new Exception($db->error);
    }
    // 讀出資料
    // 法一(以資料表欄位為索引)
    $content = [];
    while ($data = $result->fetch_assoc()) {
        // die(var_dump($data));
        $data['title'] = filter_var($data['title'], FILTER_SANITIZE_FULL_SPECIAL_CHARS);
        $data['assign'] = filter_var($data['assign'], FILTER_SANITIZE_SPECIAL_CHARS);
        $content[] = $data;

    }
    $smarty->assign('bar', $bar);
    $smarty->assign('total', $total);
    $smarty->assign('content', $content);
}

// 表單函數
function post_form()
{
    global $db, $smarty;
    if (isset($_GET['sn'])) {
        $sn = (int) $_GET['sn'];
        //查詢語法
        $sql = "SELECT * FROM `list` where `sn`='{$sn}'";
        $result = $db->query($sql);
        // 有錯誤輸出錯誤
        if (!$result) {
            throw new Exception($db->error);
        }
        // 讀出資料
        $content = $result->fetch_assoc();
        $content['assign'] = explode(';', $content['assign']);
        $next_op = 'update';
    } else {

        $content = [
            'sn' => '',
            'title' => '',
            'directions' => '',
            'end' => date("Y-m-d", strtotime("+7 day")),
            'priority' => "高",
            'assign' => ['李大頭'],
            'done' => 0,
        ];
        $next_op = 'add';
    }

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

// 新增資料
function add()
{
    global $db;

    $message = '';

    if (empty($_POST['title'])) {
        $message .= '標題必填 | ';
    }

    if (!isset($_POST['assign'])) {
        $message .= '至少指派一名';
    }
    if (!empty($message)) {
        echo "<script>alert('" . $message . "');history.back();</script>";
        exit;
    }

    $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']);
    $assign = implode(';', $_POST['assign']);
    $done = intval($_POST['done']);

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

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

    $sn = $db->insert_id;
    return $sn;

}
// 修改
function update()
{
    global $db;
    $message = '';

    if (empty($_POST['title'])) {
        $message .= '標題必填 | ';
    }

    if (!isset($_POST['assign'])) {
        $message .= '至少指派一名';
    }
    if (!empty($message)) {
        echo "<script>alert('" . $message . "');self.location=document.referrer;</script>";
        exit;
    }
    $sn = (int) $_REQUEST['sn'];
    $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']);
    $assign = implode(';', $_POST['assign']);
    $done = (int) $_POST['done'];
    $update_time = date('Y-m-d H:i:s');
    // update
    // UPDATE `list` SET `title` = '測試編輯', `end` = '2021-05-06', `priority` = '高', `assign` = '李大頭;吳大大', `create_time` = '2021-04-17 15:04:30' WHERE `list`.`sn` = 8;
    $sql = "UPDATE `list` SET
         `title` = '{$title}',
         `directions` = '{$directions}',
         `end` = '{$end}',
         `priority` = '{$priority}',
         `assign` = '{$assign}',
         `done` = '{$done}',
         `update_time` = '{$update_time}'
         WHERE sn={$sn};
    ";

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

    return $sn;

}

// 列出單筆
function find_one($sn)
{
    global $db;
    // 去資料庫找sn單筆資料
    //查詢語法
    $sql = "SELECT * FROM `list` where `sn`='{$sn}'";
    $result = $db->query($sql);
    // 有錯誤輸出錯誤
    if (!$result) {
        throw new Exception($db->error);
    }
    // 讀出資料
    $content = $result->fetch_assoc();

    $content['title'] = filter_var($content['title'], FILTER_SANITIZE_FULL_SPECIAL_CHARS);
    // $content['directions']=htmlspecialchars($content['directions'],ENT_QUOTES);
    // $content['directions'] = filter_var($content['directions'], FILTER_SANITIZE_FULL_SPECIAL_CHARS);
    $content['assign'] = filter_var($content['assign'], FILTER_SANITIZE_SPECIAL_CHARS);
    return $content;
}
// 刪除
function del($sn)
{
    global $db;
    // 刪除資料
    $sql = "delete from `list` where `sn`={$sn}";
    if (!$db->query($sql)) {
        throw new Exception($db->error);
    }

}
/***********************流程判斷************************/
$op = isset($_REQUEST['op']) ? filter_var($_REQUEST['op'], FILTER_SANITIZE_SPECIAL_CHARS) : "";
$sn = isset($_REQUEST['sn']) ? (int) $_REQUEST['sn'] : "";
switch ($op) {
    case 'delete':
        del($sn);
        // 轉向
        header("location:index.php");
        exit;
    case 'update':
        $sn = update();
        // 轉向
        // header("location:index.php?sn={$sn}");
        echo "<script>alert('修改成功');location.href='index.php?sn={$sn}';</script>";
        exit;
    case 'add':
        $sn = add();
        // 轉向
        // header("location:index.php?sn={$sn}");
        echo "<script>alert('新增成功');location.href='index.php?sn={$sn}';</script>";
        exit;
    case 'post_form':
        post_form();
        break;

    case 'done':
        done();
        break;
    default:
        if ($sn) {
            // 顯示單筆
            $content = find_one($sn);
            $smarty->assign('content', $content);
            $op = 'show_one';
        } else {
            list_all();
        }

        break;
}

$smarty->assign('op', $op);

/***********************頁尾************************/
require_once 'footer.php';