WebプログラミングU PHP

 目次へ 前へ 次へ

2014/7/15 久米

 

6 アンケート作成

6.1 表の作成

CREATE TABLE answers (
id int PRIMARY KEY AUTO_INCREMENT,
question_id int,
answer_id int,
answer varchar(255),
vote int
);

6.2 phpスクリプト

  1. question_idは自分で分かっているので、設定する。例えば$question_id=15など
  2. もし投票の時は、voteに1を足すupdate文発行し、リダイレクト
  3. answers表の読み取り。select文
  4. 結果を配列に代入。それとともに全投票数計算
  5. 配列から、formタグ文と得票数を表示するhtmlを生成
  6. 5で作ったhtmlを表示

棒グラブで表示する場合は、

  1. アンケートスクリプト.phpと同じフォルダに、barのようなbar.gifを配置し、
  2. imgタグでbar.gifのwidthを変化させる 。

 


昔のアンケートスクリプト

6.1 データベースの作成

アンケート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

最も基本的な情報だけのデータベースを作ります。

  1. phpMyAdminにアクセス
  2. 自分のデータベースを選択
  3. 現在のDBに新しいテーブルを作成します。を実行。
     名前:enquete 、フィールド(列数のこと):12
  4. あるいは、SQLタブから下記SQLを実行
    CREATE TABLE `enquete` (
    `id` int(11) NOT NULL auto_increment,
    `question` text NOT NULL,
    `s0` varchar(128) NOT NULL,
    `s1` varchar(128) NOT NULL,
    `s2` varchar(128) NOT NULL,
    `s3` varchar(128) NOT NULL,
    `s4` varchar(128) NOT NULL,
    `c0` int(11) NOT NULL default '0',
    `c1` int(11) NOT NULL default '0',
    `c2` int(11) NOT NULL default '0',
    `c3` int(11) NOT NULL default '0',
    `c4` int(11) NOT NULL default '0',
    PRIMARY KEY (`id`)
    );
  5. アンケートをひとつ入力しておく

 

6.2 アンケート結果表示 result.php

  1. ポイント1 選択肢とその投票数カウンタを$sと$cにして、、for文処理を可能にする
  2. ポイント2 棒グラフはimgあるいはtableのwidthを変化させる

アンケートスクリプト.phpと同じフォルダに、barのような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=mysqli_connect($DBSERVER, $DBUSER, $DBPASSWORD, $DBNAME);
if(!$con) exit("保守中です。しばらくしてからアクセス下さい。");

$sql=/*________________*/;
$rst=mysqli_query($con, $sql);
$row=mysqli_fetch_array($rst);
mysqli_free_result($rst);
$cls=mysqli_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=utf-8">
<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>

 

6.3 アンケート投票form作成 question.php

  1. ポイント1 選択肢を配列$sにして、for文処理を可能にする
  2. ポイント2 formタグのvalue属性の値をfor文で変化させる

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=mysqli_connect($DBSERVER, $DBUSER, $DBPASSWORD, $DBNAME);
if(!$con) exit("保守中です。しばらくしてからアクセス下さい。");

$sql="SELECT * FROM enquete WHERE id=$e_id";
$rst=mysqli_query($con, $sql);
$row=mysqli_fetch_array($rst);
mysqli_free_result($rst);
$cls=mysqli_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=utf-8">
<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>

 

6.3 アンケート投票処理 question2.php

ポイント 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=mysqli_connect($DBSERVER, $DBUSER, $DBPASSWORD, $DBNAME);
if(!$con) exit("保守中です。しばらくしてからアクセス下さい。");

$sql="UPDATE enquete SET $count=$count+1 WHERE id=$e_id";
$rst=mysqli_query($con, $sql);

$m="";
if($rst) $m="ありがとうございました。";
else $m="失敗しました。";

$cls=mysqli_close($con);
?>

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<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>

 

6.4 拡張

拡張 方法
一つのPCから複数投票回避方法 クッキーを使う。