:::

14-1 上課範例:index.php

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

:::

搜尋

QR Code 區塊

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

書籍目錄

展開 | 闔起

線上使用者

78人線上 (11人在瀏覽線上書籍)

會員: 0

訪客: 78

更多…