:::

6-1 上課範例:admin/cate.php

<?php
/*-----------引入檔案區--------------*/
include_once "header_admin.php";

/*-----------function區--------------*/
//contact_cate編輯表單
function contact_cate_form($gsn=""){
	global $xoopsDB,$xoopsUser;

	//抓取預設值
	if(!empty($gsn)){
		$DBV=get_contact_cate($gsn);
	}else{
		$DBV=array();
	}

	//預設值設定


	//設定「gsn」欄位預設值
	$gsn=(!isset($DBV['gsn']))?$gsn:$DBV['gsn'];

	//設定「title」欄位預設值
	$title=(!isset($DBV['title']))?null:$DBV['title'];

	//設定「parent_gsn」欄位預設值
	$parent_gsn=(!isset($DBV['parent_gsn']))?null:$DBV['parent_gsn'];

	//設定「sort」欄位預設值
	$sort=(!isset($DBV['sort']))?contact_cate_max_sort():$DBV['sort'];

	$op=(empty($gsn))?"insert_contact_cate":"update_contact_cate";


	$main="
	<form action='{$_SERVER['PHP_SELF']}' method='post'>
	<table class='form_tbl'>

	<!--群組名稱-->
	<tr><td class='title' nowrap>群組名稱</td>
	<td class='col'><input type='text' name='title' size='20' value='{$title}' id='title' ></td></tr>

	<!--父群組-->
	<tr><td class='title' nowrap>父群組</td>
	<td class='col'>
	<select name='parent_gsn' size=1 >
		<option value='' ".chk($parent_gsn,'','1','selected')."></option>
		".get_contact_cate_options('edit' , $gsn , $parent_gsn )."
	</select>
	</td></tr>

	<!--排序-->
	<tr><td class='title' nowrap>排序</td>
	<td class='col'><input type='text' name='sort' size='20' value='{$sort}' id='sort'></td></tr>

	<tr>
		<td class='bar' colspan='2'>

    	<!--群組編號-->
    	<input type='hidden' name='gsn' value='{$gsn}'>
			<input type='hidden' name='op' value='{$op}'>
			<input type='submit' value='儲存'>
		</td>
	</tr>
	</table>
	</form>";

	//raised,corners,inset
	$main=div_3d("群組設定",$main,"raised");

	return $main;
}


//自動取得contact_cate的最新排序
function contact_cate_max_sort(){
	global $xoopsDB;
	$sql = "select max(`sort`) from `".$xoopsDB->prefix("contact_cate")."`";
	$result = $xoopsDB->query($sql) or redirect_header($_SERVER['PHP_SELF'],3, mysql_error());
	list($sort)=$xoopsDB->fetchRow($result);
	return ++$sort;
}


//新增資料到contact_cate中
function insert_contact_cate(){
	global $xoopsDB,$xoopsUser;


	$myts =& MyTextSanitizer::getInstance();


	$sql = "insert into `".$xoopsDB->prefix("contact_cate")."`
	(`title` , `parent_gsn` , `sort`)
	values('{$_POST['title']}' , '{$_POST['parent_gsn']}' , '{$_POST['sort']}')";
	$xoopsDB->query($sql) or redirect_header($_SERVER['PHP_SELF'],3, mysql_error());

	//取得最後新增資料的流水編號
	$gsn = $xoopsDB->getInsertId();
	return $gsn;
}

//更新contact_cate某一筆資料
function update_contact_cate($gsn=""){
	global $xoopsDB,$xoopsUser;


	$myts =& MyTextSanitizer::getInstance();


	$sql = "update `".$xoopsDB->prefix("contact_cate")."` set
	 `title` = '{$_POST['title']}' ,
	 `parent_gsn` = '{$_POST['parent_gsn']}' ,
	 `sort` = '{$_POST['sort']}'
	where `gsn` = '$gsn'";
	$xoopsDB->queryF($sql) or redirect_header($_SERVER['PHP_SELF'],3, mysql_error());
	return $gsn;
}




//刪除contact_cate某筆資料資料
function delete_contact_cate($gsn=""){
	global $xoopsDB , $isAdmin;
	$sql = "delete from `".$xoopsDB->prefix("contact_cate")."` where `gsn` = '{$gsn}'";
	$xoopsDB->queryF($sql) or redirect_header($_SERVER['PHP_SELF'],3, mysql_error());
}




