From 6e27fb9de11843b7bb605fe85cb628b6e4882787 Mon Sep 17 00:00:00 2001 From: ilotterytea Date: Wed, 30 Apr 2025 00:17:39 +0500 Subject: feat: PAGination! --- public/emotes/index.php | 36 +++++++++++++++++++++++++++--------- 1 file changed, 27 insertions(+), 9 deletions(-) (limited to 'public/emotes') diff --git a/public/emotes/index.php b/public/emotes/index.php index d37b3f5..b6308a5 100644 --- a/public/emotes/index.php +++ b/public/emotes/index.php @@ -10,13 +10,12 @@ authorize_user(); $db = new PDO(DB_URL, DB_USER, DB_PASS); -function display_list_emotes(PDO &$db, int $page, int $limit): array +function display_list_emotes(PDO &$db, string $search, string $sort_by, int $page, int $limit): array { - $search = "%" . ($_GET["q"] ?? "") . "%"; $user_id = $_SESSION["user_id"] ?? "-1"; - $offset = $page * $limit; + $offset = ($page - 1) * $limit; - $sort = match ($_GET["sort_by"] ?? "") { + $sort = match ($sort_by) { "low_ratings" => "rating ASC", "recent" => "e.created_at DESC", "oldest" => "e.created_at ASC", @@ -120,10 +119,19 @@ $id = $_GET["id"] ?? ""; $db = new PDO(DB_URL, DB_USER, DB_PASS); +$page = max(1, intval($_GET["p"] ?? "1")); +$limit = 50; +$total_emotes = 0; +$total_pages = 0; +$search = "%" . ($_GET["q"] ?? "") . "%"; +$sort_by = $_GET["sort_by"] ?? ""; + if ($id == "" || !is_numeric($id)) { - $page = intval($_GET["p"] ?? "0"); - $limit = 50; - $emotes = display_list_emotes($db, $page, $limit); + $emotes = display_list_emotes($db, $search, $sort_by, $page, $limit); + $stmt = $db->prepare("SELECT COUNT(*) FROM emotes WHERE code LIKE ?"); + $stmt->execute([$search]); + $total_emotes = $stmt->fetch()[0]; + $total_pages = ceil($total_emotes / $limit); } else { $emote = display_emote($db, intval($id)); } @@ -160,7 +168,7 @@ if (CLIENT_REQUIRES_JSON) {
@@ -371,7 +379,17 @@ if (CLIENT_REQUIRES_JSON) { } ?> - 1) { + echo '' ?> +
+
+
-- cgit v1.2.3