線上書籍

Home

[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'");  //設定語系

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);
    $sn=mysql_insert_id();
   
    if(!empty($_FILES['pic']['tmp_name'])){
        foreach($_FILES['pic']['tmp_name'] as $i => $tmpname){       
            move_uploaded_file($tmpname , "userfiles/images/{$sn}_{$_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;
    $pic=get_pic($sn);
    $sql="select * from diary where sn='$sn'";
    $result=mysql_query($sql) or die($sql);
    list($sn,$date,$event)=mysql_fetch_row($result);
    $main="<div class='page'>{$event}{$pic}</div>";
    return $main;
}


//抓出圖檔
function get_pic($sn=""){
$dir = "userfiles/images/";
if (is_dir($dir)) {
    if ($dh = opendir($dir)) {
        while (($file = readdir($dh)) !== false) {
            $ff=explode("_",$file);
                        if($ff[0]==$sn){
                            $pics.="<img src='{$dir}{$file}'>";
                        }
        }
        closedir($dh);
    }
    }
    return $pics;
}

?>
<!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"></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" accept="jpg|png|gif|jpeg" maxlength="3"/>   

  <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>