kelompok12-codebeater/src/leaderboard.php
Carolus Bramnatyo Seno Mahesworo 6311cab5a2 feat: make a leaderboard feature
2025-11-24 14:41:42 +07:00

87 lines
2.5 KiB
PHP

<?php
session_start();
require_once "config/db.php";
$sql= "SELECT username, score
FROM users
ORDER BY score DESC
LIMIT 10";
$nama = $_SESSION['username'];
$score = 0;
$getscore = "SELECT score FROM users WHERE username ='$nama'";
$result_me = mysqli_query($db,$getscore);
if ($result_me && mysqli_num_rows($result_me)>0){
$row = mysqli_fetch_assoc($result_me);
$score = $row['score'];
}
$result = mysqli_query($db, $sql);
$leaderboard = [];
if($result){
$leaderboard = mysqli_fetch_all($result, MYSQLI_ASSOC);
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>leaderboard</title>
<link rel="stylesheet" href="css/global.css">
<link rel="stylesheet" href="css/style.css">
</head>
<body>
<div class="container-board">
<div class="chara">
<div class="menu">
<h1 style="text-align: center;"><?php echo $nama; ?></h1>
</div>
<h1 style="text-align: center;"> <?php echo $score; ?> </h1>
<div class="character">
<img src="assets/Design/KnightPixLE.png" alt="knight">
</div>
</div>
<div class="board">
<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 {
// Jika data kosong
echo '<tr><td colspan="3" style="text-align: center;">Belum ada pemain</td></tr>';
}
?>
</tbody>
</table>
</div>
</div>
</body>
</html>