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 +++++++++++++++++++++++++++--------- public/emotesets.php | 37 ++++++++++++++++++++++++++----------- public/users.php | 28 +++++++++++++++++++++------- 3 files changed, 74 insertions(+), 27 deletions(-) (limited to 'public') 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 '' ?> +
+
+
diff --git a/public/emotesets.php b/public/emotesets.php index ebec077..fdf32d5 100644 --- a/public/emotesets.php +++ b/public/emotesets.php @@ -13,6 +13,10 @@ $db = new PDO(DB_URL, DB_USER, DB_PASS); $emote_sets = null; $emote_set = null; +$page = max(1, intval($_GET["p"] ?? "0")); +$total_emotesets = 1; +$total_pages = 1; + if ($id == "global") { $stmt = $db->prepare("SELECT * FROM emote_sets WHERE is_global = true"); $stmt->execute(); @@ -36,9 +40,8 @@ if ($id == "global") { } } } else if (intval($id) <= 0 && intval($alias_id) <= 0) { - $page = intval($_GET["p"] ?? "0"); $limit = 20; - $offset = $page * $limit; + $offset = ($page - 1) * $limit; $stmt = $db->prepare("SELECT * FROM emote_sets LIMIT ? OFFSET ?"); $stmt->bindParam(1, $limit, PDO::PARAM_INT); @@ -63,6 +66,10 @@ if ($id == "global") { } } } + $count_stmt = $db->prepare("SELECT COUNT(*) FROM emote_sets"); + $count_stmt->execute(); + $total_emotesets = intval($count_stmt->fetch()[0]); + $total_pages = ceil($total_emotesets / $limit); } else if (intval($alias_id) > 0) { $alias_id = intval($alias_id); $stmt = $db->prepare("SELECT es.* FROM emote_sets es @@ -153,18 +160,16 @@ if (CLIENT_REQUIRES_JSON) {
-
'; + + if ($total_pages > 1) { + echo '' ?> +
+ + '; @@ -195,10 +211,9 @@ if (CLIENT_REQUIRES_JSON) { } } ?> -
+ - diff --git a/public/users.php b/public/users.php index 123d37f..914f890 100644 --- a/public/users.php +++ b/public/users.php @@ -16,9 +16,9 @@ $alias_id = $_GET["alias_id"] ?? ""; $db = new PDO(DB_URL, DB_USER, DB_PASS); if ($id == "" && $alias_id == "") { - $page = $_GET["p"] ?? "0"; - $limit = 50; - $offset = $page * $limit; + $page = max(1, intval($_GET["p"] ?? "1")); + $limit = 25; + $offset = ($page - 1) * $limit; $search = "%" . ($_GET["q"] ?? "") . "%"; $stmt = $db->prepare("SELECT id, username, joined_at, last_active_at FROM users @@ -29,7 +29,11 @@ if ($id == "" && $alias_id == "") { $stmt->bindParam(3, $offset, PDO::PARAM_INT); $stmt->execute(); - $all_user_count = $search ? $stmt->rowCount() : $db->query("SELECT COUNT(*) FROM users")->fetch()[0]; + $count_stmt = $db->prepare("SELECT COUNT(*) FROM users WHERE username LIKE ?"); + $count_stmt->execute([$search]); + + $total_users = $count_stmt->fetch()[0]; + $total_pages = ceil($total_users / $limit); if ($is_json) { header("Content-Type: application/json"); @@ -63,11 +67,11 @@ if ($id == "" && $alias_id == "") {
rowCount() != 0) { + if ($total_users != 0) { echo ''; echo ''; echo ''; @@ -94,8 +98,18 @@ if ($id == "" && $alias_id == "") { ?> + 1) { + echo '' ?> + +
+ +
+ - -- cgit v1.2.3
UsernameLast active