[981]PHP入門+XOOPS模組設計
<?php
include_once "../../mainfile.php";
define("_DIR",XOOPS_ROOT_PATH."/uploads/tad_contact");
$op=(empty($_REQUEST['op']))?"":$_REQUEST['op'];
$sn=(empty($_REQUEST['sn']))?"0":intval($_REQUEST['sn']);
switch($op){
case "show":
$main= show_data($sn);
break;
case "save":
$sn= save_data();
header("location:index.php?op=show&sn=$sn");
break;
case "update":
$sn= update();
header("location:index.php?op=show&sn=$sn");
break;
case "add":
$main= input_form(0,"#6699ff");
break;
case "del":
del_contact($sn);
header("location:index.php");
break;
case "modify":
$main= input_form(0,"#6699ff",$sn);
break;
default:
$main= list_all();
break;
}
include_once XOOPS_ROOT_PATH."/header.php";
echo $main;
include_once XOOPS_ROOT_PATH.'/footer.php';
//刪除
function del_contact($sn){
$sql="delete from `x233_contact` where sn='{$sn}' ";
mysql_db_query("tad_9801",$sql) or die(mysql_error());
}
//顯示所有結果
function list_all(){
global $xoopsDB;
$i=0;
$tbl=$xoopsDB->prefix('contact');
$sql="select * from $tbl";
$result=$xoopsDB->query($sql) or die(mysql_error());
$main="<table border=1 align='center'>
<tr><th>相片</th><th>姓名</th><th>性別</th><th>Email</th><th>功能</th></tr>";
while($data=$xoopsDB->fetchArray($result)){
$sex=($data['sex']=="m")?"先生":"女士";
$color=($i % 2)?"white":"#f0f0f0";
$i++;
$photo=_DIR."/{$data['sn']}_s.png";
$show_photo=XOOPS_URL."/uploads/tad_contact/{$data['sn']}_s.png";
$pic=(file_exists($photo))?"<img src='$show_photo'>":"沒有圖";
$main.= "
<tr bgcolor='$color'>
<td>$pic</td>
<td><a href='index.php?op=show&sn={$data['sn']}'>{$data['name']}</a></td>
<td>{$sex}</td>
<td>{$data['email']}</td>
<td>
<a href='index.php?op=modify&sn={$data['sn']}'>編輯</a> |
<a href='index.php?op=del&sn={$data['sn']}'>刪除</a> </td>
</tr>";
}
$main.="</table>";
return $main;
}
//單選判斷
function chk($val,$dbv){
if($val==$dbv){
return "checked";
}
}
//多選判斷
function chk2($val="",$dbv_arr=array()){
if(in_array($val,$dbv_arr)){
return "checked";
}
}
//輸入表單
function input_form($border=5,$bgcolor="#cccc66",$sn=""){
//修改模式
if(!empty($sn)){
$sql="select * from `x233_contact` where sn='$sn'";
$result=mysql_db_query("tad_9801",$sql) or die(mysql_error());
$data=mysql_fetch_array($result);
//可秀出資料
$show_data=explode(";",$data['show_data']);
//縣市鄉鎮預設值
include "city_menu/city.php";
$city_def=default_city($data['city']);
$town_def=default_town($data['city'],$data['town']);
if(empty($city_def))$city_def=0;
if(empty($town_def))$town_def=0;
$zip=$data['zip'];
$op="update";
}else{
//新增模式
//可秀出資料
$show_data=array();
$city_def=0;
$town_def=0;
$zip="郵遞區號";
$op="save";
}
$data="
<script src='city_menu/jquery-1.2.6.min.js'></script>
<script>
$(document).ready(function(){
initCS();
var disable_empty_list = true;
});
</script>
<script language='javascript' src='city_menu/chainedselects.js'></script>
<script language='javascript' src='city_menu/city_link.php'></script>
<script language='javascript'>
function initCS() {
var form = document.forms[0];
//初始化縣市鄉鎮選單
initListGroup('county', form.city, form.town);
setTimeout('initLists()',0);
}
//縣市選單
function initLists() {
var form = document.forms[0];
form.city.selectedIndex = $city_def;
form.city.onchange();
setTimeout('initLists2()',0);
}
//鄉鎮選單
function initLists2() {
var form = document.forms[0];
form.town.selectedIndex = $town_def;
}
</script>
<form action='index.php' method='post' enctype='multipart/form-data'>
<table border={$border} align='center' bgcolor='{$bgcolor}'>
<tr><th>姓名</th><td><input type='text' name='name' value='{$data['name']}'></td></tr>
<tr><th>性別</th><td>
<input type='radio' name='sex' value='m' ".chk("m",$data['sex']).">先生
<input type='radio' name='sex' value='f' ".chk("f",$data['sex']).">女士
</td></tr>
<tr><th>Email</th><td><input type='text' name='email' size=30 value='{$data['email']}'></td></tr>
<tr><th>電話</th><td><input type='text' name='tel' size=10 value='{$data['tel']}'></td></tr>
<tr><th>手機</th><td><input type='text' name='mtel' size=10 value='{$data['mtel']}'></td></tr>
<tr><th>地址</th><td>
<input type='text' name='zip' size=5 value='{$zip}' onClick=\"
if(this.value=='郵遞區號'){
this.value='';
}
\">
<select name='city' style='width:80px;'></select>
<select name='town' style='width:80px;'></select>
<input type='text' name='address' size=30 value='{$data['address']}'>
</td></tr>
<tr><th>服務單位</th><td><input type='text' name='company' size=30 value='{$data['company']}'></td></tr>
<tr><th>職稱</th><td><input type='text' name='job' size=10 value='{$data['job']}'></td></tr>
<tr><th>大頭照</th><td><input type='file' name='pic' size=20></td></tr>
<tr><th>可秀出資料</th><td>
<input type='checkbox' name='show_data[]' value='性別' ".chk2("性別",$show_data).">性別
<input type='checkbox' name='show_data[]' value='Email' ".chk2("Email",$show_data).">Email
<input type='checkbox' name='show_data[]' value='電話' ".chk2("電話",$show_data).">電話
<input type='checkbox' name='show_data[]' value='手機' ".chk2("手機",$show_data).">手機
<input type='checkbox' name='show_data[]' value='地址' ".chk2("地址",$show_data).">地址
<input type='checkbox' name='show_data[]' value='服務單位' ".chk2("服務單位",$show_data).">服務單位
<input type='checkbox' name='show_data[]' value='職稱' ".chk2("職稱",$show_data).">職稱
</td></tr>
<tr><th>其他說明</th><td>
<textarea name='note' cols=50 rows=5>{$data['note']}</textarea></td></tr>
</table>
<input type='hidden' name='op' value='$op'>
<input type='hidden' name='sn' value='{$sn}'>
<p align='center'><input type='submit' value='儲存資料'></p>
</form>";
return $data;
}
//顯示結果
function show_data($sn=""){
$sql="select * from `x233_contact` where `sn`='$sn'";
$result=mysql_db_query("tad_9801",$sql) or die(mysql_error());
$data=mysql_fetch_array($result);
$sex=($data['sex']=="m")?"先生":"女士";
//$_POST['note']=stripslashes($_POST['note']);
$data= "<table border=5 align='center'>
<tr><th>姓名</th><td>{$data['name']}</td></tr>
<tr><th>性別</th><td>{$sex}</td></tr>
<tr><th>Email</th><td>{$data['email']}</td></tr>
<tr><th>電話</th><td>{$data['tel']}</td></tr>
<tr><th>手機</th><td>{$data['mtel']}</td></tr>
<tr><th>地址</th><td>
{$data['zip']}{$data['city']}{$data['town']}{$data['address']}
</td></tr>
<tr><th>服務單位</th><td>{$data['company']}</td></tr>
<tr><th>職稱</th><td>{$data['job']}</td></tr>
<tr><th>大頭照</th><td></td></tr>
<tr><th>可秀出資料</th><td>{$data['show_data']}</td></tr>
<tr><th>其他說明</th><td>{$data['note']}</td></tr>
</table>";
return $data;
}
//儲存資料
function save_data(){
global $xoopsDB;
if(!empty($_POST['show_data'])){
$show_data=implode(";",$_POST['show_data']);
}
$tbl=$xoopsDB->prefix('contact');
$sql="insert into `$tbl` (`name`, `sex`, `email`, `tel`, `mtel`, `zip`, `city`, `town`, `addr`, `company`, `jobname`, `pic_type`, `show_data`, `note`, `post_date`) values('{$_POST['name']}' , '{$_POST['sex']}' , '{$_POST['email']}' , '{$_POST['tel']}' , '{$_POST['mtel']}' , '{$_POST['zip']}' , '{$_POST['city']}' , '{$_POST['town']}' , '{$_POST['addr']}' , '{$_POST['company']}' , '{$_POST['jobname']}' , '{$_POST['pic_type']}' , '{$show_data}' , '{$_POST['note']}',now())";
$xoopsDB->query($sql) or die(mysql_error());
$sn=$xoopsDB->getInsertId();
upload($sn);
return $sn;
}
//更新資料
function update(){
if(!empty($_POST['show_data'])){
$show_data=implode(";",$_POST['show_data']);
}
$sql="update `x233_contact` set
`name`='{$_POST['name']}',
`sex`='{$_POST['sex']}',
`email`='{$_POST['email']}',
`tel`='{$_POST['tel']}',
`mtel`='{$_POST['mtel']}',
`zip`='{$_POST['zip']}',
`city`='{$_POST['city']}',
`town`='{$_POST['town']}',
`addr`='{$_POST['addr']}',
`company`='{$_POST['company']}',
`jobname`='{$_POST['jobname']}',
`pic_type`='{$_POST['pic_type']}',
`show_data`='{$show_data}',
`note`='{$_POST['note']}',
`post_date`=now()
where sn='{$_POST['sn']}'";
mysql_db_query("tad_9801",$sql) or die(mysql_error());
upload($_POST['sn']);
return $_POST['sn'];
}
function upload($sn=""){
include('class/upload/class.upload.php');
if(file_exists(_DIR."/{$sn}.png")){
unlink(_DIR."/{$sn}.png");
unlink(_DIR."/{$sn}_s.png");
}
$img_handle = new upload($_FILES['pic'],"zh_TW");
if ($img_handle->uploaded) {
$img_handle->file_safe_name = false;
$img_handle->file_new_name_body = $sn;
$img_handle->image_convert = 'png';
$img_handle->image_resize = true;
$img_handle->image_x = 280;
$img_handle->image_ratio_y = true;
$img_handle->process(_DIR);
if ($img_handle->processed) {
//$img_handle->clean();
} else {
die($img_handle->error);
}
}
if ($img_handle->uploaded) {
$img_handle->file_safe_name = false;
$img_handle->file_new_name_body = $sn."_s";
$img_handle->image_convert = 'png';
$img_handle->image_resize = true;
$img_handle->image_x = 80;
$img_handle->image_ratio_y = true;
$img_handle->process(_DIR);
if ($img_handle->processed) {
$img_handle->clean();
} else {
die($img_handle->error);
}
}
}
?>