diff --git a/leaderboard.php b/leaderboard.php index df4f5e3..7c7e67b 100644 --- a/leaderboard.php +++ b/leaderboard.php @@ -1,67 +1,257 @@ query("SHOW TABLES LIKE 'leaderboard_sudoku'"); + if ($checkTable->rowCount() == 0) { + $createSql = " + CREATE TABLE leaderboard_sudoku ( + id INT AUTO_INCREMENT PRIMARY KEY, + username VARCHAR(50), + difficulty VARCHAR(10), + time_seconds INT, + created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP + )"; + $conn->exec($createSql); + } +} catch (PDOException $e) { + // Fail silently or log, but attempting to continue might show empty leaderboard instead of crash +} -$stmt = $conn->query($sql); -$data = $stmt->fetchAll(); +// Helper function to get top 10 by difficulty +function getLeaderboard($conn, $difficulty) { + $sql = " + SELECT username, time_seconds, created_at + FROM leaderboard_sudoku + WHERE difficulty = :difficulty + ORDER BY time_seconds ASC + LIMIT 10 + "; + $stmt = $conn->prepare($sql); + $stmt->execute(['difficulty' => $difficulty]); + return $stmt->fetchAll(PDO::FETCH_ASSOC); +} + +// Fetch data for each level +$easyData = getLeaderboard($conn, 'easy'); +$mediumData = getLeaderboard($conn, 'medium'); +$hardData = getLeaderboard($conn, 'hard'); ?> - + + +
+ + +| Rank | Player | Waktu |
|---|---|---|
| #{$rank} | "; + echo "" . htmlspecialchars($row['username']) . " | "; + echo "{$timeStr} | "; + echo "