XOOPS 模組開發
一、 加入管理連結
- 加入刪除連結,流程新增一組 case "del_honor": <a href='main.php?op=del_honor&honor_sn={$all['honor_sn']}'>刪除</a>
- 加入修改連結,並流程新增一組 case "modify": <a href='main.php?op=modify_honor&honor_sn={$all['honor_sn']}'>編輯</a>
- 在進入流程前,整理一下傳入變數: $honor_sn = isset($_REQUEST['honor_sn']) ? intval($_REQUEST['honor_sn']) : "";
- 刪除函數: function del_honor($honor_sn=""){ global $xoopsDB; $sql="delete from ".$xoopsDB->prefix("tad_honor")." where `honor_sn`='{$honor_sn}'"; $xoopsDB->queryF($sql) or redirect_header('main.php', 3, mysql_error()); }
- 先加入javascript的刪除函數 <script language='JavaScript' type='text/JavaScript'> function delete_data(sn){ var sure = window.confirm('確定要刪除此資料?'); if (!sure) return; location.href='main.php?op=delete_honor&honor_sn=' + sn; } </script>
-
把連結改為執行javascript
<a href='javascript:delete_data({$all['honor_sn']})'>刪除</a>
- 修改功能就是使用者選擇一筆資料(取得該資料編號),利用該資料編號,取得其原始資料,並將取得的資料塞回原先表單,以供使用者修改。改完送出後,會執行資料update的動作。
- 在switch中新增一組modify 流程: <a href='main.php?op=modify_honor&honor_sn={$all['honor_sn']}'>編輯</a>
- 替原本的表單函數加入參數:function honor_form($honor_sn=null) case "modify_honor": $main = honor_form($honor_sn); break;
- 根據 $honor_sn 來取得原始輸入的資料: //抓出原始資料 if($honor_sn){ $sql="select * from ".$xoopsDB->prefix("tad_honor")." where `honor_sn` = '$honor_sn'"; $result = $xoopsDB->query($sql) or redirect_header('main.php', 3, mysql_error()); $myts =& MyTextSanitizer::getInstance(); $all = $xoopsDB->fetchArray($result); }
- 把預設值塞進XOOPS表單元件中: $XoopsFormText =new XoopsFormText('得獎者', 'honor_student', 40 , 255, $all['honor_students']);
- 根據情況,自動修改表單的op值,以便進行不同的動作: //設定op值 if($honor_sn){ $form->addElement(new XoopsFormHidden('op', 'update_honor')); $form->addElement(new XoopsFormHidden('honor_sn', $honor_sn)); }else{ $form->addElement(new XoopsFormHidden('op', 'insert_honor')); }
- 在switch中新增一組update 流程: case "update_honor": update_honor($honor_sn); header("location:main.php"); break;
- 製作更新函數update_honor($honor_sn): //更新榮譽榜資料庫 function update_honor($honor_sn=""){ global $xoopsDB; if(!$GLOBALS['xoopsSecurity']->check()){ $error=implode("<br />" , $GLOBALS['xoopsSecurity']->getErrors()); redirect_header($_SERVER['PHP_SELF'],3, $error); } //文字過濾 $myts = & MyTextSanitizer::getInstance(); $_POST['honor_year'] = $myts->addSlashes($_POST['honor_year']); $_POST['honor_date'] = $myts->addSlashes($_POST['honor_date']); $_POST['honor_students'] = $myts->addSlashes($_POST['honor_students']); $_POST['honor_descript'] = $myts->addSlashes($_POST['honor_descript']); $_POST['honor_teachers'] = $myts->addSlashes($_POST['honor_teachers']); $sql="update ".$xoopsDB->prefix("tad_honor")." set `honor_year` = '{$_POST['honor_year']}', `honor_date` = '{$_POST['honor_date']}', `honor_students` = '{$_POST['honor_students']}', `honor_descript` = '{$_POST['honor_descript']}', `honor_teachers` = '{$_POST['honor_teachers']}' where `honor_sn`='{$honor_sn}'"; $xoopsDB->queryF($sql) or redirect_header('main.php', 3, mysql_error()); }