:::

10-1 上課範例:index.php

001<?php
002/*** 引入檔案 ***/
003include_once '../../mainfile.php';
004include_once XOOPS_ROOT_PATH."/header.php";
005 
006/*** 函數檔 ***/
007 
008//新增記事表單
009function add_form($note_sn=null){
010  global $xoopsDB,$xoopsUser;
011 
012  if(empty($xoopsUser))redirect_header('index.php', 3, "請先登入。");
013 
014  if($note_sn){
015    $sql="select * from ".$xoopsDB->prefix("tad_notes")." where `note_sn`='$note_sn'";
016    $result=$xoopsDB->query($sql) or redirect_header('index.php', 3, mysql_error());
017    $doc=$xoopsDB->fetchArray($result);
018  }
019 
020 
021  include_once(XOOPS_ROOT_PATH."/class/xoopsformloader.php");
022  $XoopsFormHiddenToken=new XoopsFormHiddenToken();
023  $token=$XoopsFormHiddenToken->render();
024 
025  $option="";
026  //抓取資料庫中的分類選項
027  $sql="select * from ".$xoopsDB->prefix("tad_note_cate")." where cate_enable='1' order by `cate_sort`";
028  $result = $xoopsDB->query($sql) or redirect_header('index.php', 3, mysql_error());
029  while($cate=$xoopsDB->fetchArray($result)){
030    $selected=($cate['cate_sn']==$doc['cate_sn'])?"selected":"";
031    $option.="<option value='{$cate['cate_sn']}' $selected>{$cate['cate_title']}</option>";
032  }
033   
034  //取得最大排序
035  $note_sort=empty($note_sn)?get_max_sort():$doc['note_sort'];
036 
037  //取得現在時間
038  $note_date=empty($note_sn)?date("Y-m-d H:i:s"):$doc['note_date'];
039 
040  $note_public1=($doc['note_public']=='1')?"checked":"";
041  $note_public0=($doc['note_public']=='0')?"checked":"";
042 
043 
044  $main="
045  <script type='text/javascript' src='class/ckeditor/ckeditor.js'></script>
046  <script language='javascript' type='text/javascript' src='class/DatePicker/WdatePicker.js'></script>
047  <h3>記事編輯</h3>
048  <form action='{$_SERVER['PHP_SELF']}' method='post'>
049    <table>
050      <tr><th nowrap>所屬分類</th><td>
051        <select name='cate_sn'>
052        <option value='0'>不分類</option>
053        $option
054        </select>
055        </td></tr>
056      <tr><th>文章標題</th><td><input type='text' name='note_title' size=40 value='{$doc['note_title']}'></td></tr>
057      <tr><td colspan=2>
058        <textarea name='note_content' cols=40 rows=6 class='ckeditor' id='ckeditor'>{$doc['note_content']}</textarea>
059        <script type='text/javascript'>
060          CKEDITOR.replace('ckeditor' , { skin : 'v2' , toolbar : 'MyToolbar' } );
061        </script>
062      </td></tr>
063      <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>
064      <tr><th>是否公開</th><td>
065        <input type='radio' name='note_public' value='1' $note_public1> 是
066        <input type='radio' name='note_public' value='0' $note_public0> 否
067        </td></tr>
068      <tr><th>排序</th><td><input type='text' name='note_sort' size=2 value='$note_sort'></td></tr>
069    </table>
070    $token
071    <input type='hidden' name='op' value='save'>
072    <input type='submit' value='儲存'>
073 
074  </form>
075  ";
076 
077 
078  /*
079  所屬分類 cate_sn
080  文章標題 note_title
081  文章內容 note_content
082  發布日期 note_date
083  是否公開 note_public
084  排序 note_sort
085  */
086  return $main;
087}
088 
089 
090//儲存文章
091function save(){
092  global $xoopsDB , $xoopsUser;
093 
094  if(!$GLOBALS['xoopsSecurity']->check()){
095    $error=implode("<br />" , $GLOBALS['xoopsSecurity']->getErrors());
096    redirect_header($_SERVER['PHP_SELF'],3, $error);
097  }
098 
099  $myts =& MyTextSanitizer::getInstance();
100  $_POST['note_title'] = $myts->addSlashes($_POST['note_title']);
101  $_POST['note_content'] = $myts->addSlashes($_POST['note_content']);
102  $_POST['note_date'] = $myts->addSlashes($_POST['note_date']);
103  $_POST['note_sort'] = $myts->addSlashes($_POST['note_sort']);
104 
105  $uid = empty($xoopsUser)? 0 : $xoopsUser->uid();
106 
107  $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']}')";
108  $xoopsDB->query($sql) or redirect_header('index.php', 3, mysql_error());
109}
110 
111//工具列
112function toolbar(){
113  $main="<a href='index.php?op=add_form'>新增記事</a>";
114  return $main;
115}
116 
117//取得最大排序
118function get_max_sort(){
119  global $xoopsDB;
120   
121  $sql="select max(`note_sort`) from ".$xoopsDB->prefix("tad_notes")." where `note_public`='1'";
122  $result=$xoopsDB->query($sql) or redirect_header('index.php', 3, mysql_error());
123  list($max_sort)=$xoopsDB->fetchRow($result);
124  return ++$max_sort;
125}
126 
127//顯示文章列表或單一文章
128function show_doc($note_sn=''){
129  global $xoopsDB,$xoopsUser;
130 
131  $now_uid=($xoopsUser)?$xoopsUser->uid():"";
132 
133  $myts =& MyTextSanitizer::getInstance();
134 
135  if(empty($note_sn)){
136    $sql="select * from ".$xoopsDB->prefix("tad_notes")." where `note_public`='1' order by note_sort";
137    $result=$xoopsDB->query($sql) or redirect_header('index.php', 3, mysql_error());
138    $main="<table>";
139    while($doc=$xoopsDB->fetchArray($result)){
140 
141      $doc['note_title'] = $myts->htmlSpecialChars($doc['note_title']);
142      $doc['note_date'] = $myts->htmlSpecialChars($doc['note_date']);
143     
144      $tool=($doc['uid']==$now_uid)?"<a href='index.php?op=del&note_sn={$doc['note_sn']}'>刪除</a> |
145      <a href='index.php?op=modify&note_sn={$doc['note_sn']}'>修改</a>":"";
146     
147      $main.="<tr>
148      <td><a href='index.php?note_sn={$doc['note_sn']}'>{$doc['note_title']}</a></td>
149      <td>{$doc['note_date']}</td>
150      <td>{$tool}</td>
151      </tr>";
152    }
153    $main.="</table>";
154  }else{
155    $sql="select * from ".$xoopsDB->prefix("tad_notes")." where `note_sn`='$note_sn' and `note_public`='1'";
156    $result=$xoopsDB->query($sql) or redirect_header('index.php', 3, mysql_error());
157    $doc=$xoopsDB->fetchArray($result);
158     
159    $doc['note_title'] = $myts->htmlSpecialChars($doc['note_title']);
160    $doc['note_date'] = $myts->htmlSpecialChars($doc['note_date']);
161    $doc['note_content'] = $myts->displayTarea($doc['note_content'], 1, 1, 0, 1, 0);
162 
163    $main="
164    <h1>{$doc['note_title']}</h1>
165    <div>{$doc['note_date']}</div>
166    <div>{$doc['note_content']}</div>
167    ";
168  }
169  return $main;
170}
171 
172 
173//刪除函數
174function del_note($note_sn=null){
175  global $xoopsDB;
176 
177  $sql="delete from ".$xoopsDB->prefix("tad_notes")." where note_sn='$note_sn'";
178  $xoopsDB->queryF($sql) or redirect_header('index.php', 3, mysql_error());
179}
180 
181 
182/*** 流程判斷 ***/
183$op = empty($_REQUEST['op'])? "" : $_REQUEST['op'];
184$note_sn = empty($_REQUEST['note_sn'])? "" : intval($_REQUEST['note_sn']);
185 
186 
187switch($op){
188 
189  case "del":
190  del_note($note_sn);
191  header("location:index.php");
192  break;
193 
194  case "modify":
195  $main=add_form($note_sn);
196  break;
197 
198 
199  case "save":
200  save();
201  header("location:index.php");
202  break;
203 
204  case "add_form":
205  $main=add_form();
206  break;
207 
208    default:
209    $main=show_doc($note_sn);
210    break;
211}
212 
213/*** 輸出 ***/
214$current1=($_SERVER['REQUEST_URI']=="/~tad0616/modules/tad_note/index.php")?"class='current'":"";
215$current2=($_SERVER['REQUEST_URI']=="/~tad0616/modules/tad_note/index.php?op=add_form")?"class='current'":"";
216 
217echo "
218<link rel='stylesheet' href='menu/menu_style.css' type='text/css' />
219<ul id='menu'>
220   <li><a href='index.php' target='_self' title='所有記事' $current1>所有記事</a></li>
221   <li><a href='index.php?op=add_form' target='_self' title='新增記事' $current2>新增記事</a></li>
222</ul>
223";
224echo $main;
225 
226 
227include_once XOOPS_ROOT_PATH.'/footer.php';
228?>

:::

搜尋

QR Code 區塊

https%3A%2F%2Ftad0616.cp22.secserverpros.com%2Fmodules%2Ftad_book3%2Fpage.php%3Ftbdsn%3D618%26tbsn%3D22

書籍目錄

展開 | 闔起

線上使用者

57人線上 (6人在瀏覽線上書籍)

會員: 0

訪客: 57

更多…