diff options
| author | ilotterytea <iltsu@alright.party> | 2025-04-30 00:17:39 +0500 |
|---|---|---|
| committer | ilotterytea <iltsu@alright.party> | 2025-04-30 00:17:39 +0500 |
| commit | 6e27fb9de11843b7bb605fe85cb628b6e4882787 (patch) | |
| tree | c4e3cf3cbf705267b24bc5ddf1fcd355b4441b56 | |
| parent | 8d6cb83506bcaa6b48aa1b24c31678a83b69374b (diff) | |
feat: PAGination!
| -rw-r--r-- | public/emotes/index.php | 36 | ||||
| -rw-r--r-- | public/emotesets.php | 37 | ||||
| -rw-r--r-- | public/users.php | 28 | ||||
| -rw-r--r-- | src/config.php | 2 | ||||
| -rw-r--r-- | src/partials.php | 23 | ||||
| -rw-r--r-- | src/utils.php | 2 |
6 files changed, 99 insertions, 29 deletions
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) { <?php display_alert() ?> <section class="box"> <div class="box navtab"> - <?php echo empty($emotes) ? "Emote - " . $emote->get_code() : "Emotes" ?> + <?php echo empty($emotes) ? "Emote - " . $emote->get_code() : "$total_emotes Emotes - Page $page/$total_pages" ?> </div> <?php if (empty($emotes)) { ?> @@ -371,7 +379,17 @@ if (CLIENT_REQUIRES_JSON) { } ?> </div> - <?php + <?php if ($total_pages > 1) { + echo '' ?> + </section> + <section class="box center row"> + <?php + html_pagination( + $total_pages, + $page, + "/emotes?q=" . substr($search, 1, strlen($search) - 2) . "&sort_by=$sort_by" + ); + } } ?> </section> 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) { <div class="wrapper"> <?php html_navigation_bar() ?> <section class="content row"> - <section class="sidebar"> - <?php html_navigation_search() ?> - </section> <section class="content"> <section class="box"> <div class="box navtab"> - <?php echo $emote_sets != null ? (count($emote_sets) . " emotesets") : ('"' . $emote_set["name"] . '" emoteset') ?> + <?php echo $emote_sets != null ? ("$total_emotesets emotesets - Page $page/$total_pages") : ('"' . $emote_set["name"] . '" emoteset') ?> </div> <div class="box content items"> <?php if ($emote_sets != null) { - foreach ($emote_sets as $set_row) { ?> + foreach ($emote_sets as $set_row) { + ?> <a href="/emotesets.php?id=<?php echo $set_row["id"] ?>" class="box"> <div> <?php @@ -178,13 +183,24 @@ if (CLIENT_REQUIRES_JSON) { <div> <?php - foreach ($set_row["emotes"] as $e) { - echo '<img src="/static/userdata/emotes/' . $e["id"] . '/1x.' . $e["ext"] . '">'; + foreach ($set_row["emotes"] as $emm) { + echo '<img src="/static/userdata/emotes/' . $emm["id"] . '/1x.' . $emm["ext"] . '">'; } ?> </div> </a> <?php } + + echo '</div></section>'; + + if ($total_pages > 1) { + echo '' ?> + <section class="box center row"> + <?php + html_pagination($total_pages, $page, "/emotesets.php"); + ?> + <?php + } } else { foreach ($emote_set["emotes"] as $emote_row) { echo '<a class="box emote" href="/emotes?id=' . $emote_row["id"] . '">'; @@ -195,10 +211,9 @@ if (CLIENT_REQUIRES_JSON) { } } ?> - </div> + </section> </section> </section> - </section> </div> </div> </body> 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 == "") { <section class="content"> <section class="box"> <div class="box navtab"> - <p><?php echo $all_user_count ?> Users</p> + <p><?php echo $total_users ?> Users - <?php echo "Page $page/$total_pages" ?></p> </div> <div class="box content"> <?php - if ($stmt->rowCount() != 0) { + if ($total_users != 0) { echo '<table>'; echo '<tr>'; echo '<th></th><th style="width:80%;">Username</th><th>Last active</th>'; @@ -94,8 +98,18 @@ if ($id == "" && $alias_id == "") { ?> </div> </section> + <?php if ($total_pages > 1) { + echo '' ?> + + <section class="box center row"> + <?php + html_pagination($total_pages, $page, "/users.php?q=" . substr($search, 1, strlen($search) - 2)); + ?> + </section> + <?php + } + ?> </section> - </section> </div> </div> </body> diff --git a/src/config.php b/src/config.php index 7c740d1..e224b72 100644 --- a/src/config.php +++ b/src/config.php @@ -1,4 +1,6 @@ <?php +define("CLIENT_REQUIRES_JSON", isset($_SERVER["HTTP_ACCEPT"]) && $_SERVER["HTTP_ACCEPT"] == "application/json"); + // DATABASE define("DB_USER", "kochan"); define("DB_PASS", "kochan"); diff --git a/src/partials.php b/src/partials.php index 0b21b50..9b02a7b 100644 --- a/src/partials.php +++ b/src/partials.php @@ -94,4 +94,27 @@ function html_navigation_search() </div> </section> <?php ; +} + +function html_pagination(int $total_pages, int $current_page, string $redirect) +{ + if (str_contains($redirect, "?")) { + $redirect .= "&p="; + } else { + $redirect .= "?p="; + } + + if ($total_pages > 1) { + echo '' ?> + <div class="pagination"> + <?php if ($current_page > 1): ?> + <a href="<?php echo $redirect . ($current_page - 1) ?>">[ prev ]</a> + <?php endif; ?> + <?php if ($current_page < $total_pages): ?> + <a href="<?php echo $redirect . ($current_page + 1) ?>">[ next ]</a> + <?php endif; ?> + + </div> + <?php ; + } }
\ No newline at end of file diff --git a/src/utils.php b/src/utils.php index a11fa16..6356cfb 100644 --- a/src/utils.php +++ b/src/utils.php @@ -1,6 +1,4 @@ <?php -define("CLIENT_REQUIRES_JSON", isset($_SERVER["HTTP_ACCEPT"]) && $_SERVER["HTTP_ACCEPT"] == "application/json"); - function json_response(mixed $response, int $status = 200) { http_response_code($status); |