//取得contact_cate無窮分類列表
function list_contact_cate_loop($show_function=1 , $parent_gsn=0 , $i=0){
	global $xoopsDB , $xoopsModule;

	$MDIR=$xoopsModule->getVar('dirname');

	$sql = "select * from `".$xoopsDB->prefix("contact_cate")."` where `parent_gsn` = '{$parent_gsn}' order by `sort`";
	$result = $xoopsDB->query($sql) or redirect_header($_SERVER['PHP_SELF'],3, mysql_error());
	$function_title=($show_function)?"<th>功能</th>":"";
	$prefix=str_repeat("----",$i);
	$i++;

	$data="";
	while($all=$xoopsDB->fetchArray($result)){
		//以下會產生這些變數: $gsn , $title , $parent_gsn , $sort
		foreach($all as $k=>$v){
			$$k=$v;
		}
		$fun=($show_function)?"
		<td>
		<a href='{$_SERVER['PHP_SELF']}?op=contact_cate_form&gsn=$gsn'><img src='".XOOPS_URL."/modules/{$MDIR}/images/edit.gif' alt='編輯'></a>
		<a href=\"javascript:delete_contact_cate_func($gsn);\"><img src='".XOOPS_URL."/modules/{$MDIR}/images/del.gif' alt='刪除'></a>
		</td>":"";
		
		
		$data.="
		<tr id='tr_{$gsn}'>
		<td>{$prefix}<a href='{$_SERVER['PHP_SELF']}?gsn={$gsn}'>{$title}</a> <img src='".XOOPS_URL."/modules/tadtools/treeTable/images/updown_s.png' style='cursor: s-resize;margin:0px 4px;' alt='拉動排序' title='拉動排序'> ({$sort})</td>
		$fun
		</tr>";

		$data.=list_contact_cate_loop($show_function,$gsn,$i);
	}

	if($parent_gsn!='0'){
		return $data;
	}else{
	
		$jquery=get_jquery(true);
	
		$main="
		$jquery
    <script type='text/javascript'>
    $(document).ready(function(){
        $('#sort').sortable({ opacity: 0.6, cursor: 'move', update: function() {
            var order = $(this).sortable('serialize');
            $.post('save_sort.php', order, function(theResponse){
                $('#save_msg').html(theResponse);
            });
        }
        });
    });

		function delete_contact_cate_func(gsn){
			var sure = window.confirm('確定要刪除此資料?');
			if (!sure)	return;
			location.href=\"{$_SERVER['PHP_SELF']}?op=delete_contact_cate&gsn=\" + gsn;
		}
		</script>
		
    <div id='save_msg' style='margin:10px auto;'></div>

    <table summary='list_table' id='tbl' style='width:100%;'>
		<tr>
		<th nowrap>群組名稱</th>
		$function_title</tr>
		<tbody id='sort'>
		$data
		</tbody>
		<tr>
		<td colspan=6 class='bar'>
		<a href='{$_SERVER['PHP_SELF']}?op=contact_cate_form'><img src='".XOOPS_URL."/modules/{$MDIR}/images/add.gif' alt='"._BP_ADD."' align='right'></a>
		{$bar}</td></tr>
		</table>";

		//raised,corners,inset
		$main=div_3d("",$main,"corners");

	}

	return $main;
}

//取得所有contact_cate分類選單的選項(模式 = edit[編輯用] or show[顯示用],目前分類編號,目前分類的所屬編號)
function get_contact_cate_options($mode='show' , $default_gsn="0" , $default_parent_gsn="0" , $unselect_level="" , $start_search_sn="0" , $level=0){
	global $xoopsDB , $xoopsModule;
	$sql = "select `gsn` , `title` from `".$xoopsDB->prefix("contact_cate")."` where `parent_gsn` = '{$start_search_sn}' order by `sort`";
	$result = $xoopsDB->query($sql) or redirect_header($_SERVER['PHP_SELF'] , 3, mysql_error());

	$prefix=str_repeat("&nbsp;&nbsp;" , $level);
	$level++;

	$unselect=explode("," , $unselect_level);

	$main="";
	while(list($gsn , $title)=$xoopsDB->fetchRow($result)){
		if($mode=="edit"){
			$selected=($gsn==$default_parent_gsn)?"selected=selected":"";
			$selected.=($gsn==$default_gsn)?"disabled=disabled":"";
			$selected.=(in_array($level , $unselect))?"disabled=disabled":"";
		}else{
			$selected=($gsn==$default_gsn)?"selected=selected":"";
			$selected.=(in_array($level , $unselect))?"disabled=disabled":"";
		}
		$main.="<option value=$gsn $selected>{$prefix}{$title}</option>";
		$main.=get_contact_cate_options($mode , $default_gsn , $default_parent_gsn , $unselect_level , $gsn , $level);

	}
	return $main;
}

/*-----------執行動作判斷區----------*/
$op = empty($_REQUEST['op'])? "":$_REQUEST['op'];
$sn=empty($_REQUEST['sn'])?"":intval($_REQUEST['sn']);
$gsn=empty($_REQUEST['gsn'])?"":intval($_REQUEST['gsn']);
$files_sn=empty($_REQUEST['files_sn'])?"":intval($_REQUEST['files_sn']);


switch($op){

  //新增資料
  case "insert_contact_cate":
  $gsn=insert_contact_cate();
  header("location: {$_SERVER['PHP_SELF']}");
  break;

  //更新資料
  case "update_contact_cate":
  update_contact_cate($gsn);
  header("location: {$_SERVER['PHP_SELF']}");
  break;

  //輸入表格
  case "contact_cate_form":
  $main=contact_cate_form($gsn);
  break;

  //刪除資料
  case "delete_contact_cate":
  delete_contact_cate($gsn);
  header("location: {$_SERVER['PHP_SELF']}");
  break;

  //預設動作
  default:
  $main=list_contact_cate_loop();
  break;

	
}

/*-----------秀出結果區--------------*/
module_admin_footer($main,1);

?>

 


:::

搜尋

QR Code 區塊

https%3A%2F%2Ftad0616.net%2Fmodules%2Ftad_book3%2Fpage.php%3Ftbdsn%3D764%26tbsn%3D26

書籍目錄

展開 | 闔起

線上使用者

61人線上 (27人在瀏覽線上書籍)

會員: 0

訪客: 61

更多…