Jill 筆記
:::
:::
所有書籍
「PHP 從頭說」目錄
MarkDown
6. 檢查必填欄位與常用小工具
1. 安裝PHP開發環境與BootStrap響應式框架
1-1 index.html
2. PHP基本語法 V.S Smarty樣板
2-1 index.html
2-2 index.php
2-3 demo.php
2-4 index.tpl
3. 資料庫規劃與表單製作
3-1 index.php
3-2 header.php
3-3 footer.php
3-4 templates/header.tpl
3-5 template/navbar.tpl
3-6 index.tpl
3-7 function.php
3-8 templates/post_form.tpl
4. 表單函數與資料庫存取
4-1 post_form.tpl
4-2 index.php
4-3 index.tpl
4-4 array($result->fetch_assoc())
4-5 array:$result->fetch_row()
5. 資料的讀取、編輯修改及刪除
5-1 index.php
5-2 header.php
5-3 footer.php
5-4 templates/index.tpl
5-5 templates/show_one.tpl
5-6 templates/post_form.tpl
6. 檢查必填欄位與常用小工具
6-1 index.php
6-2 templates/post_form.tpl
6-3 templates/show_one.tpl
6-4 templates/index.tpl
6-5 templates/sweetalert_delete.tpl
6-2 templates/post_form.tpl
PHP 從頭說 ======= ```php 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 ""; 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 ""; 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 ""; exit; case 'add': $sn = add(); // 轉向 // header("location:index.php?sn={$sn}"); echo ""; 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'; ```