線上書籍

Home

[981]PHP入門+XOOPS模組設計

<?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);

include_once "up_file.php";


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();
   
    upload_file("diary_sn",$sn);
   
    header("location:index2.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='index2.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=show_files("diary_sn",$sn);
    $main="<div class='page'>
    $event
    $pic
    </div>";
    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="upload/jquery.MultiFile.js"></script>
  </head>
  <body>
  <?php echo show_event($_GET['sn']);?>
  <form action="index2.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='upfile[]' class='multi' maxlength=5>
    <?php list_del_file("diary_sn",$sn);?>
  <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>