beautiful results

This commit is contained in:
Flatlogic Bot 2025-09-18 12:18:42 +00:00
parent 416c5781de
commit 7d516df61b
2 changed files with 97 additions and 68 deletions

View File

@ -37,13 +37,6 @@ document.addEventListener('DOMContentLoaded', function () {
event.preventDefault();
const formData = new FormData(form);
const data = Object.fromEntries(formData.entries());
const votes = (data.category === 'beauty') ? beautyVotes : funnyVotes;
// Add the individual vote numbers to the data object for the backend
data.website_number_1 = votes[0] || '';
data.website_number_2 = votes[1] || '';
data.website_number_3 = votes[2] || '';
delete data.website_numbers; // remove the comma-separated string
fetch('submit_vote.php', {
method: 'POST',

View File

@ -13,8 +13,7 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$pdo = db();
$pdo->exec('TRUNCATE TABLE votes');
$message = 'All votes have been cleared.';
}
else {
} else {
if (!isset($_SESSION['loggedin']) || !$_SESSION['loggedin']) {
$error = 'Invalid password';
}
@ -26,67 +25,104 @@ if (isset($_GET['logout'])) {
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>
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();
<?php if ($message): ?>
<div class="alert alert-success"><?php echo $message; ?></div>
<?php endif; ?>
// 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);
<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>
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>';
<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>
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 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; ?>
</body>
</html>
<?php
}
?>
</div>
</body>
</html>