:::
12. 加入管理功能
一、加入管理按鈕
- 修改 templates\list_action.tpl
{foreach $actions as action} <tr> <td>{$action.action_date}</td> <td><a href="index.php?action_id={$action.action_id}">{$action.title}</a></td> <td>{$action.end_date}</td> <td> {if $group=="admin"} <a href="admin.php?action_id={$action.action_id}" class="btn btn-warning btn-xs">修改</a> {/if} </td> </tr> {/foreach} - 在單一活動頁面也記得加,修改 templates\show_action.tpl,直接加在最下方即可
{if $group=="admin"} <div class="text-center"> <a href="admin.php?action_id={$action.action_id}" class="btn btn-warning">修改</a> </div> {/if}
二、修改流程
- 修改 admin.php 的流程,替原本的表單加入 $action_id
$action_id = isset($_REQUEST['action_id']) ? intval($_REQUEST['action_id']) : ''; switch ($op) { case "insert_action": $action_id = insert_action(); header("location:index.php?action_id=$action_id"); exit; default: $content = action_form($action_id); break; }
三、修改函數
- 函數 action_form() 原本就已經存在,我們只要加入一個新的參數 $action_id,並告訴函數,如果有參數 $action_id,就跟根據 $action_id 的值抓出該活動資料陣列。
function action_form($action_id = '') { global $db; $values = []; $op = 'insert_action'; if ($action_id) { $sql = "SELECT * FROM `actions` where `action_id`='{$action_id}'"; $result = $db->query($sql); if (!$result) { throw new Exception($db->error); } $values = $result->fetch_assoc(); $op = 'update_action'; } require_once "class/php-bootstrap-form/PFBC/Form.php"; ob_start(); echo '<script type="text/javascript" src="class/My97DatePicker/WdatePicker.js"></script>'; Form::open("action", $values); Form::Hidden("op", $op); Form::Hidden("action_id", $action_id); Form::Textbox("活動名稱", "title", ['required' => 1]); Form::Textbox("活動日期", "action_date", ['required' => 1, 'onClick' => "WdatePicker()"]); Form::Textbox("截止日期", "end_date", ['required' => 1, 'onClick' => "WdatePicker({dateFmt:'yyyy-MM-dd HH:mm:00'})"]); Form::YesNo("使否啟用", "enable"); Form::CKEditor("活動內容", "content"); Form::Button('儲存', 'submit'); Form::close(false); $form = ob_get_contents(); ob_end_clean(); return $form; } -
其中 $op 的值也要根據狀況來給不同值,若是有 $action_id 的值,那就表示要修改,故設為 update_action
-
若是沒有,就表示是要新增,故 $op 的值設為 insert_action
-
另外,要修改時多一個隱藏欄位,將 action_id 的值送出,如此,修改時,才知道要修改哪一筆活動資料。
11-2 列出單一活動