-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathresults.php
More file actions
137 lines (125 loc) · 4.25 KB
/
results.php
File metadata and controls
137 lines (125 loc) · 4.25 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
<?php
// Basic authentication (you should implement proper authentication)
$username = "santa";
$password = "secret";
if (!isset($_SERVER['PHP_AUTH_USER']) ||
!isset($_SERVER['PHP_AUTH_PW']) ||
$_SERVER['PHP_AUTH_USER'] != $username ||
$_SERVER['PHP_AUTH_PW'] != $password) {
header('WWW-Authenticate: Basic realm="Poll Results"');
header('HTTP/1.0 401 Unauthorized');
echo 'Authentication required';
exit;
}
try {
$db = new SQLite3('poll_database.db');
// Get vote counts
$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 - only for registered votes
$results = $db->query("SELECT token, vote, voted_at FROM polls WHERE vote IS NOT NULL ORDER BY voted_at DESC");
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;
}
</style>
</head>
<body>
<h1>Poll Results</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>Detailed Results (<?php echo $total_votes; ?> registered votes)</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>
</body>
</html>
<?php
// Close database connection
$db->close();
?>
<?php
}
catch (Exception $e) {
echo 'Error: ' . $e->getMessage();
}
?>