[981]PHP入門+XOOPS模組設計
<?php
$link=mysql_connect("localhost","root","12345");
mysql_query("SET NAMES 'utf8'");
if(empty($link)){
echo mysql_error();
}
$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 "add":
$main= input_form(0,"#6699ff");
break;
default:
$main= list_all();
break;
}
$web_title="Tad的通訊錄";
include_once('tbs_class.php');
$TBS =& new clsTinyButStrong ;
$TBS->LoadTemplate('theme.html',False) ;
$TBS->Show() ;
//顯示所有結果
function list_all(){
global $link;
$sql="select * from `contact`";
$result=mysql_db_query("tad",$sql) or die(mysql_error());
$main="<table border=1 align='center'>
<tr><th>姓名</th><th>性別</th><th>Email</th></tr>";
while($data=mysql_fetch_array($result)){
$sex=($data['sex']=="m")?"先生":"女士";
$color=($i % 2)?"white":"#cfcfcf";
$i++;
$main.= "
<tr bgcolor='$color'>
<td><a href='index.php?op=show&sn={$data['sn']}'>{$data['name']}</a></td>
<td>{$sex}</td>
<td>{$data['email']}</td>
</tr>";
}
$main.="</table>";
return $main;
}
//輸入表單
function input_form($border=5,$bgcolor="#cccc66"){
$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 = 0;
form.city.onchange();
setTimeout('initLists2()',0);
}
function initLists2() {
var form = document.forms[0];
form.town.selectedIndex = 0;
}
</script>
<form action='index.php' method='post'>
<table border={$border} align='center' bgcolor='{$bgcolor}'>
<tr><th>姓名</th><td><input type='text' name='name' value='{$_POST['name']}'></td></tr>
<tr><th>性別</th><td>
<input type='radio' name='sex' value='m'>先生
<input type='radio' name='sex' value='f'>女士
</td></tr>
<tr><th>Email</th><td><input type='text' name='email' size=30 value='{$_POST['email']}'></td></tr>
<tr><th>電話</th><td><input type='text' name='tel' size=10 value='{$_POST['tel']}'></td></tr>
<tr><th>手機</th><td><input type='text' name='mtel' size=10 value='{$_POST['mtel']}'></td></tr>
<tr><th>地址</th><td>
<input type='text' name='zip' size=5 value='郵遞區號' 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='{$_POST['address']}'>
</td></tr>
<tr><th>服務單位</th><td><input type='text' name='company' size=30 value='{$_POST['company']}'></td></tr>
<tr><th>職稱</th><td><input type='text' name='job' size=10 value='{$_POST['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='性別'>性別
<input type='checkbox' name='show_data[]' value='Email'>Email
<input type='checkbox' name='show_data[]' value='電話'>電話
<input type='checkbox' name='show_data[]' value='手機'>手機
<input type='checkbox' name='show_data[]' value='地址'>地址
<input type='checkbox' name='show_data[]' value='服務單位'>服務單位
<input type='checkbox' name='show_data[]' value='職稱'>職稱
</td></tr>
<tr><th>其他說明</th><td>
<textarea name='note' cols=50 rows=5>{$_POST['note']}</textarea></td></tr>
</table>
<input type='hidden' name='op' value='save'>
<p align='center'><input type='submit' value='儲存資料'></p>
</form>";
return $data;
}
//顯示結果
function show_data($sn=""){
$sql="select * from `contact` where `sn`='$sn'";
$result=mysql_db_query("tad",$sql) or die(mysql_error());
$data=mysql_fetch_array($result);
//$_POST['note']=stripslashes($_POST['note']);
$data= "<table border=5 align='center'>
<tr><th>姓名</th><td>{$data['name']}</td></tr>
<tr><th>性別</th><td>{$data['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(){
if(!empty($_POST['show_data'])){
$show_data=implode(";",$_POST['show_data']);
}
$sql="insert into contact (`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())";
mysql_db_query("tad",$sql) or die(mysql_error());
$sn=mysql_insert_id();
return $sn;
}
?>