[982]PHP網站開發 進階應用技巧
<?php
$root_pass="12345"; //資料庫root密碼
$db_name="test"; //資料庫名稱
/* 連資料庫檢查 */
$link=mysql_connect("localhost","root",$root_pass); //資料庫連線
mysql_select_db("test");
mysql_query("SET NAMES 'utf8'"); //設定語系
error_reporting(0);
if($_POST['op']=="save"){
save_event();
}
//儲存事件
function save_event(){
$sql="insert into diary (`date`,`event`) values ('{$_POST['date']}','{$_POST['event']}')";
mysql_query($sql) or die($sql);
$id=mysql_insert_id();
if(!empty($_FILES['pic']['tmp_name'])){
foreach($_FILES['pic']['tmp_name'] as $i=>$tmp_name){
move_uploaded_file($tmp_name , "myfiles/{$id}_{$_FILES['pic']['name'][$i]}");
}
}
header("location:index.php");
}
//讀取事件的下拉選單
function select_event($select_sn=""){
$sql="select * from diary order by date desc , sn";
$result=mysql_query($sql) or die($sql);
$main="<select onChange=\"location.href='index.php?sn='+this.value\">
<option value=''>撰寫日記</option>";
while(list($sn,$date,$event)=mysql_fetch_row($result)){
$selected=($select_sn==$sn)?"selected":"";
$main.="<option value='$sn' $selected>$date</option>";
}
$main.="</select>";
return $main;
}
//讀取某個事件
function show_event($sn=""){
if(empty($sn))return;
$sql="select * from diary where sn='$sn'";
$result=mysql_query($sql) or die($sql);
list($sn,$date,$event)=mysql_fetch_row($result);
$pic=get_pic($sn);
$main="<div class='page'>
$event
$pic
</div>";
return $main;
}
//讀取圖檔
function get_pic($sn=""){
$dir="myfiles";
include_once('easyphpthumbnail.class.php');
$main="<script type='text/javascript' language='javascript' src='lytebox/lytebox.js'></script>
<link rel='stylesheet' href='lytebox/lytebox.css' type='text/css' media='screen' />";
if (is_dir($dir)) {
if ($dh = opendir($dir)) {
while (($file = readdir($dh)) !== false) {
$ff=explode("_",$file);
if($ff[0]==$sn){
if(!file_exists("{$dir}/_thumbs/{$file}")){
$thumb = new easyphpthumbnail;
$thumb -> Chmodlevel = '0755';
$thumb -> Thumblocation = "{$dir}/_thumbs/";
$thumb -> Thumbprefix = '';
$thumb -> Thumbheight=100 ;
$thumb -> Backgroundcolor = '#F0F0F0';
$thumb -> Shadow = true;
$thumb -> Clipcorner = array(2,10,0,0,0,1,1);
$thumb -> Framewidth = 6;
$thumb -> Framecolor = '#F0F0F0';
$thumb -> Binder = true;
$thumb -> Binderspacing = 10;
$thumb -> Watermarkpng = 'watermark.png';
$thumb -> Watermarkposition = '20% 90%'; //位置
$thumb -> Watermarktransparency = 100; //透明度(數字越大,越不透明)
$thumb -> Copyrightfonttype = 'cwheib.ttf';
$thumb -> Copyrightfontsize = 10;
//$thumb -> Copyrighttextcolor = '#FFFFFF'; //不設會自動偵測
$thumb -> Copyrighttext = '點兩下';
$thumb -> Copyrightposition = '15% 15%';
$thumb -> Createthumb("{$dir}/{$file}",'file');
}
$main.="<a href='$dir/$file' rel='lyteshow[{$ff[0]}]' title='$file'><img src='{$dir}/_thumbs/{$file}' alt='$file' style='border:0px;'></a>
";
}
}
closedir($dh);
}else{
return "無法開啟目錄";
}
}else{
return "無法讀取圖檔目錄";
}
return $main;
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<link rel='stylesheet' type='text/css' media='screen' href='style.css' />
<title>我的記事本</title>
<script type="text/javascript" src="ckeditor/ckeditor.js"></script>
<script src="js/jquery-1.3.2.min.js" type="text/javascript"></script>
<script src="js/jquery_ui_datepicker/jquery_ui_datepicker.js" type="text/javascript"></script>
<script type="text/javascript">
$(function() {
$('#date').datepicker({
userLang : 'zh-TW',
americanMode: false,
dateFormat: 'yy-mm-dd'
});
});
</script>
<script src="js/jquery_ui_datepicker/i18n/ui.datepicker-zh-TW.js" type="text/javascript"></script>
<link rel="stylesheet" type="text/css" href="js/jquery_ui_datepicker/smothness/jquery_ui_datepicker.css">
<script src="js/jquery.MultiFile.js" type="text/javascript" language="javascript"></script>
</head>
<body>
<?php echo show_event($_GET['sn']);?>
<form action="index.php" method="post" enctype="multipart/form-data">
日期:<input name="date" type="text" id="date" size="20">
<?php echo select_event($_GET['sn']);?><br>
插圖:<input type="file" name="pic[]" class="multi"/>
<input type="submit" value="儲存" id="submit"><br>
<textarea name='event' cols=80 rows=15 class="ckeditor"></textarea>
<input name="op" type="hidden" value="save">
</form>
</body>
</html>