:::

17-5 上課範例:admin/cate.php

001<?php
002include 'header.php';
003include_once XOOPS_ROOT_PATH."/modules/" . $xoopsModule->getVar("dirname") . "/class/admin.php" ;
004$index_admin = new ModuleAdmin() ;
005echo $index_admin->addNavigation('cate.php') ;
006 
007 
008/*** 引入檔案 ***/
009 
010/*** 函數檔 ***/
011function cate_form($cate_sn=null){
012 
013  include_once(XOOPS_ROOT_PATH."/class/xoopsformloader.php");
014   
015   
016  if(!empty($cate_sn)){
017    $cate=get_cate($cate_sn);
018  }else{
019    $cate=array();
020  }
021   
022  //建立表單物件 post , get
023  $form = new XoopsThemeForm('分類設定', 'cate', 'cate.php', 'post');
024 
025  //建立文字輸入框物件
026  $XoopsFormText =new XoopsFormText('分類標題', 'cate_title', 40 , 255 , $cate['cate_title']);
027  $XoopsFormText->setDescription ("請設定一個分類名稱");
028  $form->addElement($XoopsFormText , true);
029 
030  $form->addElement(new XoopsFormText('分類排序', 'cate_sort', 4 , 4  , $cate['cate_sort']) , true);
031 
032  //建立單選物件
033  $XoopsFormRadio =new XoopsFormRadio('是否使用', 'cate_enable' , $cate['cate_enable']);
034  //$XoopsFormRadio->addOption('1', '是');
035  //$XoopsFormRadio->addOption('0', '否');
036  $options['1']='是';
037  $options['0']='否';
038  $XoopsFormRadio->addOptionArray($options);
039 
040  $form->addElement($XoopsFormRadio);
041 
042  if(!empty($cate_sn)){
043    $form->addElement(new XoopsFormHidden('op', 'update'));
044    $form->addElement(new XoopsFormHidden('cate_sn', $cate_sn));
045  }else{
046    $form->addElement(new XoopsFormHidden('op', 'insert'));
047  }
048   
049   
050  $Tray1=new XoopsFormElementTray('', '&nbsp;', 'name');
051  $Tray1->addElement(new XoopsFormButton('', '', '送出', 'submit'));
052  $Tray1->addElement(new XoopsFormButton('', '', '清除', 'reset'));
053  $form->addElement($Tray1);
054 
055   
056  $form->addElement(new XoopsFormHiddenToken());
057 
058 
059  $f=$form->render();
060   
061  return $f;
062}
063 
064 
065 
066//寫入函數
067function insert_cate(){
068  global $xoopsDB;
069 
070  if(!$GLOBALS['xoopsSecurity']->check()){
071    $error=implode("<br />" , $GLOBALS['xoopsSecurity']->getErrors());
072    redirect_header($_SERVER['PHP_SELF'],3, $error);
073    exit;
074  }
075 
076   
077  $sql="insert into ".$xoopsDB->prefix("tad_note_cate")." (`cate_title` , `cate_sort` , `cate_enable`) values('{$_POST['cate_title']}' , '{$_POST['cate_sort']}' , '{$_POST['cate_enable']}')";
078  $xoopsDB->queryF($sql) or redirect_header('cate.php', 3, mysql_error());
079}
080 
081//列出所有分類資料
082function list_cate(){
083  global $xoopsDB;
084 
085  $sql="select * from ".$xoopsDB->prefix("tad_note_cate")." order by `cate_sort`";
086  $result = $xoopsDB->query($sql) or redirect_header('cate.php', 3, mysql_error());
087   
088 
089  $main="
090  <table cellspacing=1 class='outer'>
091    <tr>
092      <th>"._MA_TADNOTE_NOTE_SN."</th>
093      <th>"._MA_TADNOTE_NOTE_TITLE."</th>
094      <th>"._MA_TADNOTE_NOTE_SORT."</th>
095      <th>"._MA_TADNOTE_NOTE_PUBLIC."</th>
096      <th>"._MA_TADNOTE_NOTE_COUNT."</th>
097      <th>"._MA_TADNOTE_NOTE_FUNCTION."</th>
098    </tr>
099    ";
100  while($cate=$xoopsDB->fetchArray($result)){
101   
102    $class=($i%2)?'odd':'even';
103    $i++;
104     
105    $enable=($cate['cate_enable']=='1')?_YES:_NO;
106   
107    $main .= "
108    <tr class='$class'>
109      <td class='txtcenter'>{$cate['cate_sn']}</td>
110      <td>{$cate['cate_title']}</td>
111      <td class='txtcenter'>{$cate['cate_sort']}</td>
112      <td class='txtcenter'>{$enable}</td>
113      <td class='txtcenter'>{$cate['cate_count']}</td>
114      <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>
115    </tr>";
116  }
117  $main.="</table>";
118   
119  return $main;
120}
121 
122 
123 
124 
125//刪除函數
126function delete_cate($cate_sn=null){
127  global $xoopsDB,$xoopsModule;
128 
129  $sql="delete from ".$xoopsDB->prefix("tad_note_cate")." where cate_sn='$cate_sn'";
130  $xoopsDB->queryF($sql) or redirect_header('cate.php', 3, mysql_error());
131   
132  $mid=$xoopsModule->getVar('mid');
133  xoops_notification_deletebyitem ($mid, "cate", $cate_sn);
134}
135 
136 
137//更新函數
138function update_cate($cate_sn=null){
139  global $xoopsDB;
140 
141  if(!$GLOBALS['xoopsSecurity']->check()){
142    $error=implode("<br />" , $GLOBALS['xoopsSecurity']->getErrors());
143    redirect_header($_SERVER['PHP_SELF'],3, $error);
144    exit;
145  }
146 
147  $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}'";
148  $xoopsDB->queryF($sql) or redirect_header('cate.php', 3, mysql_error());
149}
150 
151 
152 
153 
154/*** 流程判斷 ***/
155$op = isset($_REQUEST['op'])? $_REQUEST['op'] : "";
156$cate_sn = isset($_REQUEST['cate_sn'])? intval($_REQUEST['cate_sn']) : "";
157 
158 
159 
160switch($op){
161 
162  case "insert":
163  insert_cate();
164  header('location:cate.php'); //轉向
165  break;
166 
167  case "delete":
168  delete_cate($cate_sn);
169  header('location:cate.php'); //轉向
170  break;
171   
172  case "modify":
173  $main = cate_form($cate_sn);
174  break;
175 
176  case "update":
177  update_cate($cate_sn);
178  header('location:cate.php'); //轉向
179  break;
180   
181  default:
182  $main = cate_form();
183  $main.= list_cate();
184}
185 
186/*** 輸出 ***/
187echo $main;
188 
189include "footer.php";
190?>

:::

搜尋

QR Code 區塊

https%3A%2F%2Ftad0616.cp27.secserverpros.com%2Fmodules%2Ftad_book3%2Fpage.php%3Ftbdsn%3D673%26tbsn%3D22

書籍目錄

展開 | 闔起

線上使用者

145人線上 (15人在瀏覽線上書籍)

會員: 0

訪客: 145

更多…