| 第十一週滿意度問卷設計與計算<?php // 檔案操作 $fp=fopen("test.txt","w"); // 開啟檔案 寫入 for ($i=1; $i<=100; $i++) { fputs($fp,$i); // 寫入檔案 fputs($fp,"\n"); // 跳列 } fclose($fp); // 關檔 $fp=fopen("test.txt","r"); // 開檔 輸入 while ($line=fgets($fp)) { // 讀入檔案,每次一列 echo $line; // 顯示於瀏覽器 echo "<br>"; // 跳列 }; fclose($fp); // 關檔 ?> <?php $command=$_POST["command"]; if ($command==null) { echo("<html><body><center> <form method=post action=''> 選項:<br> <input type=checkbox name=choice1[] value=1>choice 1 <br> <input type=checkbox name=choice1[] value=2>choice 2 <br> <input type=checkbox name=choice1[] value=3>choice 3 <br> <input type=checkbox name=choice1[] value=4>choice 4 <br> <input type=checkbox name=choice1[] value=5>choice 5 <br> <input type=submit name=command value=送出> <input type=reset name=command value=清除> </form> </center></body></html>"); } if ($command=="送出") { $choice1=$_POST["choice1"]; $cnt=count($choice1); for ($i=0; $i<$cnt; $i++) { echo "選項:".$choice1[$i]."<br>"; } } ?>
作業 : 設計滿意度問卷五題 例如 1. 對學校的滿意度 2. 對教師的滿意度 將題目建立到一個檔案中,第一個頁面讀取檔案內容,並以checkbox方式顯示 第二個頁面計算 平均數 答 1 的個數 答 2 的個數 答 3 的個數 答 4 的個數 答 5 的個數
參考下列程式
<?php $command=$_POST["command"]; if ($command==null) { $fp=fopen("item.txt","r"); $item_cnt=0; echo "<html><body> <form method=post action=''>"; while (($item=fgets($fp))!=null) { if (trim($item)=="") continue; $item_cnt=$item_cnt+1; $var="choice$item_cnt" . "[]"; echo "$item <input type=checkbox name='$var' value=1> <input type=checkbox name='$var' value=2> <input type=checkbox name='$var' value=3> <input type=checkbox name='$var' value=4> <input type=checkbox name='$var' value=5><br>"; } echo "<input type='submit' name='command' value='送出'> <input type='hidden' name='item_cnt' value='$item_cnt'> </form></body></html>"; } if ($command=="送出") { $item_cnt=$_POST["item_cnt"]; $total_score=0; for ($i=1; $i<=$item_cnt; $i++) { $ans_list[$i]=0; } for ($i=1; $i<=$item_cnt; $i++) { $var="choice$i"; $choice=$_POST[$var]; $ans=$choice[0]; $total_score=$total_score+$ans; $ans_list[$ans]=$ans_list[$ans]+1; } $average=$total_score/$item_cnt; $average=round($average,2); echo "平均=$average<br>"; for ($i=1; $i<=5; $i++) { echo "答 $i 者有 " . $ans_list[$i] . "個<br>"; } } ?> | |