<?php
require_once 'header.php';
$today = date("Y-m-d");
$smarty->assign('now', $today);
/****************** 函數區****************/
//列出所有
function list_all(){
global $db;
//查詢語法
$sql="SELECT * FROM `list` order by end" ;
$result=$db->query($sql);
// 有錯誤輸出錯誤
if(!$result){
throw new Exception($db->error);
}
// 讀出資料
// 法一(以資料表欄位為索引)
// while ($data= $result->fetch_assoc()) {
// $content[]=$data;
// }
// 法二(以資料表欄位順序為索引)--不建議
// while ($data= $result->fetch_row()) {
// $content[]=$data;
// }
// 法三
$i=0;
while (list($sn,$title,$directions,$end,$priority,$assign,$done,$create_time,$update_time)= $result->fetch_row()) {
$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=$i+1;
$i++;
}
// print_r($content);
// die(var_dump($content));
return $content;
}
// 表單函數
function post_form(){
global $smarty;
$content=[
'title'=>'',
'directions'=>'',
'end' => date("Y-m-d",strtotime("+7 day")),
'priority'=> "高",
'assign'=>['李大頭'],
'done' => 0,
];
$next_op='add';
$smarty->assign('next_op', $next_op);
return $content;
}
// 新增資料
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']);
$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;
}
/***********************流程判斷************************/
$op=isset($_REQUEST['op'])?$_REQUEST['op']:'';
switch ($op) {
case 'add':
$sn=add();
// 轉向
// header("location:index.php?sn={$sn}");
echo "<script>alert('新增成功');location.href='index.php?sn={$sn}';</script>";
exit;
case 'post_form':
$content=post_form();
break;
default:
$content=list_all();
break;
}
$smarty->assign('op', $op);
/***********************頁尾************************/
require_once 'footer.php';