-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpoll.php
94 lines (86 loc) · 2.68 KB
/
poll.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
<?php
include 'connect.php';
if (!isset($_GET['p'])) {
header("Location:index.php");
}
else {
$id = $_GET['p'];
$upit = "SELECT id, question
FROM polls
WHERE id = $id";
$rez = mysql_query($upit,$k);
$r = mysql_fetch_object($rez);
$upitOdgovori = "SELECT polls_choice.id AS choice_id, polls_choice.name as choice_name
FROM polls_answer JOIN polls_choice
ON polls_answer.choice_id = polls_choice.id
WHERE polls_answer.poll_id = $id";
$rezOdgovori = mysql_query($upitOdgovori,$k);
$upitOdgovori = "SELECT polls_choice.name,
COUNT(polls_answer.id) * 100 / (
SELECT COUNT(*)
FROM polls_answer
WHERE polls_answer.poll_id = $id
) AS percentage
FROM polls_choice LEFT JOIN polls_answer
ON polls_choice.id = polls_answer.choice_id
WHERE polls_choice.poll_id = $id
GROUP BY polls_choice.id";
$rezOdgovori = mysql_query($upitOdgovori, $k);
while ($row = mysql_fetch_object($rezOdgovori)) {
$answers[] = $row;
}
$upitPonude = "SELECT polls.id, polls_choice.id AS c_id, polls_choice.name
FROM polls JOIN polls_choice
ON polls.id = polls_choice.poll_id
WHERE polls.id = $id";
$rezPonude = mysql_query($upitPonude,$k);
while ($row = mysql_fetch_object($rezPonude)) {
$choices[] = $row;
}
}
?>
<html>
<head>
<link rel="stylesheet" href="style.css" />
</head>
<body>
<?php if(!p): ?>
<p>That poll doesen't exist</p>
<?php else: ?>
<div class="poll">
<div class="poll-question">
<?php echo $r->question; ?>
</div>
<?php if(!empty($choices)): ?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<?php foreach($choices as $index => $c): ?>
<div class="poll-option">
<input type="radio" name="c" id="c<?php echo $index; ?>" value="<?php echo $c->c_id; ?>">
<label for="c<?php echo $index; ?>"><?php echo $c->name; ?> </label>
</div>
<?php endforeach; ?>
<input type="submit" value="Vote" name="vote" class="blogBtn">
<input type="hidden" name="p" value="<?php echo $id ?>">
</form>
<div class="current">Current results:</div>
<ul class="current__results">
<?php foreach ($answers as $answer): ?>
<li><?php echo $answer->name; ?> (<?php echo number_format($answer->percentage,2); ?>%)</li>
<?php endforeach; ?>
</ul>
<?php else: ?>
<p>No choices avaliable</p>
<?php endif; ?>
<?php endif; ?>
<?php
if (isset($_POST['p'], $_POST['c'])) {
$poll = $_POST['p'];
$choice = $_POST['c'];
$upitGlasanje = "INSERT INTO polls_answer (poll_id, choice_id)
VALUES($poll, $choice)";
$rezGlasanje = mysql_query($upitGlasanje,$k);
}
?>
</div>
</body>
</html>