:::

8-3 上課範例:index.php

<?php
/*** 引入檔案 ***/
include_once '../../mainfile.php';
include_once XOOPS_ROOT_PATH."/header.php";

/*** 函數檔 ***/

//新增記事表單
function add_form(){
  global $xoopsDB;


  $option="";
  //抓取資料庫中的分類選項
  $sql="select * from ".$xoopsDB->prefix("tad_note_cate")." where cate_enable='1' order by `cate_sort`";
  $result = $xoopsDB->query($sql) or redirect_header('cate.php', 3, mysql_error());
  while($cate=$xoopsDB->fetchArray($result)){
    $option.="<option value='{$cate['cate_sn']}'>{$cate['cate_title']}</option>";
  }

  //取得現在時間
  $note_date=date("Y-m-d H:i:s");

  $main="
  <script type='text/javascript' src='class/ckeditor/ckeditor.js'></script>
  <script language='javascript' type='text/javascript' src='class/DatePicker/WdatePicker.js'></script>
  <h3>記事編輯</h3>
  <form action='{$_SERVER['PHP_SELF']}' method='post'>
    <table>
      <tr><th nowrap>所屬分類</th><td>
        <select name='cate_sn'>
        <option value='0'>不分類</option>
        $option
        </select>
        </td></tr>
      <tr><th>文章標題</th><td><input type='text' name='note_title' size=40 value=''></td></tr>
      <tr><td colspan=2>
        <textarea name='note_content' cols=40 rows=6 class='ckeditor' id='ckeditor'></textarea>
        <script type='text/javascript'>
          CKEDITOR.replace('ckeditor' , { skin : 'v2' , toolbar : 'MyToolbar' } );
        </script>
      </td></tr>
      <tr><th>發布日期</th><td><input type='text' name='note_date' value='$note_date' onClick=\"WdatePicker({skin:'whyGreen' , dateFmt:'yyyy-MM-dd HH:mm:ss'})\" class='Wdate'></td></tr>
      <tr><th>是否公開</th><td>
        <input type='radio' name='note_public' value='1'> 是
        <input type='radio' name='note_public' value='0'> 否
        </td></tr>
      <tr><th>排序</th><td><input type='text' name='note_sort' size=2></td></tr>
    </table>

    <input type='hidden' name='op' value='save'>
    <input type='submit' value='儲存'>

  </form>
  ";


  /*
  所屬分類 cate_sn
  文章標題 note_title
  文章內容 note_content
  發布日期 note_date
  是否公開 note_public
  排序 note_sort
  */
  return $main;
}


//儲存文章
function save(){
  global $xoopsDB , $xoopsUser;

  $uid = $xoopsUser->uid();

  $sql="insert into ".$xoopsDB->prefix("tad_notes")." (`cate_sn`, `note_title`, `note_content`, `note_date`, `note_public`, `note_count`, `uid`, `note_sort`) values('{$_POST['cate_sn']}' , '{$_POST['note_title']}' , '{$_POST['note_content']}' , '{$_POST['note_date']}' , '{$_POST['note_public']}' , '0' , '{$uid}' , '{$_POST['note_sort']}')";
  $xoopsDB->query($sql) or redirect_header('index.php', 3, mysql_error());
}


function toolbar(){
  $main="<a href='index.php?op=add_form'>新增記事</a>";
  return $main;
}

/*** 流程判斷 ***/
$op=empty($_REQUEST['op'])?"":$_REQUEST['op'];

switch($op){

  case "save":
  save();
  header("location:index.php");
  break;

  case "add_form":
  $main=add_form();
  break;

	default:
	$main=toolbar();
	break;
}

/*** 輸出 ***/
echo $main;
include_once XOOPS_ROOT_PATH.'/footer.php';
?>

:::

搜尋

QR Code 區塊

https%3A%2F%2Fwww.tad0616.net%2Fmodules%2Ftad_book3%2Fpage.php%3Ftbsn%3D22%26tbdsn%3D614

書籍目錄

展開 | 闔起

線上使用者

66人線上 (5人在瀏覽線上書籍)

會員: 0

訪客: 66

更多…