2025-12-13 19:00:10 +07:00

114 lines
3.1 KiB
PHP

<?php
session_start();
require_once "koneksi.php"; // Menggunakan koneksi MySQLi
if (isset($_SESSION['username'])) {
$nama = $_SESSION['username'];
} else {
$nama = "";
}
$score = 0;
// 1. Ambil Skor User yang Login (Untuk Tampilan Individual, jika diperlukan)
if (!empty($nama)) {
// ⚠️ Perlu DITINGKATKAN ke prepared statement untuk keamanan
$getScore = "SELECT score FROM users WHERE username = '$nama'";
$resultMe = mysqli_query($koneksi, $getScore);
if ($resultMe && mysqli_num_rows($resultMe) > 0) {
$row = mysqli_fetch_assoc($resultMe);
$score = $row['score'];
}
}
// 2. Ambil 10 Skor Tertinggi
$sql = "SELECT username, score FROM users ORDER BY score DESC LIMIT 10";
$result = mysqli_query($koneksi, $sql);
$leaderboard = [];
if($result) {
// Ambil semua hasil
$leaderboard = mysqli_fetch_all($result, MYSQLI_ASSOC);
}
?>
<!DOCTYPE html>
<html lang="id">
<style>
body {
background: linear-gradient(to bottom right, #A3D438, #004D40);
height: 100vh; /* Menggunakan vh agar mencakup seluruh tinggi layar */
display: flex;
flex-direction: column;
align-items: center;
padding-top: 50px;
color: white;
font-family: sans-serif;
}
table {
border-collapse: collapse;
width: 80%;
max-width: 600px;
margin-top: 20px;
background-color: rgba(0, 0, 0, 0.4);
}
th, td {
border: 1px solid #ddd;
padding: 12px;
text-align: left;
}
th {
background-color: #004D40;
color: white;
text-transform: uppercase;
}
tr:nth-child(even) {
background-color: rgba(255, 255, 255, 0.1);
}
</style>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Leaderboard</title>
</head>
<body>
<h1>🥇 Leaderboard Top 10 🥈</h1>
<?php if (!empty($nama)): ?>
<p style="font-size: 1.2em;">Skor Tertinggi Anda (<?php echo htmlspecialchars($nama); ?>): <strong><?php echo $score; ?> PTS</strong></p>
<?php else: ?>
<p>Silakan <a href="login.php" style="color: yellow;">login</a> untuk melihat skor Anda.</p>
<?php endif; ?>
<table>
<thead>
<tr>
<th>#</th>
<th>Username</th>
<th>Score</th>
</tr>
</thead>
<tbody>
<?php
$peringkat = 1;
if (!empty($leaderboard)) {
foreach ($leaderboard as $pemain):
?>
<tr>
<td><?php echo $peringkat; ?></td>
<td><?php echo htmlspecialchars($pemain['username']); ?></td>
<td><?php echo $pemain['score']; ?> PTS</td>
</tr>
<?php
$peringkat++;
endforeach;
} else {
echo '<tr><td colspan="3" style="text-align: center;">Belum ada Pemain</td></tr>';
}
?>
</tbody>
</table>
</body>
</html>