[981]PHP入門+XOOPS模組設計
<?php
include_once "../../mainfile.php";
include_once "function.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:view.php?op=show&sn=$sn");
break;
case "update":
$sn= update();
header("location:view.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,$xoopsUser,$xoopsModuleConfig;
$i=0;
$tbl=$xoopsDB->prefix('contact');
$sql="select * from $tbl";
//PageBar(資料數, 每頁顯示幾筆資料, 最多顯示幾個頁數選項);
$result = $xoopsDB->query($sql) or redirect_header($_SERVER['PHP_SELF'],3, mysql_error());
$total=$xoopsDB->getRowsNum($result);
$navbar = new PageBar($total, $xoopsModuleConfig['show_num'], 10);
$mybar = $navbar->makeBar();
$bar= sprintf(_BP_TOOLBAR,$mybar['total'],$mybar['current'])."{$mybar['left']}{$mybar['center']}{$mybar['right']}";
$sql.=$mybar['sql'];
//分頁工具列為 $bar
$result=$xoopsDB->query($sql) or die(mysql_error());
$tool_title=($xoopsUser)?"<th>功能</th>":"";
$main="<table border=1 style='width:auto;'>
<tr><th>相片</th><th>姓名</th><th>性別</th><th>Email</th>$tool_title</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'>":"沒有圖";
$tool=($xoopsUser)?"<td>
<a href='index.php?op=modify&sn={$data['sn']}'>編輯</a> |
<a href='index.php?op=del&sn={$data['sn']}'>刪除</a>
</td>":"";
$main.= "
<tr bgcolor='$color'>
<td>$pic</td>
<td><a href='view.php?sn={$data['sn']}'>{$data['name']}</a></td>
<td>{$sex}</td>
<td>{$data['email']}</td>
$tool
</tr>";
}
$main.="</table>
<p>$bar</p>";
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=""){
global $xoopsUser,$xoopsModule;
if(!$xoopsUser){
//header("location:index.php");
redirect_header("../../user.php",3,"您沒有權限,請先登入。");
}
//修改模式
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";
}
$MDIR=$xoopsModule->getVar('dirname');
include(XOOPS_ROOT_PATH."/modules/{$MDIR}/class/fckeditor/fckeditor.php") ;
$oFCKeditor = new FCKeditor('note') ;
$oFCKeditor->BasePath = XOOPS_URL."/modules/{$MDIR}/class/fckeditor/" ;
$oFCKeditor->Config['AutoDetectLanguage']=false;
$oFCKeditor->Config['DefaultLanguage'] = 'zh' ;
$oFCKeditor->ToolbarSet ='my';
$oFCKeditor->Width = '550' ;
$oFCKeditor->Height = '200' ;
$oFCKeditor->Value =$data['note'];
$editor=$oFCKeditor->CreateHtml() ;
include_once XOOPS_ROOT_PATH."/class/xoopsformloader.php";
$XoopsFormCaptcha=new XoopsFormCaptcha('','',false);
$Captcha=$XoopsFormCaptcha->render();
$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.myform;
//初始化縣市鄉鎮選單
initListGroup('county', form.city, form.town);
setTimeout('initLists()',0);
}
//縣市選單
function initLists() {
var form = document.myform;
form.city.selectedIndex = $city_def;
form.city.onchange();
setTimeout('initLists2()',0);
}
//鄉鎮選單
function initLists2() {
var form = document.myform;
form.town.selectedIndex = $town_def;
}
</script>
<form action='index.php' method='post' name='myform' 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>
$editor</td></tr>
<tr><th>圖形驗證</th><td>
$Captcha</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 save_data(){
global $xoopsDB,$xoopsUser;
xoops_load('captcha');
$xoopsCaptcha = XoopsCaptcha::getInstance();
if(!$xoopsCaptcha->verify()) {
redirect_header($_SERVER['PHP_SELF'], 5, $xoopsCaptcha->getMessage());
}
if($xoopsUser){
$uid=$xoopsUser->uid();
}
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`,`uid`) 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(),'$uid')";
$xoopsDB->query($sql) or die(mysql_error());
$sn=$xoopsDB->getInsertId();
upload($sn);
return $sn;
}
//更新資料
function update(){
global $xoopsDB,$xoopsUser;
xoops_load('captcha');
$xoopsCaptcha = XoopsCaptcha::getInstance();
if(!$xoopsCaptcha->verify()) {
redirect_header($_SERVER['PHP_SELF'], 5, $xoopsCaptcha->getMessage());
}
if($xoopsUser){
$uid=$xoopsUser->getVar('uid');
}
if(!empty($_POST['show_data'])){
$show_data=implode(";",$_POST['show_data']);
}
$tbl=$xoopsDB->prefix('contact');
$sql="update `$tbl` 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(),
`uid`='{$uid}'
where sn='{$_POST['sn']}'";
$xoopsDB->queryF($sql) or die(mysql_error());
upload($_POST['sn']);
return $_POST['sn'];
}
function upload($sn=""){
if(empty($_FILES['pic']['name'])){
return;
}
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);
}
}
}
?>