From 3e81200d3548ba0f3a732928b18468ed4744f287 Mon Sep 17 00:00:00 2001
From: MHEN2606 <matthew.mahendra@gmail.com>
Date: Wed, 15 Nov 2023 18:16:50 +0700
Subject: [PATCH] feat: Pagination API on University Stats

---
 api/university/stats.php | 30 ++++++++++++++++++++++++++++++
 1 file changed, 30 insertions(+)

diff --git a/api/university/stats.php b/api/university/stats.php
index ad6c999..65b8581 100644
--- a/api/university/stats.php
+++ b/api/university/stats.php
@@ -24,7 +24,37 @@ INNER JOIN (
         university_id
 ) counts ON uni.university_id = counts.university_id";
 
+$whereClauses = [];
+$params = [];
+$types = "";
+
+if(isset($_GET["uid"]) && $_GET['uid'] != NULL){
+    $whereClauses[] = " uni.university_id = ? ";
+    $params[] = $_GET['uid'];
+    $types .= 'i';
+}
+
+if(isset($_GET["name"]) && ($_GET['name'] != NULL || $_GET['name'] == "undefined") ){
+    $whereClauses[] = ' u.name LIKE ? ';
+    $params[] = "%".$_GET['name']."%";
+    $types .= 's';
+}
+
+if($whereClauses != NULL){
+    $query .= ' WHERE '.implode(" AND ", $whereClauses);
+}
+if(isset($_GET["currentpage"]) && $_GET["currentpage"] != NULL){
+    $query .= "LIMIT ? OFFSET ?";
+    $offset = ($_GET['currentpage'] - 1) * $_GET['itemsperpage'];
+    $types .= "ii";
+    $params[] = $_GET['itemsperpage'];
+    $params[] = $offset;
+}
+
 $stmt = $db->setSTMT($query);
+if($types != ''){
+    mysqli_stmt_bind_param($stmt, $types, ...$params);
+}
 $result = mysqli_stmt_execute($stmt);
 $row = mysqli_fetch_all(mysqli_stmt_get_result($stmt), MYSQLI_ASSOC);
 
-- 
GitLab