:::

2-1 上課範例:admin/index.php

<?php
//  ------------------------------------------------------------------------ //
// 本模組由 tad 製作
// 製作日期:2012-09-12
// $Id:$
// ------------------------------------------------------------------------- //

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

/*-----------function區--------------*/
//contact編輯表單
function contact_form($sn=""){
	global $xoopsDB,$xoopsUser;

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

	//預設值設定

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

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

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

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

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

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

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

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

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

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

	$op=(empty($sn))?"insert_contact":"update_contact";


  $jquery=get_jquery();

	$main="
	$jquery
	
	<script type='text/javascript'>
  $(document).ready(function(){
    $.post('ajax.php' , function(data){
      $('#menu1').html(data);
      $.post('ajax.php' , {parent_gsn: $('#menu1').val()} , function(data){
        $('#menu2').html(data);
      });
    });
    
    $('#menu1').change(function(){
      $.post('ajax.php' , {parent_gsn: $('#menu1').val()} , function(data){
        $('#menu2').html(data);
      });
    });
    
  });
  </script>
	
	<form action='{$_SERVER['PHP_SELF']}' method='post' id='myForm'>
	
	<table class='form_tbl'>

	<!--群組-->
	<tr><td class='title' nowrap>群組</td>
	<td class='col'>
	<select id='menu1'></select>
	<select name='gsn' id='menu2'></select>
	</td></tr>

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


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

	<!--信箱-->
	<tr><td class='title' nowrap>信箱</td>
	<td class='col'><input type='text' name='email' size='20' value='{$email}' id='email' ></td></tr>
	
	
	<!--生日-->
	<tr><td class='title' nowrap>生日</td>
	<td class='col'><input type='text' name='birthday' size='20' value='{$birthday}' id='birthday' ></td></tr>

	<!--郵遞區號-->
	<tr><td class='title' nowrap>郵遞區號</td>
	<td class='col'><input type='text' name='zip' size='20' value='{$zip}' id='zip' ></td></tr>

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

	<!--鄉鎮市區-->
	<tr><td class='title' nowrap>鄉鎮市區</td>
	<td class='col'><input type='text' name='city' size='20' value='{$city}' id='city' ></td></tr>

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

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

    	<!--編號-->
    	<input type='hidden' name='sn' value='{$sn}'>

			<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中
function insert_contact(){
	global $xoopsDB,$xoopsUser;


	$myts =& MyTextSanitizer::getInstance();
	$_POST['tel']=$myts->addSlashes($_POST['tel']);
	$_POST['email']=$myts->addSlashes($_POST['email']);
	$_POST['name']=$myts->addSlashes($_POST['name']);
	$_POST['birthday']=$myts->addSlashes($_POST['birthday']);
	$_POST['zip']=$myts->addSlashes($_POST['zip']);
	$_POST['county']=$myts->addSlashes($_POST['county']);
	$_POST['city']=$myts->addSlashes($_POST['city']);
	$_POST['addr']=$myts->addSlashes($_POST['addr']);


	$sql = "insert into `".$xoopsDB->prefix("contact")."`
	(`tel` , `email` , `name` , `gsn` , `birthday` , `zip` , `county` , `city` , `addr`)
	values('{$_POST['tel']}' , '{$_POST['email']}' , '{$_POST['name']}' , '{$_POST['gsn']}' , '{$_POST['birthday']}' , '{$_POST['zip']}' , '{$_POST['county']}' , '{$_POST['city']}' , '{$_POST['addr']}')";
	$xoopsDB->query($sql) or redirect_header($_SERVER['PHP_SELF'],3, mysql_error());

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

//更新contact某一筆資料
function update_contact($sn=""){
	global $xoopsDB,$xoopsUser;


	$myts =& MyTextSanitizer::getInstance();
	$_POST['tel']=$myts->addSlashes($_POST['tel']);
	$_POST['email']=$myts->addSlashes($_POST['email']);
	$_POST['name']=$myts->addSlashes($_POST['name']);
	$_POST['birthday']=$myts->addSlashes($_POST['birthday']);
	$_POST['zip']=$myts->addSlashes($_POST['zip']);
	$_POST['county']=$myts->addSlashes($_POST['county']);
	$_POST['city']=$myts->addSlashes($_POST['city']);
	$_POST['addr']=$myts->addSlashes($_POST['addr']);


	$sql = "update `".$xoopsDB->prefix("contact")."` set
	 `tel` = '{$_POST['tel']}' ,
	 `email` = '{$_POST['email']}' ,
	 `name` = '{$_POST['name']}' ,
	 `gsn` = '{$_POST['gsn']}' ,
	 `birthday` = '{$_POST['birthday']}' ,
	 `zip` = '{$_POST['zip']}' ,
	 `county` = '{$_POST['county']}' ,
	 `city` = '{$_POST['city']}' ,
	 `addr` = '{$_POST['addr']}'
	where `sn` = '$sn'";
	$xoopsDB->queryF($sql) or redirect_header($_SERVER['PHP_SELF'],3, mysql_error());
	return $sn;
}

//列出所有contact資料
function list_contact($show_function=1){
	global $xoopsDB , $xoopsModule , $isAdmin;

	$sql = "select * from `".$xoopsDB->prefix("contact")."` ";
	$result = $xoopsDB->query($sql) or redirect_header($_SERVER['PHP_SELF'],3, mysql_error());

	$function_title=($show_function)?"<th>功能</th>":"";

	$all_content="";

	while($all=$xoopsDB->fetchArray($result)){
		//以下會產生這些變數: $tel , $email , $name , $gsn , $sn , $birthday , $zip , $county , $city , $addr
		foreach($all as $k=>$v){
			$$k=$v;
		}

		$fun=($show_function)?"
		<td>
		<a href='{$_SERVER['PHP_SELF']}?op=contact_form&sn=$sn' class='link_button'>編輯</a>
		<a href=\"javascript:delete_contact_func($sn);\" class='link_button'>刪除</a>
		</td>":"";


		$cate=get_contact_cate_all();

		$all_content.="
		<tr>
			<td>{$cate[$gsn]['title']}</td>
			<td>{$name}</td>
			<td>{$tel}</td>
			<td>{$email}</td>
			<td>{$birthday}</td>
			<td>{$zip}</td>
			<td>{$county}</td>
			<td>{$city}</td>
			<td>{$addr}</td>
			$fun
		</tr>
		";
	}

	//if(empty($all_content))return "";

	$add_button=($show_function)?"<a href='{$_SERVER['PHP_SELF']}?op=contact_form' class='link_button_r'>新增</a>":"";

	//刪除確認的JS
	$main="
	<script>
	function delete_contact_func(sn){
		var sure = window.confirm('確定刪除此資料?');
		if (!sure)	return;
		location.href=\"{$_SERVER['PHP_SELF']}?op=delete_contact&sn=\" + sn;
	}
	</script>

	<table summary='list_table' id='tbl' style='width:100%;'>
	<tr>
		<th>群組</th>
		<th>姓名</th>
		<th>電話</th>
		<th>信箱</th>
		<th>生日</th>
		<th>郵遞區號</th>
		<th>縣市</th>
		<th>鄉鎮市區</th>
		<th>地址</th>
		$function_title
	</tr>

	<tbody>
	$all_content
	</tbody>

	<tr>
		<td colspan=11 class='bar'>
		{$add_button}
		</td>
	</tr>
	</table>";

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

	return $main;
}


//以流水號取得某筆contact資料
function get_contact($sn=""){
	global $xoopsDB;
	if(empty($sn))return;
	$sql = "select * from `".$xoopsDB->prefix("contact")."` where `sn` = '{$sn}'";
	$result = $xoopsDB->query($sql) or redirect_header($_SERVER['PHP_SELF'],3, mysql_error());
	$data=$xoopsDB->fetchArray($result);
	return $data;
}

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


//取得所有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":
  $sn=insert_contact();
  header("location: {$_SERVER['PHP_SELF']}?sn=$sn");
  break;

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

  //輸入表格
  case "contact_form":
  $main=contact_form($sn);
  break;

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

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

	
	/*---判斷動作請貼在上方---*/
}

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

?>

:::

搜尋

QR Code 區塊

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

書籍目錄

展開 | 闔起

線上使用者

47人線上 (13人在瀏覽線上書籍)

會員: 0

訪客: 47

更多…