34198-vm/results.php
2025-09-18 10:46:41 +00:00

92 lines
2.9 KiB
PHP

<?php
session_start();
$password = 'flatlogic1863';
$error = '';
$message = '';
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
if (isset($_POST['password']) && $_POST['password'] === $password) {
$_SESSION['loggedin'] = true;
} elseif (isset($_POST['cleanup']) && isset($_SESSION['loggedin']) && $_SESSION['loggedin']) {
require_once 'db/config.php';
$pdo = db();
$pdo->exec('TRUNCATE TABLE votes');
$message = 'All votes have been cleared.';
}
else {
if (!isset($_SESSION['loggedin']) || !$_SESSION['loggedin']) {
$error = 'Invalid password';
}
}
}
if (isset($_GET['logout'])) {
session_destroy();
header('Location: results.php');
exit;
}
if (isset($_SESSION['loggedin']) && $_SESSION['loggedin']) {
// Display results
echo '<h1>Voting Results</h1>';
if ($message) {
echo "<p style='color: green;'>$message</p>";
}
// Database connection
require_once 'db/config.php';
$pdo = db();
// Fetch results
$stmt = $pdo->query('SELECT category, website_number, COUNT(*) as votes FROM votes GROUP BY category, website_number ORDER BY category, votes DESC');
$results = $stmt->fetchAll(PDO::FETCH_ASSOC);
echo '<h2>Most Beautiful Website</h2>';
echo '<ul>';
$beauty_results = array_filter($results, function($row) { return $row['category'] === 'beauty'; });
if (empty($beauty_results)) {
echo '<li>No votes yet.</li>';
} else {
foreach ($beauty_results as $row) {
echo '<li>Website ' . htmlspecialchars($row['website_number']) . ': ' . htmlspecialchars($row['votes']) . ' votes</li>';
}
}
echo '</ul>';
echo '<h2>Most Funny/Interesting Website</h2>';
echo '<ul>';
$funny_results = array_filter($results, function($row) { return $row['category'] === 'funny'; });
if (empty($funny_results)) {
echo '<li>No votes yet.</li>';
} else {
foreach ($funny_results as $row) {
echo '<li>Website ' . htmlspecialchars($row['website_number']) . ': ' . htmlspecialchars($row['votes']) . ' votes</li>';
}
}
echo '</ul>';
echo '<br><form method="POST" onsubmit="return confirm(\'Are you sure you want to delete all votes?\');"><button type="submit" name="cleanup" value="true">Clear All Votes</button></form>';
echo '<br><a href="results.php?logout=true">Logout</a>';
} else {
// Display password form
?>
<!DOCTYPE html>
<html>
<head>
<title>View Results</title>
</head>
<body>
<h1>Enter Password to View Results</h1>
<form method="POST">
<input type="password" name="password" required>
<button type="submit">Login</button>
</form>
<?php if ($error): ?>
<p style="color: red;"><?php echo $error; ?></p>
<?php endif; ?>
</body>
</html>
<?php
}
?>