{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}
{if $group=="admin"} <div class="text-center"> <a href="admin.php?action_id={$action.action_id}" class="btn btn-warning">修改</a> </div> {/if}
$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; }
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 的值送出,如此,修改時,才知道要修改哪一筆活動資料。