:::

17-1 上課範例:index.php

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

$xoopsOption['template_main'] = "tad_note_index.html";

/*** 函數檔 ***/

//新增記事表單
function add_form($note_sn=null){
  global $xoopsDB,$xoopsUser,$xoopsTpl;

  if(empty($xoopsUser))redirect_header('index.php', 3, "請先登入。");

  if($note_sn){
    $sql="select * from ".$xoopsDB->prefix("tad_notes")." where `note_sn`='$note_sn'";
    $result=$xoopsDB->query($sql) or redirect_header('index.php', 3, mysql_error());
    $doc=$xoopsDB->fetchArray($result);
    
    $op="update";
  }else{
    $op="save";
  }


  include_once(XOOPS_ROOT_PATH."/class/xoopsformloader.php");
  $XoopsFormHiddenToken=new XoopsFormHiddenToken();
  $token=$XoopsFormHiddenToken->render();

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

  //取得現在時間
  $note_date=empty($note_sn)?date("Y-m-d H:i:s"):$doc['note_date'];

  $note_public1=($doc['note_public']=='1')?"checked":"";
  $note_public0=($doc['note_public']=='0')?"checked":"";


  $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='{$doc['note_title']}'></td></tr>
      <tr><td colspan=2>
        <textarea name='note_content' cols=40 rows=6 class='ckeditor' id='ckeditor'>{$doc['note_content']}</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' $note_public1> 是
        <input type='radio' name='note_public' value='0' $note_public0> 否
        </td></tr>
      <tr><th>排序</th><td><input type='text' name='note_sort' size=2 value='$note_sort'></td></tr>
    </table>
    $token
    <input type='hidden' name='note_sn' value='$note_sn'>
    <input type='hidden' name='op' value='$op'>
    <input type='submit' value='儲存'>

  </form>
  ";


  /*
  所屬分類 cate_sn
  文章標題 note_title
  文章內容 note_content
  發布日期 note_date
  是否公開 note_public
  排序 note_sort
  */
  
  $xoopsTpl->assign("content",$main);
  $xoopsTpl->assign("mode","form");
}


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

  if(!$GLOBALS['xoopsSecurity']->check()){
    $error=implode("<br />" , $GLOBALS['xoopsSecurity']->getErrors());
    redirect_header($_SERVER['PHP_SELF'],3, $error);
  }

  $myts =& MyTextSanitizer::getInstance();
  $_POST['note_title'] = $myts->addSlashes($_POST['note_title']);
  $_POST['note_content'] = $myts->addSlashes($_POST['note_content']);
  $_POST['note_date'] = $myts->addSlashes($_POST['note_date']);
  $_POST['note_sort'] = $myts->addSlashes($_POST['note_sort']);

  $uid = empty($xoopsUser)? 0 : $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());
  $note_sn=$xoopsDB->getInsertId();
  
  //全局
  $extra_tags['NOTE_TITLE'] = $_POST['note_title'];
  $extra_tags['NOTE_URL'] = XOOPS_URL."/modules/tad_note/view.php?note_sn={$note_sn}";
  $notification_handler =& xoops_gethandler('notification');
  $notification_handler->triggerEvent("global", null , "new_note", $extra_tags , null, null,0);
  
  //分類
  if(!empty($_POST['cate_sn'])){
    $cate=get_cate($_POST['cate_sn']);
    $extra_tags['NOTE_CATE_TITLE'] = $cate['cate_title'];
    $notification_handler =& xoops_gethandler('notification');
    $notification_handler->triggerEvent("cate", $_POST['cate_sn'] , "new_cate_note", $extra_tags , null, null,0);
  }
  
  redirect_header('index.php', 1, "新增完成!");
}

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

//取得最大排序
function get_max_sort(){
  global $xoopsDB;
  
  $sql="select max(`note_sort`) from ".$xoopsDB->prefix("tad_notes")." where `note_public`='1'";
  $result=$xoopsDB->query($sql) or redirect_header('index.php', 3, mysql_error());
  list($max_sort)=$xoopsDB->fetchRow($result);
  return ++$max_sort;
}

