Leaderboard Fix

This commit is contained in:
angelicatesvara07-crypto 2025-12-15 19:57:21 +07:00
parent 9dae58f29d
commit d27b2bf3f9
4 changed files with 38 additions and 108 deletions

View File

@ -794,13 +794,17 @@ if (!isset($_SESSION['username'])) {
}
function saveScore() {
fetch("save_score.php", {
fetch("/kelompok03-codeplay-Angelica-Rinaldo-Farrel/save_score.php", {
method: "POST",
headers: { "Content-Type": "application/x-www-form-urlencoded" },
body: `difficulty=${currentDifficulty}&time=${secondsElapsed}`
});
headers: {
"Content-Type": "application/x-www-form-urlencoded"
},
body: 'difficulty=${currentDifficulty}&time=${secondsElapsed}'
})
.then(res => res.text())
.then(data => console.log("SAVE SCORE:", data))
.catch(err => console.error(err));
}
</script>
</body>
</html>

View File

@ -1,11 +1,6 @@
<?php
require_once 'db.php';
// TES KONEKSI
if (!$conn) {
die("DB connection failed");
}
$sql = "
SELECT username, difficulty, time_seconds
FROM leaderboard_sudoku
@ -13,7 +8,8 @@ $sql = "
LIMIT 10
";
$result = $conn->query($sql);
$stmt = $conn->query($sql);
$data = $stmt->fetchAll();
?>
<style>
@ -48,8 +44,8 @@ $result = $conn->query($sql);
<th>Time</th>
</tr>
<?php if ($result && $result->num_rows > 0): ?>
<?php while ($row = $result->fetch_assoc()): ?>
<?php if (count($data) > 0): ?>
<?php foreach ($data as $row): ?>
<tr>
<td><?= htmlspecialchars($row['username']) ?></td>
<td><?= strtoupper($row['difficulty']) ?></td>
@ -61,7 +57,7 @@ $result = $conn->query($sql);
) ?>
</td>
</tr>
<?php endwhile; ?>
<?php endforeach; ?>
<?php else: ?>
<tr>
<td colspan="3">Belum ada data</td>
@ -69,6 +65,3 @@ $result = $conn->query($sql);
<?php endif; ?>
</table>
</div>
<?php $conn->close(); ?>

View File

@ -1,13 +0,0 @@
<?php
session_start();
require "koneksi.php"; // sesuaikan dengan file koneksi DB kamu
if (!isset($_SESSION['username'])) exit;
$username = $_SESSION['username'];
$difficulty = $_POST['difficulty'];
$time = intval($_POST['time']);
$stmt = $conn->prepare("INSERT INTO leaderboard_sudoku (username, difficulty, time_seconds) VALUES (?, ?, ?)");
$stmt->bind_param("ssi", $username, $difficulty, $time);
$stmt->execute();

View File

@ -1,84 +1,30 @@
<?php
session_start();
require_once 'db.php';
$sql = "
SELECT username, difficulty, time_seconds
FROM leaderboard_sudoku
ORDER BY difficulty,
time_seconds ASC
";
$result = $conn->query($sql);
?>
<!DOCTYPE html>
<html>
<head>
<title>Leaderboard Sudoku</title>
<style>
body {
font-family: Arial;
background: #1e1e1e;
color: #fff;
padding: 40px;
if (!isset($_SESSION['username'])) {
echo "NO_SESSION";
exit;
}
table {
border-collapse: collapse;
width: 600px;
margin: auto;
background: #2b2b2b;
if (!isset($_POST['difficulty'], $_POST['time'])) {
echo "NO_POST";
exit;
}
th, td {
padding: 12px;
text-align: center;
border-bottom: 1px solid #444;
}
th {
background: #3b3b3b;
}
h1 {
text-align: center;
margin-bottom: 20px;
}
.back {
display: block;
margin: 30px auto;
width: 200px;
text-align: center;
padding: 10px;
background: #4caf50;
color: white;
text-decoration: none;
border-radius: 6px;
}
</style>
</head>
<body>
<h1>🏆 Leaderboard Sudoku</h1>
$username ='testuser';
$difficulty = $_POST['difficulty'];
$time = (int) $_POST['time'];
<table>
<tr>
<th>Username</th>
<th>Difficulty</th>
<th>Time (mm:ss)</th>
</tr>
$sql = "INSERT INTO leaderboard_sudoku
(username, difficulty, time_seconds)
VALUES (:username, :difficulty, :time)";
<?php while ($row = $result->fetch(PDO::FETCH_ASSOC)): ?>
<tr>
<td><?= htmlspecialchars($row['username']) ?></td>
<td><?= strtoupper($row['difficulty']) ?></td>
<td>
<?= sprintf("%02d:%02d",
floor($row['time_seconds'] / 60),
$row['time_seconds'] % 60
) ?>
</td>
</tr>
<?php endwhile; ?>
$stmt = $conn->prepare($sql);
$stmt->execute([
':username' => $username,
':difficulty' => $difficulty,
':time' => $time
]);
</table>
<a href="index.php" class="back"> Kembali</a>
</body>
</html>
echo "SUCCESS";