線上書籍

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){ //若op是刪除班級,則呼叫刪除班級函數 case "delete": delet_class($class_sn); break; case "edit": $main=class_form($class_sn); break; case "update": update_class($class_sn); break; case "insert": insert_class(); break; default: //否則執行列出班級函數 $main=list_class(); break; } //套用樣板 theme("theme.html"); /*************** 功能函數區 **************/ //建立班級資料的表單 function class_form($class_sn=""){ $next_op="insert"; //初始值設定 $data['seme'] = $data['class_name'] = $data['teacher'] = $data['passwd'] = $opt1 = $opt2 = $opt3 = $opt4 = $opt5 = $opt6 = $opt7 = $opt8 = $radio1 = $radio0 = ""; if($class_sn){ //設定SQL語法 $sql="select * from `tncomu_class` where class_sn='{$class_sn}'"; //執行SQL語法 $result=mysql_query($sql) or die("無法執行:".mysql_error()); //擷取資料回來存到 $data $data=mysql_fetch_assoc($result); //還原下拉選單預設值 $opt1=($data['subject']=="語言與文化")?"selected":""; $opt2=($data['subject']=="生命與健康")?"selected":""; $opt3=($data['subject']=="社會與生活")?"selected":""; $opt4=($data['subject']=="自然與環境")?"selected":""; $opt5=($data['subject']=="美學與藝術")?"selected":""; $opt6=($data['subject']=="資訊與傳播")?"selected":""; $opt7=($data['subject']=="台江分校")?"selected":""; $opt8=($data['subject']=="台江大廟興學")?"selected":""; $radio1=($data['access']=="1")?"checked":""; $radio0=($data['access']=="0")?"checked":""; $next_op="update"; } $main=" <form action='{$_SERVER['PHP_SELF']}' method='post'> 學年學期:<input type='text' name='seme' size='4' value='{$data['seme']}'><br> 所屬學程:<select name='subject' size=1> <option value='語言與文化' $opt1>語言與文化</option> <option value='生命與健康' $opt2>生命與健康</option> <option value='社會與生活' $opt3>社會與生活</option> <option value='自然與環境' $opt4>自然與環境</option> <option value='美學與藝術' $opt5>美學與藝術</option> <option value='資訊與傳播' $opt6>資訊與傳播</option> <option value='台江分校' $opt7>台江分校</option> <option value='台江大廟興學' $opt8>台江大廟興學</option> </select> <br> 班級名稱:<input type='text' name='class_name' size='30' value='{$data['class_name']}'><br> 講師名稱:<input type='text' name='teacher' size='10' value='{$data['teacher']}'><br> 管理密碼:<input type='text' name='passwd' size='10' value='{$data['passwd']}'><br> 是否開放: <input type='radio' name='access' value='1' id='show' $radio1><label for='show'>開放</label> <input type='radio' name='access' value='0' id='hide' $radio0><label for='hide'>隱藏</label> <br> <input type='hidden' name='op' value='{$next_op}'> <input type='hidden' name='class_sn' value='{$class_sn}'> <input type='submit' value='儲存'> </form>"; return $main; } //新增班級資料 function insert_class(){ //SQL新增語法 $sql="insert into tncomu_class (`seme`, `subject`, `class_name`, `teacher`, `passwd`, `access`) values('{$_POST['seme']}' , '{$_POST['subject']}' , '{$_POST['class_name']}' , '{$_POST['teacher']}' , '{$_POST['passwd']}' , '{$_POST['access']}')"; //執行SQL語法 mysql_query($sql) or die("無法執行:".mysql_error()); //執行完轉向 header("location: {$_SERVER['PHP_SELF']}"); } //更新班級資料 function update_class($class_sn=""){ //SQL更新語法 $sql="update tncomu_class set `seme`='{$_POST['seme']}', `subject`='{$_POST['subject']}', `class_name`='{$_POST['class_name']}', `teacher`='{$_POST['teacher']}', `passwd`='{$_POST['passwd']}', `access`='{$_POST['access']}' where class_sn='$class_sn'"; //執行SQL語法 mysql_query($sql) or die("無法執行:".mysql_error()); //執行完轉向 header("location: {$_SERVER['PHP_SELF']}"); } //列出所有班級資料 function list_class(){ //設定SQL語法 $sql="select * from `tncomu_class` order by seme desc, subject"; //執行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&class_sn=' + sn; } </script> <table border=1>"; while($data=mysql_fetch_assoc($result)){ $main.=" <tr> <td>{$data['class_sn']}</td> <td>{$data['seme']}</td> <td>{$data['subject']}</td> <td>{$data['class_name']}</td> <td>{$data['teacher']}</td> <td>{$data['passwd']}</td> <td>{$data['access']}</td> <td><a href='javascript:delete_func({$data['class_sn']})'>刪除</a> | <a href='{$_SERVER['PHP_SELF']}?class_sn={$data['class_sn']}&op=edit'>編輯</a></td> </tr>"; } $main.="</table>"; return $main; } //刪除班級資料 function delet_class($class_sn=null){ //設定SQL語法 $sql="delete from `tncomu_class` where class_sn='{$class_sn}'"; //執行SQL語法 mysql_query($sql) or die("無法執行:".mysql_error()); //執行完轉向 header("location: {$_SERVER['PHP_SELF']}"); } ?>