線上書籍

Home

[1002] PHP入門班

<?php //引入共同檔 require_once "header.php"; //變數初始化 $op=isset($_REQUEST['op'])?$_REQUEST['op']:""; //$class_sn= isset($_REQUEST['class_sn'])? intval($_REQUEST['class_sn']) : ""; //流程控制 switch($op){ case "insert": insert_article(); header("location:{$_SERVER['PHP_SELF']}"); break; case "edit": $main=article_form(); break; default: $main=list_article(); break; } //套用樣板 theme("theme.html"); /*************** 功能函數區 **************/ function list_article(){ $now_seme=get_seme(); //設定SQL語法 $sql="select a.* , b.class_name from `tncomu_article` as a left join `tncomu_class` as b on a.class_sn=b.class_sn where a.enable='1' and b.seme='$now_seme' order by a.post_time desc"; //執行SQL語法 $result = mysql_query($sql) or die("無法執行:".mysql_error()); $main=" <script> function delete_func(sn){ var sure = window.confirm('確定要刪除此資料?'); if (!sure) return; location.href='{$_SERVER['PHP_SELF']}?op=delete&sn=' + sn; } </script> <table border=1>"; while($data=mysql_fetch_assoc($result)){ //$sql="select class_name from `tncomu_class` where class_sn='{$data['class_sn']}'"; //$result2 = mysql_query($sql) or die("無法執行:".mysql_error()); //list($class_name)=mysql_fetch_row($result2); $main.=" <tr> <td>{$data['class_name']}</td> <td>{$data['stud_name']}</td> <td>{$data['post_time']}</td> <td>{$data['counter']}</td> <td><a href='javascript:delete_func({$data['sn']})'>刪除</a> | <a href='{$_SERVER['PHP_SELF']}?class_sn={$data['sn']}&op=edit'>編輯</a></td> </tr>"; } $main.="</table>"; return $main; } //輸入學習收藏的表單 function article_form(){ $now_seme=get_seme(); $sql="select class_sn,class_name from tncomu_class where access='1' and seme='{$now_seme}' "; $result=mysql_query($sql) or die($sql); $opt=""; while(list($class_sn , $class_name) = mysql_fetch_row($result)){ $opt.="<option value='$class_sn'>$class_name</option>"; } $main="<h3 style='color:#0066CC'>輸入學習收藏</h3> <form action='{$_SERVER['PHP_SELF']}' method='post'> <table> <tr> <th>您的姓名:</th> <td><input type='text' name='stud_name' size='10'></td> <th>{$now_seme}班級:</th> <td> <select name='class_sn'> <option value=''>請選擇{$now_seme}班級</option> $opt </select> </td> </tr> <tr> <th>學習收藏內容</th> <td colspan=3><textarea name='content' cols=50 rows=8></textarea></td> </tr> <tr> <th>設定密碼:</th> <td><input type='text' name='text_passwd' size='10'></td> <th>是否發布?</th> <td> <input type='radio' name='enable' value='1' id='enable'><label for='enable'>發布</label> <input type='radio' name='enable' value='0' id='unable'><label for='unable'>暫不發布</label> <input type='hidden' name='op' value='insert'> <input type='submit' value='儲存'> </td> </tr> </table> </form> "; return $main; } //執行儲存動作 function insert_article(){ $stud_name=trim($_POST['stud_name']); $stud_name = (! get_magic_quotes_gpc()) ? addslashes($stud_name) : $stud_name; $_POST['content'] = (! get_magic_quotes_gpc()) ? addslashes($_POST['content']) : $_POST['content']; $_POST['text_passwd'] = (! get_magic_quotes_gpc()) ? addslashes($_POST['text_passwd']) : $_POST['text_passwd']; $class_sn=intval($_POST['class_sn']); $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']}')"; mysql_query($sql) or die(mysql_error().$sql); return "儲存完畢"; } ?>