Jill 筆記
:::
:::
所有書籍
「PHP 從頭說」目錄
MarkDown
4-1 post_form.tpl
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
4-3 index.tpl
PHP 從頭說 ======= ```php 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 ""; exit; case 'post_form': $content=post_form(); break; default: $content=list_all(); break; } $smarty->assign('op', $op); /***********************頁尾************************/ require_once 'footer.php'; ```