11-6
                        上課範例:admin/cate.php
                    
                
                                
<?php
include 'header.php';
include_once XOOPS_ROOT_PATH."/modules/" . $xoopsModule->getVar("dirname") . "/class/admin.php" ;
$index_admin = new ModuleAdmin() ;
echo $index_admin->addNavigation('cate.php') ;
/*** 引入檔案 ***/
/*** 函數檔 ***/
function cate_form($cate_sn=null){
  include_once(XOOPS_ROOT_PATH."/class/xoopsformloader.php");
  
  
  if(!empty($cate_sn)){
    $cate=get_cate($cate_sn);
  }else{
    $cate=array();
  }
  
  //建立表單物件 post , get
  $form = new XoopsThemeForm('分類設定', 'cate', 'cate.php', 'post');
  //建立文字輸入框物件
  $XoopsFormText =new XoopsFormText('分類標題', 'cate_title', 40 , 255 , $cate['cate_title']);
  $XoopsFormText->setDescription ("請設定一個分類名稱");
  $form->addElement($XoopsFormText , true);
  $form->addElement(new XoopsFormText('分類排序', 'cate_sort', 4 , 4  , $cate['cate_sort']) , true);
  //建立單選物件
  $XoopsFormRadio =new XoopsFormRadio('是否使用', 'cate_enable' , $cate['cate_enable']);
  //$XoopsFormRadio->addOption('1', '是');
  //$XoopsFormRadio->addOption('0', '否');
  $options['1']='是';
  $options['0']='否';
  $XoopsFormRadio->addOptionArray($options);
  $form->addElement($XoopsFormRadio);
  if(!empty($cate_sn)){
    $form->addElement(new XoopsFormHidden('op', 'update'));
    $form->addElement(new XoopsFormHidden('cate_sn', $cate_sn));
  }else{
    $form->addElement(new XoopsFormHidden('op', 'insert'));
  }
  
  
  $Tray1=new XoopsFormElementTray('', ' ', 'name');
  $Tray1->addElement(new XoopsFormButton('', '', '送出', 'submit'));
  $Tray1->addElement(new XoopsFormButton('', '', '清除', 'reset'));
  $form->addElement($Tray1);
  
  $form->addElement(new XoopsFormHiddenToken());
  $f=$form->render();
  
  return $f;
}
//取得某筆分類資料陣列
function get_cate($cate_sn=null){
  global $xoopsDB;
  $sql="select * from ".$xoopsDB->prefix("tad_note_cate")." where `cate_sn` = '{$cate_sn}'";
  $result = $xoopsDB->query($sql) or redirect_header('cate.php', 3, mysql_error());
  $cate=$xoopsDB->fetchArray($result);
  return $cate;
}
//寫入函數
function insert_cate(){
  global $xoopsDB;
  if(!$GLOBALS['xoopsSecurity']->check()){
    $error=implode("<br />" , $GLOBALS['xoopsSecurity']->getErrors());
    redirect_header($_SERVER['PHP_SELF'],3, $error);
    exit;
  }
  
  $sql="insert into ".$xoopsDB->prefix("tad_note_cate")." (`cate_title` , `cate_sort` , `cate_enable`) values('{$_POST['cate_title']}' , '{$_POST['cate_sort']}' , '{$_POST['cate_enable']}')";
  $xoopsDB->queryF($sql) or redirect_header('cate.php', 3, mysql_error());
}
//列出所有分類資料
function list_cate(){
  global $xoopsDB;
  $sql="select * from ".$xoopsDB->prefix("tad_note_cate")." order by `cate_sort`";
  $result = $xoopsDB->query($sql) or redirect_header('cate.php', 3, mysql_error());
  
  $main="
  <table cellspacing=1 class='outer'>
    <tr>
      <th>"._MA_TADNOTE_NOTE_SN."</th>
      <th>"._MA_TADNOTE_NOTE_TITLE."</th>
      <th>"._MA_TADNOTE_NOTE_SORT."</th>
      <th>"._MA_TADNOTE_NOTE_PUBLIC."</th>
      <th>"._MA_TADNOTE_NOTE_COUNT."</th>
      <th>"._MA_TADNOTE_NOTE_FUNCTION."</th>
    </tr>
    ";
  while($cate=$xoopsDB->fetchArray($result)){
  
    $class=($i%2)?'odd':'even';
    $i++;
    
    $enable=($cate['cate_enable']=='1')?_YES:_NO;
  
    $main .= "
    <tr class='$class'>
      <td class='txtcenter'>{$cate['cate_sn']}</td>
      <td>{$cate['cate_title']}</td>
      <td class='txtcenter'>{$cate['cate_sort']}</td>
      <td class='txtcenter'>{$enable}</td>
      <td class='txtcenter'>{$cate['cate_count']}</td>
      <td class='txtcenter'><a href='cate.php?op=delete&cate_sn={$cate['cate_sn']}'>"._MA_TADNOTE_DEL."</a> | <a href='cate.php?op=modify&cate_sn={$cate['cate_sn']}'>"._MA_TADNOTE_MODIFY."</a></td>
    </tr>";
  }
  $main.="</table>";
  
  return $main;
}
//刪除函數
function delete_cate($cate_sn=null){
  global $xoopsDB;
  $sql="delete from ".$xoopsDB->prefix("tad_note_cate")." where cate_sn='$cate_sn'";
  $xoopsDB->queryF($sql) or redirect_header('cate.php', 3, mysql_error());
}
//更新函數
function update_cate($cate_sn=null){
  global $xoopsDB;
  if(!$GLOBALS['xoopsSecurity']->check()){
    $error=implode("<br />" , $GLOBALS['xoopsSecurity']->getErrors());
    redirect_header($_SERVER['PHP_SELF'],3, $error);
    exit;
  }
  $sql="update ".$xoopsDB->prefix("tad_note_cate")." set `cate_title` ='{$_POST['cate_title']}' , `cate_sort` = '{$_POST['cate_sort']}' , `cate_enable` = '{$_POST['cate_enable']}' where `cate_sn` = '{$cate_sn}'";
  $xoopsDB->queryF($sql) or redirect_header('cate.php', 3, mysql_error());
}
/*** 流程判斷 ***/
$op = isset($_REQUEST['op'])? $_REQUEST['op'] : "";
$cate_sn = isset($_REQUEST['cate_sn'])? intval($_REQUEST['cate_sn']) : "";
switch($op){
  case "insert":
  insert_cate();
  header('location:cate.php'); //轉向
  break;
  case "delete":
  delete_cate($cate_sn);
  header('location:cate.php'); //轉向
  break;
  
  case "modify":
  $main = cate_form($cate_sn);
  break;
  case "update":
  update_cate($cate_sn);
  header('location:cate.php'); //轉向
  break;
  
  default:
  $main = cate_form();
  $main.= list_cate();
}
/*** 輸出 ***/
echo $main;
include "footer.php";
?>