<?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);
// 處理複選框
$content['assign_arr'] = explode(';', $content['assign']);
// die(var_dump($content['assign_arr']));
$next_op = 'update';
$smarty->assign('sn', $sn);
} else {
$next_op = 'add';
// 加入預設值
$content = [
'title' => '',
'directions' => '',
'end' => date("Y-m-d", strtotime("+10 day")),
'priority' => '中',
'assign' => [],
'assign_arr' => ['我'],
'done' => 0,
];
}
$smarty->assign('content', $content);
$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);
}
// 讀出資料
$data = $result->fetch_assoc();
// die(var_dump($data));
return $data;
}
//新增清單
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;
}
function update(){
global $db;
//過濾變數
$sn=(int) $_POST['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']);
$done = (int) $_POST['done'];
$assign = $db->real_escape_string(implode(';', $_POST['assign']));
$update_time=date('Y-m-d H:i:s');
// 連線資料庫
$sql = "UPDATE `list` SET
`title` = '{$title}',
`directions` = '{$directions}',
`end` = '{$end}',
`priority` = '{$priority}',
`assign` = '{$assign}',
`done` = '{$done}',
`update_time` = '{$update_time}'
WHERE `sn` = '{$sn}'";
// die($sql);
if (!$db->query($sql)) {
throw new Exception($db->error);
}
return $sn;
}
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 'post_form':
post_form();
break;
case 'add':
$sn = add();
header("location: index.php?sn={$sn}");
break;
case 'update':
$sn = update();
header("location: index.php?sn={$sn}");
break;
case 'delete':
del($sn);
header("location: index.php");
break;
default:
if(empty($sn)){
list_all();
}else{
$content=find_one($sn);
$smarty->assign('content', $content);
$op='show_one';
}
break;
}
/*****************頁尾***********************/
// 呈現在哪個檔案 templates/xxx.tpl
$tpl = 'index.tpl';
// 引入頁尾
require_once 'footer.php';