//顯示文章列表
function list_doc($cate_sn=""){
  global $xoopsDB,$xoopsUser,$xoopsModuleConfig,$xoopsTpl;

  $now_uid=($xoopsUser)?$xoopsUser->uid():"";

  $myts =& MyTextSanitizer::getInstance();

  $and_cate_sn=empty($cate_sn)?"":"and a.cate_sn='$cate_sn'";

  $sql="select a.* , b.`cate_title`
  from ".$xoopsDB->prefix("tad_notes")." as a left join
  ".$xoopsDB->prefix("tad_note_cate")." as b on a.`cate_sn`=b.`cate_sn`
  where a.`note_public`='1' $and_cate_sn
  order by a.`note_sort`";
  
  //PageBar(資料數, 每頁顯示幾筆資料, 最多顯示幾個頁數選項);
  $result = $xoopsDB->query($sql) or redirect_header($_SERVER['PHP_SELF'],3, mysql_error());
  $total=$xoopsDB->getRowsNum($result);

  $navbar = new PageBar($total,$xoopsModuleConfig['show_num'], 10);
  $mybar = $navbar->makeBar();
  $bar= sprintf(_BP_TOOLBAR,$mybar['total'],$mybar['current'])."{$mybar['left']}{$mybar['center']}{$mybar['right']}";
  $sql.=$mybar['sql'];
  //分頁工具列為 $bar
  
  $result=$xoopsDB->query($sql) or redirect_header('index.php', 3, mysql_error());

  $i=0;
  while($doc=$xoopsDB->fetchArray($result)){

    $doc['note_title'] = $myts->htmlSpecialChars($doc['note_title']);
    $doc['note_date'] = $myts->htmlSpecialChars($doc['note_date']);
  
    $tool=($doc['uid']==$now_uid)?"<a href='index.php?op=del&note_sn={$doc['note_sn']}'>刪除</a> |
    <a href='index.php?op=modify&note_sn={$doc['note_sn']}'>修改</a>":"";
  
    $main[$i]['note_sn']=$doc['note_sn'];
    $main[$i]['note_title']=$doc['note_title'];
    $main[$i]['note_date']=$doc['note_date'];
    $main[$i]['tool']=$tool;
    
    $main[$i]['cate_title']=$doc['cate_title'];
    $main[$i]['cate_sn']=$doc['cate_sn'];
    
    $i++;
  }

  $xoopsTpl->assign("news",$main);
  $xoopsTpl->assign("bar",$bar);
  $xoopsTpl->assign("mode","list");

}


//刪除函數
function del_note($note_sn=null){
  global $xoopsDB;

  $sql="delete from ".$xoopsDB->prefix("tad_notes")." where note_sn='$note_sn'";
  $xoopsDB->queryF($sql) or redirect_header('index.php', 3, mysql_error());
}


function update(){
  global $xoopsDB , $xoopsUser;

  if(!$GLOBALS['xoopsSecurity']->check()){
    $error=implode("<br />" , $GLOBALS['xoopsSecurity']->getErrors());
    redirect_header($_SERVER['PHP_SELF'],3, $error);
  }

  $myts =& MyTextSanitizer::getInstance();
  $_POST['note_title'] = $myts->addSlashes($_POST['note_title']);
  $_POST['note_content'] = $myts->addSlashes($_POST['note_content']);
  $_POST['note_date'] = $myts->addSlashes($_POST['note_date']);
  $_POST['note_sort'] = $myts->addSlashes($_POST['note_sort']);

  $uid = empty($xoopsUser)? 0 : $xoopsUser->uid();

  $sql="update ".$xoopsDB->prefix("tad_notes")." set `cate_sn`='{$_POST['cate_sn']}' , `note_title`='{$_POST['note_title']}', `note_content`='{$_POST['note_content']}', `note_date`='{$_POST['note_date']}', `note_public`='{$_POST['note_public']}',   `note_sort`='{$_POST['note_sort']}' where `note_sn`='{$_POST['note_sn']}'";
  $xoopsDB->queryF($sql) or redirect_header('index.php', 3, mysql_error());
}

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


switch($op){

  case "del":
  del_note($note_sn);
  header("location:index.php");
  break;

  case "modify":
  add_form($note_sn);
  break;


  case "save":
  save();
  break;
  

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


  case "add_form":
  add_form();
  break;

	default:
	list_doc($cate_sn);
	break;
}



include_once XOOPS_ROOT_PATH.'/footer.php';
?>

:::

搜尋

QR Code 區塊

https%3A%2F%2Ftad0616.net%2Fmodules%2Ftad_book3%2Fpage.php%3Ftbdsn%3D669%26tbsn%3D22

書籍目錄

展開 | 闔起

線上使用者

63人線上 (29人在瀏覽線上書籍)

會員: 0

訪客: 63

更多…