179 lines
8.3 KiB
PHP
179 lines
8.3 KiB
PHP
<?php
|
|
session_start();
|
|
|
|
// Hardcoded password for demonstration
|
|
$password = 'flatlogic1863'; // Replace with a secure password management
|
|
$error = '';
|
|
$message = '';
|
|
|
|
// Handle login
|
|
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['password'])) {
|
|
if ($_POST['password'] === $password) {
|
|
$_SESSION['loggedin'] = true;
|
|
header('Location: results.php'); // Redirect to avoid form resubmission
|
|
exit;
|
|
} else {
|
|
$error = 'Invalid password';
|
|
}
|
|
}
|
|
|
|
// Handle cleanup
|
|
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['cleanup'])) {
|
|
if (isset($_SESSION['loggedin']) && $_SESSION['loggedin']) {
|
|
require_once 'db/config.php';
|
|
$pdo = db();
|
|
$pdo->exec('TRUNCATE TABLE votes');
|
|
$message = 'All votes have been cleared.';
|
|
} else {
|
|
// Optional: handle unauthorized cleanup attempt
|
|
}
|
|
}
|
|
|
|
// Handle logout
|
|
if (isset($_GET['logout'])) {
|
|
session_destroy();
|
|
header('Location: results.php');
|
|
exit;
|
|
}
|
|
|
|
$is_loggedin = isset($_SESSION['loggedin']) && $_SESSION['loggedin'];
|
|
?>
|
|
<!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 ($is_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)):
|
|
echo "<p>No votes yet for this category.</p>";
|
|
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)):
|
|
echo "<p>No votes yet for this category.</p>";
|
|
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="card mt-4">
|
|
<div class="card-body">
|
|
<h2 class="card-title">Raw Votes</h2>
|
|
<?php
|
|
$stmt = $pdo->query("SELECT id, voter_name, category, website_number, created_at FROM votes ORDER BY created_at DESC");
|
|
$raw_votes = $stmt->fetchAll(PDO::FETCH_ASSOC);
|
|
if (empty($raw_votes)):
|
|
echo "<p>No votes have been cast yet.</p>";
|
|
else:
|
|
?>
|
|
<table class="table table-striped">
|
|
<thead>
|
|
<tr>
|
|
<th>ID</th>
|
|
<th>Voter Name</th>
|
|
<th>Category</th>
|
|
<th>Website Number</th>
|
|
<th>Timestamp</th>
|
|
</tr>
|
|
</thead>
|
|
<tbody>
|
|
<?php foreach ($raw_votes as $vote): ?>
|
|
<tr>
|
|
<td><?php echo htmlspecialchars($vote['id']); ?></td>
|
|
<td><?php echo htmlspecialchars($vote['voter_name']); ?></td>
|
|
<td><?php echo htmlspecialchars($vote['category']); ?></td>
|
|
<td><?php echo htmlspecialchars($vote['website_number']); ?></td>
|
|
<td><?php echo htmlspecialchars($vote['created_at']); ?></td>
|
|
</tr>
|
|
<?php endforeach; ?>
|
|
</tbody>
|
|
</table>
|
|
<?php endif; ?>
|
|
</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>
|
|
<p class="text-center text-muted">You must be logged in to see the voting results.</p>
|
|
<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 text-center"><?php echo $error; ?></p>
|
|
<?php endif; ?>
|
|
</form>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
<?php endif; ?>
|
|
</div>
|
|
</body>
|
|
</html>
|