:::
8-3 取消報名
- 取消報名實際上就是把 signups 中的一筆報名資料刪除而已。
- 還記得之前教的 確認後刪除 嗎?依樣畫葫蘆即可!
一、修改連結並產生相關函數
- 一樣先把取消報名連結改成這樣:
<a href="javascript:delete_signup({$action.action_id})" class="btn btn-danger btn-xs">取消報名</a> -
接著修改 index.php 的 list_action 函數中利用 tadtools內建的 sweet_alert 類別再產生一組刪除的js函數,以便做出「確認後刪除的功能」:
//顯示活動列表 function list_action() { global $xoopsDB, $xoopsTpl, $isAdmin; $tbl = $xoopsDB->prefix('actions'); $where = $isAdmin ? '' : "WHERE `enable` ='1' AND `end_date` > now() "; $sql = "SELECT * FROM `{$tbl}` $where ORDER BY `end_date` DESC"; //getPageBar($原sql語法, 每頁顯示幾筆資料, 最多顯示幾個頁數選項); $PageBar = getPageBar($sql, 5, 10); $bar = $PageBar['bar']; $sql = $PageBar['sql']; $total = $PageBar['total']; $xoopsTpl->assign('bar', $bar); $xoopsTpl->assign('total', $total); $result = $xoopsDB->query($sql) or web_error($sql); $myts = MyTextSanitizer::getInstance(); while ($action = $xoopsDB->fetchArray($result)) { $action['title'] = $myts->htmlSpecialChars($action['title']); $action['content'] = $myts->displayTarea($action['content'], 1, 1, 1, 1, 0); $actions[] = $action; } $xoopsTpl->assign('actions', $actions); include_once XOOPS_ROOT_PATH . "/modules/tadtools/sweet_alert.php"; $sweet_alert = new sweet_alert(); $sweet_alert->render("delete_action", "admin/main.php?op=delete_action&action_id=", 'action_id'); $sweet_alert2 = new sweet_alert(); $sweet_alert2->render("delete_signup", "index.php?op=delete_signup&action_id=", 'action_id', "確定要取消嗎?" , "取消後就不能參加活動囉!" , "是!含淚取消!"); }
二、做出刪除的函數
- 接著到 index.php,先新增一組流程:
case "delete_signup": delete_signup($action_id); header("location:{$_SERVER['PHP_SELF']}?action_id=$action_id"); exit; -
接著實際做出該函數:
//取消報名 function delete_signup($action_id) { global $xoopsDB, $xoopsUser; $uid = $xoopsUser->uid(); $tbl = $xoopsDB->prefix('signups'); $sql = "DELETE FROM `{$tbl}` WHERE `action_id`='{$action_id}' and `uid`='{$uid}'"; $xoopsDB->queryF($sql) or web_error($sql); $_SESSION['uid_signup'] = array_diff($_SESSION['uid_signup'], [$action_id]); } - 報名了要把該活動加到 $_SESSION['uid_signup'] 陣列中,同樣的,取消報名也要從 $_SESSION['uid_signup'] 陣列中把該編號拿掉。不拿掉會怎樣?不會怎樣,系統確實是刪了資料,但該活動還是會一直出現「取消報名」的按鈕而已。
- 所以,我們利用 array_diff([陣列一], [陣列二]) 來達成刪掉陣列中某個值。
三、關於array_diff()
- 這個其實並不是什麼移除陣列內容的函數,而是拿來比對兩個陣列的差異,並將差異的部份傳回一個新陣列。
- 所以,假設$_SESSION['uid_signup'] 陣列中有三個活動,依序為 [1, 2, 3],那麼,若是要取消活動 2,我們只要寫成 array_diff([1, 2, 3], [2]),該函數就會傳回陣列 [1, 3],因為這剛好是差異部份,是不是看起來如同 2 被刪除了一般!
8-2 報名過後的界面改變