Jill 筆記
:::
:::
所有書籍
「PHP從入門到放棄實戰班」目錄
MarkDown
10-2 刪除事項
1. 實戰之前
1-1 安裝開發環境
1-2 上課範例
2. HTML基礎
2-1 index.html
3. BootStrap響應式框架
3-1 格線系統
3-2 表格
3-3 導覽列
3-4 表單
4. PHP基本語法
4-1 註解
4-2 PHP資訊頁
4-3 PHP變數與陣列
4-4 各種訊息整理
5. 套用Smarty樣板
5-1 使用 Smarty
5-2 index.php
5-3 templates/index.tpl
5-4 php二維陣列
6. 資料庫規劃
6-1 MySQL常用資料類型一覽
6-2 讓PHP7連線到MySQL資料庫
7. PHP程式整併與樣板流程判斷
7-1 PHP條件判斷
7-2 PHP的變數過濾
7-3 練習自訂函數
7-4 templates/index.tpl
7-5 index.php
8. 製作表單並寫入資料到資料庫
8-1 index.php
8-2 post_form.tpl
8-2-1 post_form.tpl
8-3 templates/index.tpl
8-4 footer.php
9. 資料庫讀取
9-1 幾個常用的迴圈用法
9-2 讀出資料的安全性過濾並顯示
9-3 想想寫入還有什麼問題?
9-4 templates/post_form.tpl
9-5 index.php
9-6 templates/index.tpl
10. 編輯表單、刪除資料
10-1 顯示單筆資料
10-2 刪除事項
10-3 index.php
10-4 templates/post_form.tpl
10-5 templates/index.tpl
10-6 templates/show_one.tpl
11. 其他細節處理(補充)
11-1 小月曆
11-2 加入所見即所得編輯器
11-3 表單驗證
11-4 分頁功能
12. 練習
10-4 templates/post_form.tpl
PHP從入門到放棄實戰班 ============ ```php 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'; ```