34198-vm/results.php
2025-09-18 12:18:42 +00:00

129 lines
5.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;
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Voting Results</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
<link rel="stylesheet" href="assets/css/custom.css?v=<?php echo time(); ?>">
</head>
<body>
<div class="container mt-5">
<?php if (isset($_SESSION['loggedin']) && $_SESSION['loggedin']): ?>
<div class="d-flex justify-content-between align-items-center mb-4">
<h1 class="mb-0">Voting Results</h1>
<a href="results.php?logout=true" class="btn btn-secondary">Logout</a>
</div>
<?php if ($message): ?>
<div class="alert alert-success"><?php echo $message; ?></div>
<?php endif; ?>
<div class="row">
<div class="col-md-6">
<div class="card">
<div class="card-body">
<h2 class="card-title">Most Beautiful Website</h2>
<?php
require_once 'db/config.php';
$pdo = db();
$stmt = $pdo->query("SELECT website_number, COUNT(*) as votes FROM votes WHERE category = 'beauty' GROUP BY website_number ORDER BY votes DESC");
$results = $stmt->fetchAll(PDO::FETCH_ASSOC);
if (empty($results)): ?>
<p>No votes yet.</p>
<?php else: ?>
<ul class="list-group list-group-flush">
<?php foreach ($results as $row): ?>
<li class="list-group-item d-flex justify-content-between align-items-center">
Website <?php echo htmlspecialchars($row['website_number']); ?>
<span class="badge bg-primary rounded-pill"><?php echo htmlspecialchars($row['votes']); ?> votes</span>
</li>
<?php endforeach; ?>
</ul>
<?php endif; ?>
</div>
</div>
</div>
<div class="col-md-6">
<div class="card">
<div class="card-body">
<h2 class="card-title">Most Funny/Interesting Website</h2>
<?php
$stmt = $pdo->query("SELECT website_number, COUNT(*) as votes FROM votes WHERE category = 'funny' GROUP BY website_number ORDER BY votes DESC");
$results = $stmt->fetchAll(PDO::FETCH_ASSOC);
if (empty($results)): ?>
<p>No votes yet.</p>
<?php else: ?>
<ul class="list-group list-group-flush">
<?php foreach ($results as $row): ?>
<li class="list-group-item d-flex justify-content-between align-items-center">
Website <?php echo htmlspecialchars($row['website_number']); ?>
<span class="badge bg-primary rounded-pill"><?php echo htmlspecialchars($row['votes']); ?> votes</span>
</li>
<?php endforeach; ?>
</ul>
<?php endif; ?>
</div>
</div>
</div>
</div>
<div class="mt-4">
<form method="POST" onsubmit="return confirm('Are you sure you want to delete all votes?');">
<button type="submit" name="cleanup" value="true" class="btn btn-danger">Clear All Votes</button>
</form>
</div>
<?php else: ?>
<div class="row justify-content-center">
<div class="col-md-6">
<div class="card">
<div class="card-body">
<h1 class="card-title text-center">View Results</h1>
<form method="POST">
<div class="mb-3">
<label for="password" class="form-label">Password</label>
<input type="password" name="password" id="password" class="form-control" required>
</div>
<button type="submit" class="btn btn-primary w-100">Login</button>
<?php if ($error): ?>
<p class="text-danger mt-2"><?php echo $error; ?></p>
<?php endif; ?>
</form>
</div>
</div>
</div>
</div>
<?php endif; ?>
</div>
</body>
</html>