:::

5-1 上課範例: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(){

  include_once(XOOPS_ROOT_PATH."/class/xoopsformloader.php");
  
  //建立表單物件 post , get
  $form = new XoopsThemeForm('分類設定', 'cate', 'cate.php', 'post');

  //建立文字輸入框物件
  $XoopsFormText =new XoopsFormText('分類標題', 'cate_title', 40 , 255);
  $XoopsFormText->setDescription ("請設定一個分類名稱");
  $form->addElement($XoopsFormText , true);

  $form->addElement(new XoopsFormText('分類排序', 'cate_sort', 4 , 4) , true);

  //建立單選物件
  $XoopsFormRadio =new XoopsFormRadio('是否使用', 'cate_enable' , '1');
  //$XoopsFormRadio->addOption('1', '是');
  //$XoopsFormRadio->addOption('0', '否');
  $options['1']='是';
  $options['0']='否';
  $XoopsFormRadio->addOptionArray($options);

  $form->addElement($XoopsFormRadio);

  $form->addElement(new XoopsFormHidden('op', 'insert'));
  
  $Tray1=new XoopsFormElementTray('', '&nbsp;', 'name');
  $Tray1->addElement(new XoopsFormButton('', '', '送出', 'submit'));
  $Tray1->addElement(new XoopsFormButton('', '', '清除', 'reset'));
  $form->addElement($Tray1);

  
  $form->addElement(new XoopsFormHiddenToken());


  $f=$form->render();
  
  return $f;
}


//寫入函數
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>分類編號</th>
      <th>分類標題</th>
      <th>排序</th>
      <th>是否使用</th>
      <th>人氣</th>
      <th>功能</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']}'>刪除</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());
}



/*** 流程判斷 ***/
$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;
  
  
  default:
  $main = cate_form();
  $main.= list_cate();
}

/*** 輸出 ***/
echo $main;

include "footer.php";
?>

:::

搜尋

QR Code 區塊

https%3A%2F%2Fmail.tad0616.cp27.secserverpros.com%2Fmodules%2Ftad_book3%2Fpage.php%3Ftbdsn%3D603%26tbsn%3D22

書籍目錄

展開 | 闔起

線上使用者

582人線上 (130人在瀏覽線上書籍)

會員: 0

訪客: 582

更多…