From bb05b244f95bb6f507c9e825efd4da4d3d93662b Mon Sep 17 00:00:00 2001 From: ilotterytea Date: Tue, 22 Apr 2025 03:48:11 +0500 Subject: feat: search functionality --- public/emotes/index.php | 22 ++++++++++++++++++---- public/users.php | 19 +++++++++++++++---- src/partials.php | 4 ++-- 3 files changed, 35 insertions(+), 10 deletions(-) diff --git a/public/emotes/index.php b/public/emotes/index.php index 63bc9da..b377430 100644 --- a/public/emotes/index.php +++ b/public/emotes/index.php @@ -7,6 +7,7 @@ authorize_user(); function display_list_emotes(int $page, int $limit): array { + $search = $_GET["q"] ?? ""; $user_id = $_SESSION["user_id"] ?? "-1"; $offset = $page * $limit; $db = new PDO(DB_URL, DB_USER, DB_PASS); @@ -17,13 +18,26 @@ function display_list_emotes(int $page, int $limit): array INNER JOIN emote_sets es ON es.id = ec.emote_set_id WHERE ec.emote_id = e.id AND es.owner_id = ? ) THEN 1 ELSE 0 END AS is_in_user_set - FROM emotes e + FROM emotes e " . + (($search != "") ? "WHERE e.code LIKE ?" : "") + . + " ORDER BY e.created_at ASC LIMIT ? OFFSET ? "); - $stmt->bindParam(1, $user_id, PDO::PARAM_INT); - $stmt->bindParam(2, $limit, PDO::PARAM_INT); - $stmt->bindParam(3, $offset, PDO::PARAM_INT); + + if ($search == "") { + $stmt->bindParam(1, $user_id, PDO::PARAM_INT); + $stmt->bindParam(2, $limit, PDO::PARAM_INT); + $stmt->bindParam(3, $offset, PDO::PARAM_INT); + } else { + $search = "%$search%"; + $stmt->bindParam(1, $user_id, PDO::PARAM_INT); + $stmt->bindParam(2, $search, PDO::PARAM_STR); + $stmt->bindParam(3, $limit, PDO::PARAM_INT); + $stmt->bindParam(4, $offset, PDO::PARAM_INT); + } + $stmt->execute(); $emotes = []; diff --git a/public/users.php b/public/users.php index 73f1d54..fae1e70 100644 --- a/public/users.php +++ b/public/users.php @@ -17,12 +17,23 @@ if ($id == "") { $page = $_GET["p"] ?? "0"; $limit = 50; $offset = $page * $limit; - $stmt = $db->prepare("SELECT id, username, joined_at, last_active_at FROM users ORDER BY last_active_at DESC LIMIT ? OFFSET ?"); - $stmt->bindParam(1, $limit, PDO::PARAM_INT); - $stmt->bindParam(2, $offset, PDO::PARAM_INT); + $search = $_GET["q"] ?? ""; + $stmt = $db->prepare("SELECT id, username, joined_at, last_active_at + FROM users + " . ($search != "" ? " WHERE username LIKE ? " : "") . " + ORDER BY last_active_at DESC LIMIT ? OFFSET ?"); + if ($search == "") { + $stmt->bindParam(1, $limit, PDO::PARAM_INT); + $stmt->bindParam(2, $offset, PDO::PARAM_INT); + } else { + $search = "%$search%"; + $stmt->bindParam(1, $search, PDO::PARAM_STR); + $stmt->bindParam(2, $limit, PDO::PARAM_INT); + $stmt->bindParam(3, $offset, PDO::PARAM_INT); + } $stmt->execute(); - $all_user_count = $db->query("SELECT COUNT(*) FROM users")->fetch()[0]; + $all_user_count = $search ? $stmt->rowCount() : $db->query("SELECT COUNT(*) FROM users")->fetch()[0]; if ($is_json) { header("Content-Type: application/json"); diff --git a/src/partials.php b/src/partials.php index 0087df4..d245708 100644 --- a/src/partials.php +++ b/src/partials.php @@ -35,8 +35,8 @@ function html_navigation_search() Search...
-
-
+ " method="GET"> + ">
-- cgit v1.2.3