-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmyresults.php
More file actions
223 lines (206 loc) · 6.94 KB
/
myresults.php
File metadata and controls
223 lines (206 loc) · 6.94 KB
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
<?php
try {
$db = new SQLite3('poll_database.db');
// Retrieve token from URL
$token = isset($_GET['token']) ? $_GET['token'] : null;
if (!$token) {
throw new Exception("Token parameter is required.");
}
// First check if the token exists
$stmt = $db->prepare("SELECT COUNT(*) FROM polls WHERE token = :token");
$stmt->bindValue(':token', $token, SQLITE3_TEXT);
$token_exists = $stmt->execute()->fetchArray()[0];
if (!$token_exists) {
throw new Exception("Unknown token provided.");
}
// If we get here, the token exists, so proceed with the rest of the code
$yes_votes = $db->querySingle("SELECT COUNT(*) FROM polls WHERE vote = 'yes'");
$no_votes = $db->querySingle("SELECT COUNT(*) FROM polls WHERE vote = 'no'");
$total_votes = $yes_votes + $no_votes;
$pending_votes = $db->querySingle("SELECT COUNT(*) FROM polls WHERE vote IS NULL");
// Calculate percentages
$yes_percentage = $total_votes > 0 ? round(($yes_votes / $total_votes) * 100, 1) : 0;
$no_percentage = $total_votes > 0 ? round(($no_votes / $total_votes) * 100, 1) : 0;
// Get detailed results for the specific token
$stmt = $db->prepare("SELECT token, vote, voted_at FROM polls WHERE token = :token ORDER BY voted_at DESC");
$stmt->bindValue(':token', $token, SQLITE3_TEXT);
$results = $stmt->execute();
if (!$results) {
throw new Exception("Failed to execute query: " . $db->lastErrorMsg());
}
// Display results
?>
<!DOCTYPE html>
<html>
<head>
<title>Poll Results</title>
<style>
body {
font-family: Arial, sans-serif;
margin: 20px;
line-height: 1.6;
}
.summary {
background-color: #f5f5f5;
padding: 20px;
border-radius: 5px;
margin-bottom: 20px;
}
.progress-bar {
background-color: #e0e0e0;
height: 20px;
border-radius: 10px;
margin: 10px 0;
}
.progress {
background-color: #4CAF50;
height: 100%;
border-radius: 10px;
text-align: right;
padding-right: 5px;
color: white;
box-sizing: border-box;
}
.no-progress {
background-color: #f44336;
}
table {
width: 100%;
border-collapse: collapse;
margin-top: 20px;
}
th, td {
padding: 8px;
text-align: left;
border-bottom: 1px solid #ddd;
}
th {
background-color: #f5f5f5;
}
.error {
padding: 20px;
background-color: #f2dede;
border: 1px solid #ebccd1;
border-radius: 4px;
color: #a94442;
max-width: 500px;
margin: 20px auto;
text-align: center;
}
.footer {
margin-top: 40px;
padding: 20px;
border-top: 1px solid #ddd;
text-align: center;
color: #666;
font-size: 0.9em;
}
.footer a {
color: #337ab7;
text-decoration: none;
}
.footer a:hover {
text-decoration: underline;
}
</style>
</head>
<body>
<h1>Poll Results for Token: <?php echo htmlspecialchars($token); ?></h1>
<div class="summary">
<h2>Summary</h2>
<p>Total votes: <?php echo $total_votes; ?></p>
<p>Pending votes: <?php echo $pending_votes; ?></p>
<h3>Yes Votes: <?php echo $yes_votes; ?> (<?php echo $yes_percentage; ?>%)</h3>
<div class="progress-bar">
<div class="progress" style="width: <?php echo $yes_percentage; ?>%">
<?php echo $yes_percentage; ?>%
</div>
</div>
<h3>No Votes: <?php echo $no_votes; ?> (<?php echo $no_percentage; ?>%)</h3>
<div class="progress-bar">
<div class="progress no-progress" style="width: <?php echo $no_percentage; ?>%">
<?php echo $no_percentage; ?>%
</div>
</div>
</div>
<h2>My Vote</h2>
<table>
<tr>
<th>Token</th>
<th>Vote</th>
<th>Voted At</th>
</tr>
<?php while ($row = $results->fetchArray(SQLITE3_ASSOC)) { ?>
<tr>
<td><?php echo htmlspecialchars($row['token']); ?></td>
<td><?php echo htmlspecialchars($row['vote']); ?></td>
<td><?php echo htmlspecialchars($row['voted_at']); ?></td>
</tr>
<?php } ?>
</table>
<div class="footer">
<p>Powered by <strong>EazyPoll</strong> - Open source polling software</p>
<p>Source code available at: <a href="https://github.com/beamzer/EazyPoll" target="_blank">https://github.com/beamzer/EazyPoll</a></p>
</div>
</body>
</html>
<?php
// Close database connection
$db->close();
?>
<?php
}
catch (Exception $e) {
// Display only the error message in a styled div
?>
<!DOCTYPE html>
<html>
<head>
<title>Error</title>
<style>
body {
font-family: Arial, sans-serif;
margin: 20px;
line-height: 1.6;
}
.error {
padding: 20px;
background-color: #f2dede;
border: 1px solid #ebccd1;
border-radius: 4px;
color: #a94442;
max-width: 500px;
margin: 20px auto;
text-align: center;
}
.footer {
margin-top: 40px;
padding: 20px;
border-top: 1px solid #ddd;
text-align: center;
color: #666;
font-size: 0.9em;
}
.footer a {
color: #337ab7;
text-decoration: none;
}
.footer a:hover {
text-decoration: underline;
}
</style>
</head>
<body>
<div class="error">
<h2>Error</h2>
<p><?php echo htmlspecialchars($e->getMessage()); ?></p>
</div>
<div class="footer">
<p>Powered by <strong>EazyPoll</strong> - Open source polling software</p>
<p>Source code available at: <a href="https://github.com/beamzer/EazyPoll" target="_blank">https://github.com/beamzer/EazyPoll</a></p>
</div>
</body>
</html>
<?php
}
?>