feat: make a leaderboard feature
This commit is contained in:
parent
f9fc7ba011
commit
6311cab5a2
@ -1,17 +1,29 @@
|
|||||||
<?php
|
<?php
|
||||||
// session_start();
|
session_start();
|
||||||
// require_once "config/db.php";
|
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_me && mysqli_num_rows($result_me)>0){
|
||||||
// if($result&&$result -> num_rows >0){
|
$row = mysqli_fetch_assoc($result_me);
|
||||||
// while($row = $result->fetch_assoc()){
|
$score = $row['score'];
|
||||||
// $leaderboard[]=$row;
|
}
|
||||||
// }
|
|
||||||
// }
|
|
||||||
|
$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="container-board">
|
||||||
<div class="chara">
|
<div class="chara">
|
||||||
<div class="menu">
|
<div class="menu">
|
||||||
<h1 style="text-align: center;">Matthew as Knight</h1>
|
<h1 style="text-align: center;"><?php echo $nama; ?></h1>
|
||||||
</div>
|
</div>
|
||||||
<h1 style="text-align: center;">150pts</h1>
|
<h1 style="text-align: center;"> <?php echo $score; ?> </h1>
|
||||||
<div class="character">
|
<div class="character">
|
||||||
<img src="assets/Design/KnightPixLE.png" alt="knight">
|
<img src="assets/Design/KnightPixLE.png" alt="knight">
|
||||||
</div>
|
</div>
|
||||||
@ -46,25 +58,26 @@
|
|||||||
</tr>
|
</tr>
|
||||||
</thead>
|
</thead>
|
||||||
<tbody>
|
<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
|
<?php
|
||||||
$peringkat++;
|
$peringkat = 1;
|
||||||
endforeach;
|
if (!empty($leaderboard)) {
|
||||||
|
foreach ($leaderboard as $pemain) :
|
||||||
|
?>
|
||||||
|
<tr>
|
||||||
|
<td><?php echo $peringkat; ?></td>
|
||||||
|
<td><?php echo htmlspecialchars($pemain['username']); ?></td>
|
||||||
|
|
||||||
if (empty($leaderboard)) {
|
<td><?php echo $pemain['score']; ?> PTS</td>
|
||||||
echo '<tr><td colspan="3" style="text-align: center;">Belum ada pemain</td></tr>';
|
</tr>
|
||||||
}
|
|
||||||
?> -->
|
<?php
|
||||||
|
$peringkat++;
|
||||||
|
endforeach;
|
||||||
|
} else {
|
||||||
|
// Jika data kosong
|
||||||
|
echo '<tr><td colspan="3" style="text-align: center;">Belum ada pemain</td></tr>';
|
||||||
|
}
|
||||||
|
?>
|
||||||
</tbody>
|
</tbody>
|
||||||
</table>
|
</table>
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
@ -32,7 +32,7 @@ if(isset($_POST['register'])){
|
|||||||
|
|
||||||
$new_user_id =$db-> insert_id;
|
$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 = $db->prepare($sql_char);
|
||||||
$stmt_char->bind_param("is", $new_user_id, $username);
|
$stmt_char->bind_param("is", $new_user_id, $username);
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user