2008/8/26 久米
アンケートDB enquete  | 
  |||||||||||
| id | question | s0 | s1 | s2 | s3 | s4 | c0 | c1 | c2 | c3 | c4 | 
| 1 | どのスクリプトが得意ですか | PHP | ActionScript | JavaScript | 0 | 0 | 0 | 0 | 0 | ||
| 2 | 今日のお昼はなにを食べたいですか | うな丼 | 天ざる | 上ずし | カップラーメン | 0 | 0 | 0 | 0 | 0 | |
最も基本的な情報だけのデータベースを作ります。
アンケートスクリプト.phpと同じフォルダに、
のようなbar.gifを配置しておく。
result.php?e_id=1などとアクセスする。
/*________*/を埋めて完成させよ。
<?php
//result.php結果表示
require_once($_SERVER['DOCUMENT_ROOT']."/kume/common/db_value.php");
if(!isset($_GET["e_id"]))exit();
$e_id=$_GET["e_id"];
$con=mysql_connect($DBSERVER, $DBUSER, $DBPASSWORD);
if(!$con) exit("保守中です。しばらくしてからアクセス下さい。");
$sel=mysql_select_db($DBNAME, $con);
$sql=/*________________*/;
$rst=mysql_query($sql, $con);
$row=mysql_fetch_array($rst);
mysql_free_result($rst);
$cls=mysql_close($con);
//アンケート内容を取得
$q=nl2br($row["question"]);//質問文
$s=array();
$c=array();
for($i=0; $i<5; $i++){
    if($row["s$i"]=="")break;
    $s[$i]=$row["s$i"];//選択肢
    /*________*///各投票数カウンタ
}
//全投票数計算
$total=0;
for($i=0; $i<count($c); $i++){
    /*________*/
}
//パーセントを計算
$p=array_fill(0, count($c), 0);//$p配列を全て0で埋める
if($total!=0){//ゼロで割らないように
    for($i=0; $i<count($c); $i++){
        $p[$i]=round(/*________*/);//四捨五入
    }
}
//棒グラフを作成
$m="<table border='0' cellspacing='0' cellpadding='2'>";
for($i=0; $i<count($s); $i++){
    $tohyo=" $c[$i]票($p[$i]%)";
    $m.="<tr><td>$s[$i]</td>";
    $m.="<td><img src='bar.gif' height='8' width='/*___*/'>$tohyo</td></tr>";
}
$m.="</table>";
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=Shift_JIS">
<title>アンケート結果</title>
</head>
<body>
<h1>アンケート結果</h1>
<?php print "<h2>$q</h2>"; ?> 
<?php print $m; ?>
<p><a href="question.php?e_id=<?php print $e_id; ?>">投票する</a></p>
</body>
</html>
 | 
  
question.php?e_id=1などとアクセスする。
<?php
//question.php投票form作成
require_once($_SERVER['DOCUMENT_ROOT']."/kume/common/db_value.php");
if(!isset($_GET["e_id"]))exit();
$e_id=$_GET["e_id"];
$con=mysql_connect($DBSERVER, $DBUSER, $DBPASSWORD);
if(!$con) exit("保守中です。しばらくしてからアクセス下さい。");
$sel=mysql_select_db($DBNAME, $con);
$sql="SELECT * FROM enquete WHERE id=$e_id";
$rst=mysql_query($sql, $con);
$row=mysql_fetch_array($rst);
mysql_free_result($rst);
$cls=mysql_close($con);
//アンケート内容を取得
$q=nl2br($row["question"]);//質問文
$s=array();
for($i=0; $i<5; $i++){
    if($row["s$i"]=="")break;
    $s[$i]=$row["s$i"];//選択肢
}
//formタグを作成
$m="";
for($i=0; $i<count($s); $i++){
    $m.="<p><input type='radio' name='count' value='c$i'>";
    $m.=$s[$i]."</p>";
}
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=Shift_JIS">
<title>アンケート</title>
</head>
<body>
<h1>アンケート</h1>
<?php print "<h2>$q</h2>"; ?> 
<form name='form2' method='post' action='question2.php'>
<input type="hidden" name="e_id" value="<?php print $e_id; ?>">
<?php print $m; ?> 
<p><input type="submit" name="sub" value="投票する"> 
<a href="result.php?e_id=<?php print $e_id; ?>">結果を見る</a></p>
</form>
</body>
</html>
 | 
  
ポイント question.phpから送られてきた選択された項目$countを使用し、UPDATE文を組み立てる
<?php
//question2.php投票処理
require_once($_SERVER['DOCUMENT_ROOT']."/kume/common/db_value.php");
if(!isset($_POST['sub']))exit("アクセスエラーです。");
if($_POST['count']=="")exit("選択してください。");
$count=$_POST['count'];//選択された項目
$e_id=$_POST["e_id"];
$con=mysql_connect($DBSERVER, $DBUSER, $DBPASSWORD);
if(!$con) exit("保守中です。しばらくしてからアクセス下さい。");
$sel=mysql_select_db($DBNAME, $con);
$sql="UPDATE enquete SET $count=$count+1 WHERE id=$e_id";
$rst=mysql_query($sql, $con);
$m="";
if($rst) $m="ありがとうございました。";
else $m="失敗しました。";
$cls=mysql_close($con);
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=Shift_JIS">
<title>アンケート</title>
</head>
<body>
<h1>アンケート</h1>
<p><?php print $m; ?></p>
<p><a href="result.php?e_id=<?php print $e_id; ?>">結果を見る</a></p>
</body>
</html>
 | 
  
| 拡張 | 方法 | 
| 一つのPCから複数投票回避方法 | クッキーを使う。 |