설문조사

Download Report

Transcript 설문조사

4장장 설문조사
16
한빛미디어(주)
Section
01
학습 목표
설문조사 프로그램의 동작 기능을 이해한다
설문조사 DB 테이블의 구조를 이해한다
해당 항목 투표 시 이를 DB 테이블에 저장하는 법을 익힌다
투표수와 투표율을 화면에 표시하는 법을 익힌다
투표율을 막대그래프로 표시하는 방법을 익힌다
2
Section
주요
학습01내용
01. 설문조사 파일목록 및 DB 테이블
02. 설문조사 메인 프로그램
03. 투표하기
04. 투표 결과보기
3
Section 01
설문조사 미리 보기
[그림 16-1] 설문조사 메인 화면
4
Section 02
01
설문조사 파일 목록
파일명
설명
survey.sql
설문조사 DB 테이블 생성 명령
survey.php
설문조사 메인 화면
update.php
투표하기
result.php
결과보기
[표 16-1] 설문조사에 사용되는 파일 목록
5
Section 03
01
설문조사 프로그램 설치
⑴ 작업 폴더(“www" 폴더) 밑에 “survey" 폴더를 만들고 생성된
“survey" 폴더 밑에 교재 뒤 CD의 “www\survey" 폴더에 있는 파
일들과 "img" 디렉토리를 통째로 복사한다.
⑵ 설문조사 DB 테이블을 생성한다.
6
Section 04
01
설문조사 DB 테이블
필드 이름
타입
필드 설명
ans1
int
첫 번째 항목 투표수
ans2
int
두 번째 항목 투표수
ans3
int
세 번째 항목 투표수
ans4
int
네 번째 항목 투표수
[표 16-2] 설문조사 DB 테이블 (테이블 명 : survey)
7
Section 05
01
설문조사 DB 테이블 만들기
c:\mysql\bin” 폴더에 “survey.sql” 파일 작성 및 초기값 입력
create table survey(
ans1 int,
ans2 int,
ans3 int,
ans4 int
);
insert into survey values(0, 0, 0, 0);
명령 프롬프트에서 다음을 실행
C:\mysql\bin> mysql -uphp5 -p1234 php5_db < survey.sql
8
Section16-1】survey.php
01
【예제
02
설문조사 메인 프로그램
1 : <html>
2 : <head>
3 : <title> :: PHP 프로그래밍 입문에 오신 것을 환영합니다~~ :: </title>
4 : <link rel="stylesheet" href="../style.css" type="text/css">
5 : <script>
6:
function update()
7:
{ var vote;
9:
var length = document.survey_form.singer.length;
10 :
11 :
for (var i=0; i<length; i++)
12 :
{ if (document.survey_form.singer[i].checked == true)
14 :
{
15 :
vote= document.survey_form.singer[i].value;
16 :
break;
17 :
}
18 :
}
9
Section16-1】survey.php
01
【예제
02
20 :
21 :
23 :
24 :
25 :
26 :
27 :
28
29
30
31
32
33
:
:
:
:
:
:
if (i == length)
{ alert("항목을 선택하세요!");
return;
}
window.open("update.php?singer="+vote , "",
"left=200, top=200, width=180, height=250,
status=no, scrollbars=no");
}
function result()
{
window.open("result.php" , "",
"left=200, top=200, width=180, height=250, status=no,
scrollbars=no");
34 : }
35 : </script>
10
Section16-1】survey.php
01
【예제
02
37 : </head>
38 : <body leftmargin="0" topmargin="0" marginwidth="0“
marginheight="0">
39 : <form name=survey_form method=post >
40 : <table border=0 cellspacing=0 cellpdding=0 width='300'
align='center'>
41 :
<input type=hidden name=kkk value=100>
42 :
<tr> <td><img src="img/bbs_poll.gif"></td> </tr>
45 :
<tr height=1 bgcolor=#5AB2C8><td></td></tr>
46 :
<tr height=7><td></td></tr>
47 :
<tr><td><b> ♬ 제일 좋아하는 가수는?</b></td></tr>
48 :
<tr><td><input type=radio name='singer' value='ans1' >
1.동방신기
</td></tr>
49 :
<tr height=5><td></td></tr>
50 :
<tr><td><input type=radio name='singer' value='ans2' >
2.이효리 </td></tr>
11
Section16-1】survey.php
01
【예제
02
51 :
52 :
53 :
54 :
55
56
57
58
59
60
:
:
:
:
:
:
61
62
63
65
:
:
:
:
<tr height=5><td></td></tr>
<tr><td><input type=radio name='singer' value='ans3' >
3.비</td></tr>
<tr height=5><td></td></tr>
<tr><td><input type=radio name='singer' value='ans4' >
4.세븐</td></tr>
<tr height=7><td></td></tr>
<tr height=1 bgcolor=#5AB2C8><td></td></tr>
<tr>
<tr height=7><td></td></tr>
<tr>
<td align=middle><img src="img/b_vote.gif" border="0"
style='cursor:hand'
onclick=update()></a>
<img src="img/b_result.gif" border="0" style='cursor:hand'
onclick=result()></a></td></tr> </table>
</form>
12
Section16-2】update.php
01
【예제
02
투표하기
1 : <?
2 : include "../dbconn.php";
3:
4 : $sql = "update survey set $singer = $singer + 1";
5 : mysql_query($sql, $connect);
6:
7 : mysql_close();
8:
9 : Header("location:result.php");
10 : ?>
13
Section16-3】result.php
01
【예제
02
투표결과
1 : <?
2 : include "../dbconn.php";
4 : $sql ="select * from survey";
5 : $result = mysql_query($sql, $connect);
7 : $row = mysql_fetch_array($result);
9 : $total = $row[ans1] + $row[ans2] + $row[ans3] + $row[ans4];
10 :
11 : $ans1_percent = $row[ans1]/$total * 100;
12 : $ans2_percent = $row[ans2]/$total * 100;
13 : $ans3_percent = $row[ans3]/$total * 100;
14 : $ans4_percent = $row[ans4]/$total * 100;
15 :
16 : $ans1_percent = floor($ans1_percent);
17 : $ans2_percent = floor($ans2_percent);
18 : $ans3_percent = floor($ans3_percent);
19 : $ans4_percent = floor($ans4_percent);
20 : ?>
14
Section16-3】result.php
01
【예제
02
21 : <html>
22 : <head>
23 : <title> :: PHP 프로그래밍 입문에 오신 것을 환영합니다~~ :: </title>
24 : <link rel="stylesheet" href="../style.css" type="text/css">
25 : </head>
26 : <body bgcolor=#ffffff leftmargin=0 topmargin=0
marginwidth=0 marginheight=0>
28 : <table width=180 height=250 border='0' cellspacing='0‘
cellpadding='0'>
29 :
<tr> <td width=180 height=1 colspan=5 bgcolor=#ffffff></td>
31 :
</tr>
32 :
<tr> <td width=1 height=35 bgcolor='#ffffff'></td>
34 :
<td width=9 bgcolor='#ffffff'></td>
35 :
<td width=150 align=center bgcolor='#ffffff'><b>
36 :
총 투표수 : <? echo $total ?> &nbsp;명 </b></td>
37 :
<td width=9 bgcolor='#ffffff'></td>
38 :
<td width=1 bgcolor='#ffffff'></td> /tr>
15
Section16-3】result.php
01
【예제
02
40 :
42 :
43 :
44 :
45 :
47 :
49 :
50 :
51 :
53 :
55 :
57 :
59 :
<tr> <td height=29 bgcolor='#ffffff'></td>
<td></td>
<td valign=middle><b>♬ 제일 좋아하는 가수는?</b></td>
<td></td>
<td bgcolor='#ffffff'></td> </tr>
<tr> <td height=20 bgcolor='#ffffff'></td>
<td></td>
<td> 동방신기 (<b><? echo $ans1_percent ?></b> %)
<font color=purple><b><? echo $row[ans1] ?></b>
</font> 명 </td> <td></td>
<td bgcolor='#ffffff'></td> </tr>
<tr> <td height=3 bgcolor='#ffffff'></td>
<td></td>
<td>
<table width=100 border=0 height=3 cellspacing=0
cellpadding=0>
16
Section16-3】result.php
01
【예제
02
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
:
<tr>
: <?
: $rest = 100 - $ans1_percent;
:
: echo "
:
<td width='$ans1_percent%' bgcolor=purple></td>
:
<td width='$rest%' bgcolor='#dddddd' height=5></td>
:
";
: ?>
:
</tr>
: </table>
:
</td>
:
<td></td>
:
<td bgcolor='#ffffff'></td>
:
</tr>
:
<tr>
17
Section16-3】result.php
01
【예제
02
76 :
77 :
78 :
79 :
81
83
85
86
87
88
89
90
92
94
95
96
:
:
:
:
:
:
:
:
:
:
:
:
<td height=20 bgcolor='#ffffff'></td>
<td></td>
<td> 이효리 (<b><? echo $ans2_percent ?></b> %)
<font color=blue><b><? echo $row[ans2] ?></b></font> 명
</td> <td></td>
<td bgcolor='#ffffff'></td></tr>
<tr> <td height=3 bgcolor='#ffffff'></td>
<td></td>
<td>
<table width=100 border=0 height=3 cellspacing=0 cellpadding=0>
<tr>
<?
$rest = 100 - $ans2_percent;
echo " <td width='$ans2_percent%' bgcolor=blue></td>
<td width='$rest%' bgcolor='#dddddd' height=5></td>
";
?>
18
Section16-3】result.php
01
【예제
02
97 :
</tr>
98 : </table>
99 :
</td>
100 :
<td></td> <td bgcolor='#ffffff'></td>
102 :
</tr>
103 :
<tr> <td height=20 bgcolor='#ffffff'></td> <td></td>
106 :
<td> 비 (<b><? echo $ans3_percent ?></b> %)
107 :
<font color=green><b><? echo $row[ans3] ?></b></font> 명
</td>
108 :
<td></td> <td bgcolor='#ffffff'></td>
110 :
</tr>
111 :
<tr>
112 :
<td height=3 bgcolor='#ffffff'></td>
113 :
<td></td>
114 :
<td>
115 :
<table width=100 border=0 height=3 cellspacing=0
cellpadding=0> <tr>
19
Section16-3】result.php
01
【예제
02
117
118
120
122
123
124
125
126
127
129
131
132
135
136
: <?
: $rest = 100 - $ans3_percent;
:
echo " <td width='$ans3_percent%' bgcolor=green></td>
:
<td width='$rest%' bgcolor='#dddddd' height=5></td>
:
";
: ?>
:
</tr>
: </table>
:
</td> <td></td>
:
<td bgcolor='#ffffff'></td> </tr>
:
:
<tr> <td height=20 bgcolor='#ffffff'></td> <td></td>
:
<td> 세븐 (<b><? echo $ans4_percent ?></b> %)
:
<font color=skyblue><b><? echo $row[ans4] ?></b></font>
명</td> <td></td>
138 :
<td bgcolor='#ffffff'></td> </tr>
20
Section16-3】result.php
01
【예제
02
140 :
142 :
143 :
144 :
<tr> <td height=3 bgcolor='#ffffff'></td>
<td></td>
<td>
<table width=100 border=0 height=3 cellspacing=0
cellpadding=0> <tr>
146 : <?
147 :
$rest = 100 - $ans4_percent;
148 :
149 :
echo "
150 :
<td width='$ans4_percent%' bgcolor=skyblue></td>
151 :
<td width='$rest%' bgcolor='#dddddd' height=5></td>
152 :
";
153 : ?>
154 :
</tr> </table>
156 :
</td> <td></td>
158 :
<td bgcolor='#ffffff'></td> </tr>
21
Section16-3】result.php
01
【예제
02
160 :
161 :
162 :
163 :
164 :
165
166
167
168
169
170
171
172
173
174
:
:
:
:
:
:
:
:
:
:
<tr>
<td height=40 bgcolor='#ffffff'></td>
<td></td>
<td align=center valign=middle>
<input type='image' style='cursor:hand' src='img/close.gif‘
border=0
onfocus=this.blur() onclick="window.close()"></td>
<td></td>
<td bgcolor='#ffffff'></td>
</tr>
<tr>
<td height=1 colspan=5 bgcolor=#ffffff></td>
</tr>
</table>
</body>
</html>
22