:::

17-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']=($data['mode']=="圖片")?"<img src='pic/{$data['content']}'>":$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' enctype='multipart/form-data'>
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 
254      <tr>
255        <th>上傳圖檔:</th>
256        <td colspan=3><input type='file' name='pic' accept='image/*'></td>
257      </tr>
258       
259      <tr>
260        <th>設定密碼:</th>
261        <td><input type='text' name='text_passwd' size='10'></td>
262        <th>是否發布?</th>
263        <td>
264          <input type='radio' name='enable' value='1' id='enable' $radio1><label for='enable'>發布</label>
265          <input type='radio' name='enable' value='0' id='unable' $radio0><label for='unable'>暫不發布</label>
266          <input type='hidden' name='sn' value='$sn'>
267          <input type='hidden' name='op' value='$next_op'>
268          <input type='submit' value='儲存'>
269        </td>
270      </tr>
271    </table>
272  </form>
273  ";
274  return $main;
275}
276 
277 
278//執行儲存動作
279function insert_article(){
280  //過濾姓名
281  $stud_name=trim($_POST['stud_name']);
282  $stud_name=strip_tags($stud_name);
283  $stud_name = (! get_magic_quotes_gpc()) ? addslashes($stud_name) : $stud_name;
284  $stud_name=htmlspecialchars($stud_name);
285  //過濾內容
286  $_POST['content'] = (! get_magic_quotes_gpc()) ? addslashes($_POST['content']) : $_POST['content'];
287  $_POST['content']=htmlspecialchars($_POST['content']);
288  //過濾密碼
289  $_POST['text_passwd'] = (! get_magic_quotes_gpc()) ? addslashes($_POST['text_passwd']) : $_POST['text_passwd'];
290   
291  $class_sn=intval($_POST['class_sn']);
292 
293  $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']}')";
294  mysql_query($sql) or die(mysql_error().$sql);
295   
296  $sn=mysql_insert_id();
297   
298  if($_FILES['pic']['name']){
299    $ext=strtolower(strrchr($_FILES['pic']['name'],"."));
300    move_uploaded_file($_FILES['pic']['tmp_name'],"pic/{$sn}{$ext}");
301     
302    $sql="update `tncomu_article` set `mode`='圖片',content='{$sn}{$ext}' where `sn`='{$sn}'";
303    mysql_query($sql) or die(mysql_error().$sql);
304  }
305  return "儲存完畢";
306}
307 
308//執行更新動作
309function update_article($sn=''){
310 
311  if($sn){
312   //設定SQL語法
313    $sql="select text_passwd from `tncomu_article` where sn='{$sn}'";
314    //執行SQL語法
315    $result=mysql_query($sql) or die("無法執行:".mysql_error());
316    //擷取資料回來存到 $data
317    list($text_passwd)=mysql_fetch_row($result);
318 
319    if($text_passwd!=$_POST['text_passwd'] or empty($_POST['text_passwd'])){
320        return;
321    }
322  }
323 
324  //過濾姓名
325  $stud_name=trim($_POST['stud_name']);
326  $stud_name=strip_tags($stud_name);
327  $stud_name = (! get_magic_quotes_gpc()) ? addslashes($stud_name) : $stud_name;
328  $stud_name=htmlspecialchars($stud_name);
329  //過濾內容
330  $_POST['content'] = (! get_magic_quotes_gpc()) ? addslashes($_POST['content']) : $_POST['content'];
331  $_POST['content']=htmlspecialchars($_POST['content']);
332  //過濾密碼
333  $_POST['text_passwd'] = (! get_magic_quotes_gpc()) ? addslashes($_POST['text_passwd']) : $_POST['text_passwd'];
334 
335  $class_sn=intval($_POST['class_sn']);
336 
337  $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}'";
338  mysql_query($sql) or die(mysql_error().$sql);
339  return "儲存完畢";
340}
341 
342//刪除文章資料
343function delete_article($sn=null){
344 
345  //設定SQL語法
346  $sql="delete from `tncomu_article` where sn='{$sn}'";
347 
348  //執行SQL語法
349  mysql_query($sql) or die("無法執行:".mysql_error());
350 
351  //執行完轉向
352  header("location: {$_SERVER['PHP_SELF']}");
353}
354?>

 


:::

搜尋

QR Code 區塊

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

書籍目錄

展開 | 闔起

線上使用者

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

會員: 0

訪客: 44

更多…