:::

16-1 上課範例:index.php

001<?php
002//引入共同檔
003require_once "header.php";
004 
005//變數初始化
006$op=isset($_REQUEST['op'])?$_REQUEST['op']:"";
007$sn= isset($_REQUEST['sn'])? intval($_REQUEST['sn']) : "";
008 
009//流程控制
010switch($op){
011 
012  case "admin":
013  $main= ($_SESSION['isLeader']) ? list_article($_SESSION['isLeader']) : login_form();
014  break;
015 
016  case "login":
017  leader_login($_POST['class_sn'], $_POST['pass']);
018  header("location:index.php?op=admin");
019  break;
020 
021  case "logout":
022  $_SESSION['isLeader']=null;
023  header("location:index.php");
024  break;
025 
026  case "insert":
027  insert_article();
028  header("location:{$_SERVER['PHP_SELF']}");
029  break;
030   
031  case "edit":
032  $main=article_form($sn);
033  break;
034   
035 
036  case "update":
037  update_article($sn);
038  header("location:{$_SERVER['PHP_SELF']}");
039  break;
040 
041  case "delete":
042  delete_article($sn);
043  header("location:{$_SERVER['PHP_SELF']}");
044  break;
045 
046  default:
047  $main=empty($sn)?list_article():show_article($sn);
048  break;
049}
050 
051 
052//套用樣板
053theme("theme.html");
054 
055/*************** 功能函數區 **************/
056//登入表單
057function login_form(){
058  $now_seme=get_seme();
059  $sql="select class_sn,class_name from tncomu_class where access='1' and seme='{$now_seme}' ";
060  $result=mysql_query($sql) or die($sql);
061  $opt="";
062  while(list($class_sn , $class_name) = mysql_fetch_row($result)){
063    $opt.="<option value='$class_sn'>$class_name</option>";
064  }
065   
066  $main="
067  <form action='{$_SERVER['PHP_SELF']}' method='post'>
068  <select name='class_sn'>
069    $opt
070  </select>
071           
072  密碼:<input type='password' name='pass'>
073  <input type='hidden' name='op' value='login'>
074  <input type='submit' value='登入'>
075  </form>";
076  return $main;
077}
078 
079//進行認證
080function leader_login($class_sn='',$pass=''){
081  if(empty($class_sn) or empty($pass))return;
082 
083   //設定SQL語法
084    $sql="select passwd from `tncomu_class` where class_sn='{$class_sn}'";
085    $result=mysql_query($sql) or die("無法執行:".mysql_error());
086    list($passwd)=mysql_fetch_row($result);
087 
088  if($passwd==$pass){
089    $_SESSION['isLeader']=$class_sn;
090  }
091}
092 
093 
094//秀出某一篇文章
095function show_article($sn=null){
096  $now_seme=get_seme();
097 
098  $sql="update `tncomu_article` set `counter`=`counter`+1 where sn='$sn'";
099  mysql_query($sql) or die("無法執行:".mysql_error());
100 
101  //設定SQL語法
102  $sql="select a.* , b.class_name
103  from `tncomu_article` as a left join `tncomu_class` as b on a.class_sn=b.class_sn
104  where a.enable='1' and a.sn='$sn'";
105 
106  //執行SQL語法
107  $result = mysql_query($sql) or die("無法執行:".mysql_error());
108 
109  $data=mysql_fetch_assoc($result);
110   
111  $data['content']=nl2br($data['content']);
112   
113  $main="
114  <h1>「{$data['stud_name']}」的學習收藏</h1>
115  <div style='text-align:right;margin:10px 0px;'>{$data['class_name']}</div>
116  <div>{$data['content']}</div>
117  <div style='text-align:right;margin:10px 0px;'>{$data['post_time']}</div>
118  ";
119   
120  return $main;
121}
122 
123 
124//列出所有文章
125function list_article($class_sn=null){
126  require_once "pagebar.php";
127  $now_seme=get_seme();
128   
129  $and_class_sn=empty($class_sn)?"":"and a.class_sn='$class_sn'";
130   
131  //設定SQL語法
132  $sql="select a.* , b.class_name
133  from `tncomu_article` as a left join `tncomu_class` as b on a.class_sn=b.class_sn
134  where a.enable='1' and b.seme='$now_seme' $and_class_sn
135  order by a.post_time desc";
136 
137  //PageBar(資料數, 每頁顯示幾筆資料, 最多顯示幾個頁數選項);
138  mysql_query($sql);
139  $total=mysql_affected_rows();
140  $navbar = new PageBar($total, 5, 10);
141  $mybar = $navbar->makeBar();
142  $bar= "<p align='center'>{$mybar['left']}{$mybar['center']}{$mybar['right']}</p>";
143  $sql.=$mybar['sql'];
144 
145 
146  //執行SQL語法
147  $result = mysql_query($sql) or die("無法執行:".mysql_error());
148 
149  $js="";
150  if($_SESSION['isLeader']){
151    $js="
152      <script>
153      function delete_func(sn){
154       var sure = window.confirm('確定要刪除此資料?');
155       if (!sure)   return;
156       location.href='{$_SERVER['PHP_SELF']}?op=delete&sn=' + sn;
157      }
158      </script>";
159  }
160 
161 
162  $main="
163  $js
164  $bar
165  <table>
166  <tr>
167    <th>所屬班級</th>
168    <th>學員姓名</th>
169    <th>發布日期</th>
170    <th>人氣</th>
171    <th>相關功能</th>
172  </tr>";
173 
174  $i=2;
175  while($data=mysql_fetch_assoc($result)){
176   
177    $color=($i % 2)?"white":"#D0D0D0";
178    $i++;
179     
180    $tool=($_SESSION['isLeader']==$data['class_sn'] and !empty($_SESSION['isLeader']))?"| <a href='javascript:delete_func({$data['sn']})'>刪除</a>":"";
181     
182    $main.="
183    <tr style='background-color:$color;'>
184    <td>{$data['class_name']}</td>
185    <td><a href='{$_SERVER['PHP_SELF']}?sn={$data['sn']}'>{$data['stud_name']}</a></td>
186    <td>{$data['post_time']}</td>
187    <td>{$data['counter']}</td>
188    <td><a href = '{$_SERVER['PHP_SELF']}?sn={$data['sn']}&op=edit' >編輯</a>{$tool}</td>
189    </tr>";
190  }
191 
192  $main.="</table>
193  $bar";
194 
195  return $main;
196}
197 
198//輸入學習收藏的表單
199function article_form($sn=''){
200 
201  $next_op="insert";
202 
203  //初始值設定
204  $data['stud_name'] = $data['class_sn'] = $data['content'] = $data['enable'] = $radio1 = $radio0 = "";
205 
206  if($sn){
207   //設定SQL語法
208    $sql="select * from `tncomu_article` where sn='{$sn}'";
209 
210    //執行SQL語法
211    $result=mysql_query($sql) or die("無法執行:".mysql_error());
212 
213    //擷取資料回來存到 $data
214    $data=mysql_fetch_assoc($result);
215 
216    //還原下拉選單預設值
217 
218    $radio1=($data['enable']=="1")?"checked":"";
219    $radio0=($data['enable']=="0")?"checked":"";
220    $next_op="update";
221  }
222 
223 
224  $now_seme=get_seme();
225 
226  $sql="select class_sn,class_name from tncomu_class where access='1' and seme='{$now_seme}' ";
227  $result=mysql_query($sql) or die($sql);
228  $opt="";
229  while(list($class_sn , $class_name) = mysql_fetch_row($result)){
230    $selected = ($class_sn == $data['class_sn'])?"selected":"";
231    $opt.="<option value='$class_sn' $selected>$class_name</option>";
232  }
233 
234 
235  $main="<h3 style='color:#0066CC'>輸入學習收藏</h3>
236  <script type='text/javascript' src='ckeditor/ckeditor.js'></script>
237  <form action='{$_SERVER['PHP_SELF']}' method='post'>
238    <table>
239      <tr>
240        <th>您的姓名:</th>
241        <td><input type='text' name='stud_name' size='10' value='{$data['stud_name']}'></td>
242        <th>{$now_seme}班級:</th>
243        <td>
244          <select name='class_sn'>
245            <option value=''>請選擇{$now_seme}班級</option>
246            $opt
247          </select>
248        </td>
249      </tr>
250      <tr>
251        <td colspan=4><textarea name='content' class='ckeditor' cols=50 rows=8>{$data['content']}</textarea></td>
252      </tr>
253      <tr>
254        <th>設定密碼:</th>
255        <td><input type='text' name='text_passwd' size='10'></td>
256        <th>是否發布?</th>
257        <td>
258          <input type='radio' name='enable' value='1' id='enable' $radio1><label for='enable'>發布</label>
259          <input type='radio' name='enable' value='0' id='unable' $radio0><label for='unable'>暫不發布</label>
260          <input type='hidden' name='sn' value='$sn'>
261          <input type='hidden' name='op' value='$next_op'>
262          <input type='submit' value='儲存'>
263        </td>
264      </tr>
265    </table>
266  </form>
267  ";
268  return $main;
269}
270 
271 
272//執行儲存動作
273function insert_article(){
274  //過濾姓名
275  $stud_name=trim($_POST['stud_name']);
276  $stud_name=strip_tags($stud_name);
277  $stud_name = (! get_magic_quotes_gpc()) ? addslashes($stud_name) : $stud_name;
278  $stud_name=htmlspecialchars($stud_name);
279  //過濾內容
280  $_POST['content'] = (! get_magic_quotes_gpc()) ? addslashes($_POST['content']) : $_POST['content'];
281  $_POST['content']=htmlspecialchars($_POST['content']);
282  //過濾密碼
283  $_POST['text_passwd'] = (! get_magic_quotes_gpc()) ? addslashes($_POST['text_passwd']) : $_POST['text_passwd'];
284   
285  $class_sn=intval($_POST['class_sn']);
286 
287  $sql="INSERT INTO `tncomu_article`(`stud_name`, `content`, `post_time`, `enable`, `class_sn`, `mode`, `text_passwd`) VALUES ('{$stud_name}' , '{$_POST['content']}' , now(), '{$_POST['enable']}', '{$class_sn}', '文字', '{$_POST['text_passwd']}')";
288  mysql_query($sql) or die(mysql_error().$sql);
289  return "儲存完畢";
290}
291 
292 
293//執行更新動作
294function update_article($sn=''){
295 
296  if($sn){
297   //設定SQL語法
298    $sql="select text_passwd from `tncomu_article` where sn='{$sn}'";
299    //執行SQL語法
300    $result=mysql_query($sql) or die("無法執行:".mysql_error());
301    //擷取資料回來存到 $data
302    list($text_passwd)=mysql_fetch_row($result);
303 
304    if($text_passwd!=$_POST['text_passwd'] or empty($_POST['text_passwd'])){
305        return;
306    }
307  }
308 
309  //過濾姓名
310  $stud_name=trim($_POST['stud_name']);
311  $stud_name=strip_tags($stud_name);
312  $stud_name = (! get_magic_quotes_gpc()) ? addslashes($stud_name) : $stud_name;
313  $stud_name=htmlspecialchars($stud_name);
314  //過濾內容
315  $_POST['content'] = (! get_magic_quotes_gpc()) ? addslashes($_POST['content']) : $_POST['content'];
316  $_POST['content']=htmlspecialchars($_POST['content']);
317  //過濾密碼
318  $_POST['text_passwd'] = (! get_magic_quotes_gpc()) ? addslashes($_POST['text_passwd']) : $_POST['text_passwd'];
319 
320  $class_sn=intval($_POST['class_sn']);
321 
322  $sql="update `tncomu_article` set `stud_name`='{$stud_name}', `content`='{$_POST['content']}', `post_time`=now(), `enable`='{$_POST['enable']}', `class_sn`='{$_POST['class_sn']}' where `sn`='{$sn}'";
323  mysql_query($sql) or die(mysql_error().$sql);
324  return "儲存完畢";
325}
326 
327//刪除文章資料
328function delete_article($sn=null){
329 
330  //設定SQL語法
331  $sql="delete from `tncomu_article` where sn='{$sn}'";
332 
333  //執行SQL語法
334  mysql_query($sql) or die("無法執行:".mysql_error());
335 
336  //執行完轉向
337  header("location: {$_SERVER['PHP_SELF']}");
338}
339?>

:::

搜尋

QR Code 區塊

https%3A%2F%2Ftad0616.cp27.secserverpros.com%2Fmodules%2Ftad_book3%2Fpage.php%3Ftbdsn%3D578%26tbsn%3D20

書籍目錄

展開 | 闔起

線上使用者

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

會員: 0

訪客: 67

更多…