アンケート −PHP入門サンプル置き場

PHP入門向けのサンプルソースを公開しています。

アンケート

questionnaire.php

<html> <head> <title>アンケート</title> <meta http-equiv="Content-Type" content="text/html; charset=Shift_JIS"> </head> <body>あなたの好きな教科を選んで投票してください。 <form name="form" method="post" action="questionnaire.php"> <?php //ラジオボタンを項目ごとに出力 $subject=array('国語','社会','数学','理科','英語'); for($i=0; $i<count($subject); $i++){  echo "<input type='radio' name='kyouka' value='$i'>{$subject[$i]}<br>\n"; } ?> <br> <input type="submit" name="submit" value="投票"> </form> <table border="1"> <?php //データの書き込み $data=file('data.txt'); for($i=0; $i<count($subject); $i++){ $data[$i]=rtrim($data[$i]); } if($_POST['submit']){ $data[$_POST['kyouka']]++; $fp=@fopen('data.txt','w'); for($i=0; $i<count($subject); $i++){ fwrite($fp,$data[$i]."\n"); } fclose($fp); } //データの出力 echo "<hr>"; for($i=0; $i<count($subject); $i++){ echo "<tr>"; echo "<td>{$subject[$i]}</td>"; echo "<td><table><tr>"; $wd=$data[$i]*10; //出力幅の設定 echo "<td width='$wd' bgcolor='#eeeeee'> </td>"; echo "<td>{$data[$i]} 票</td>"; echo "</tr></table></td>"; echo "</tr>\n"; } ?> </table> </body> </html>

実行ページ

補足情報

アンケート結果のグラフ表示に<table>タグを使用しました。票数によって、出力幅を変更させるとよいでしょう。