leaderboard
This commit is contained in:
parent
b6fbbc31c7
commit
c2a00e3238
10
home.html
10
home.html
@ -16,7 +16,6 @@
|
||||
width: 70%;
|
||||
justify-self: center;
|
||||
background-image: url();
|
||||
|
||||
}
|
||||
|
||||
#button {
|
||||
@ -35,13 +34,14 @@
|
||||
<body>
|
||||
<h1>EAT THAT APPLE</h1>
|
||||
<div class="container">
|
||||
<a href="game.html">
|
||||
<div id="button">Play Game</div>
|
||||
</a>
|
||||
|
||||
<div id="button">Leaderboard</div>
|
||||
<div id="button">Quit</div>
|
||||
<div id="button">Logout</div>
|
||||
</div>
|
||||
</body>
|
||||
|
||||
<script>
|
||||
|
||||
</script>
|
||||
<script></script>
|
||||
</html>
|
||||
84
leaderboard.php
Normal file
84
leaderboard.php
Normal file
@ -0,0 +1,84 @@
|
||||
<?php
|
||||
session_start();
|
||||
require_once "koneksi.php";
|
||||
|
||||
// --- START Perbaikan untuk mengatasi error 'Undefined array key' ---
|
||||
// Cek apakah session 'username' sudah ada?
|
||||
if (isset($_SESSION['username'])) {
|
||||
$nama = $_SESSION['username'];
|
||||
} else {
|
||||
// Jika belum login, anggap sebagai Guest/kosong
|
||||
$nama = "";
|
||||
}
|
||||
// --- END Perbaikan ---
|
||||
|
||||
$score = 0;
|
||||
|
||||
// Hanya jalankan query user JIKA $nama tidak kosong
|
||||
if (!empty($nama)) {
|
||||
$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'];
|
||||
}
|
||||
}
|
||||
|
||||
// Ambil data leaderboard (Top 10)
|
||||
$sql = "SELECT username, score FROM users ORDER BY score DESC LIMIT 10";
|
||||
$result = mysqli_query($koneksi, $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="">
|
||||
<link rel="stylesheet" href="">
|
||||
</head>
|
||||
<body>
|
||||
<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>
|
||||
32
login.php
32
login.php
@ -1,6 +1,22 @@
|
||||
=
|
||||
<?php
|
||||
session_start();
|
||||
include "koneksi.php";
|
||||
if(isset($_POST['username'])) {
|
||||
$username = $_POST['username'];
|
||||
$password = md5($_POST['password']);
|
||||
|
||||
$query = mysqli_query($koneksi, "SELECT * FROM users where username='$username' and password='$password'");
|
||||
|
||||
if(mysqli_num_rows($query) > 0) {
|
||||
$data = mysqli_fetch_array($query);
|
||||
$_SESSION['users'] = $data;
|
||||
$_SESSION['username'] = $data = ['username'];
|
||||
echo '<script>alert("Selamat datang, '.$data['nama'].'"); location.href="index.php";</script>';
|
||||
} else {
|
||||
echo '<script>alert("Username atau Password tidak sesuai");</script>';
|
||||
}
|
||||
}
|
||||
?>
|
||||
|
||||
<!DOCTYPE html>
|
||||
@ -11,22 +27,6 @@ include "koneksi.php";
|
||||
<title>Halaman: Login</title>
|
||||
</head>
|
||||
<body>
|
||||
<?php
|
||||
if(isset($_POST['username'])) {
|
||||
$username = $_POST['username'];
|
||||
$password = md5($_POST['password']);
|
||||
|
||||
$query = mysqli_query($koneksi, "SELECT * FROM users where username='$username' and password='$password'");
|
||||
|
||||
if(mysqli_num_rows($query) > 0) {
|
||||
$data = mysqli_fetch_array($query);
|
||||
$_SESSION['users'] = $data;
|
||||
echo '<script>alert("Selamat datang, '.$data['nama'].'"); location.href="index.php";</script>';
|
||||
} else {
|
||||
echo '<script>alert("Username atau Password tidak sesuai");</script>';
|
||||
}
|
||||
}
|
||||
?>
|
||||
|
||||
<form action="" method="post">
|
||||
<table align="center">
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user