feat: make a leaderboard feature

This commit is contained in:
Carolus Bramnatyo Seno Mahesworo 2025-11-24 14:41:42 +07:00
parent f9fc7ba011
commit 6311cab5a2
2 changed files with 43 additions and 30 deletions

View File

@ -1,17 +1,29 @@
<?php
// session_start();
// require_once "config/db.php";
session_start();
require_once "config/db.php";
// $sql_leaderbord = "SELECT u.username, c.poin FROM characters c JOIN users u ON u.user_id = c.user_id ORDER BY c.poin DESC LIMIT 10 ";
$sql= "SELECT username, score
FROM users
ORDER BY score DESC
LIMIT 10";
$nama = $_SESSION['username'];
$score = 0;
// $result = $db ->query($sql_leaderbord);
$getscore = "SELECT score FROM users WHERE username ='$nama'";
$result_me = mysqli_query($db,$getscore);
// $leaderboard=[];
// if($result&&$result -> num_rows >0){
// while($row = $result->fetch_assoc()){
// $leaderboard[]=$row;
// }
// }
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);
}
?>
@ -29,9 +41,9 @@
<div class="container-board">
<div class="chara">
<div class="menu">
<h1 style="text-align: center;">Matthew as Knight</h1>
<h1 style="text-align: center;"><?php echo $nama; ?></h1>
</div>
<h1 style="text-align: center;">150pts</h1>
<h1 style="text-align: center;"> <?php echo $score; ?> </h1>
<div class="character">
<img src="assets/Design/KnightPixLE.png" alt="knight">
</div>
@ -46,25 +58,26 @@
</tr>
</thead>
<tbody>
<!-- <?php
$peringkat = 1;
foreach ($leaderboard as $pemain) :
?>
<tr>
<td><?php echo $peringkat; ?></td>
<td><?php echo htmlspecialchars($pemain['username']); ?></td>
<td><?php echo $pemain['poin']; ?> PTS</td>
</tr>
<?php
$peringkat++;
$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;
if (empty($leaderboard)) {
echo '<tr><td colspan="3" style="text-align: center;">Belum ada pemain</td></tr>';
}
?> -->
} else {
// Jika data kosong
echo '<tr><td colspan="3" style="text-align: center;">Belum ada pemain</td></tr>';
}
?>
</tbody>
</table>
</div>

View File

@ -32,7 +32,7 @@ if(isset($_POST['register'])){
$new_user_id =$db-> insert_id;
$sql_char = "INSERT INTO users (user_id, name, poin) VALUES (?, ?, 0)";
$sql_char = "INSERT INTO users (user_id, name, score) VALUES (?, ?, 0)";
$stmt_char = $db->prepare($sql_char);
$stmt_char->bind_param("is", $new_user_id, $username);