diff --git a/content/search-results.php b/content/search-results.php index 31430a7..c015d0a 100644 --- a/content/search-results.php +++ b/content/search-results.php @@ -4,18 +4,48 @@ if (session_status() === PHP_SESSION_NONE) { } $all_results = $_SESSION["search_results"] ?? []; -$query = $_SESSION["search_query"] ?? ""; -$totalResultsCount = count($all_results); +$query = $_SESSION["search_query"] ?? ""; + +// --------------------------------------------------------------- +// Parameter aus der URL lesen & validieren +// --------------------------------------------------------------- +$allowedCategories = [ + 'all', 'Deutsch', 'Englisch', 'Franzoesisch', 'Latein', 'Literatur', + 'Mathematik', 'Biologie', 'Informatik', 'Chemie', 'Physik', 'Astronomie', + 'Geschichte', 'Erdkunde', 'Sozialkunde', 'Wirtschaftskunde', 'Religion', + 'Ethikunterricht', 'Philosophie', 'Psychologie', 'Kunst', 'Musik', + 'Theater', 'Technik', 'Werken', 'Hauswirtschaft', 'Sport', +]; + +$category = $_GET['category'] ?? 'all'; +if (!in_array($category, $allowedCategories, true)) { + $category = 'all'; +} $limit = isset($_GET['limit']) ? (int)$_GET['limit'] : 10; -if (!in_array($limit, [10, 20, 50, 100])) { +if (!in_array($limit, [10, 20, 50, 100], true)) { $limit = 10; } -// Gesamtseitenzahl -$totalPages = max(1, ceil($totalResultsCount / $limit)); +$currentSort = $_SESSION['search_sort'] ?? 'alphabet'; + +// --------------------------------------------------------------- +// Diese Berechnung ist der Server-seitige Fallback für Nutzer ohne +// JavaScript. Sobald JS aktiv ist, übernimmt js/search-results.js +// Filterung, Sortierung und Pagination komplett im Browser (auf +// Basis des unten eingebetteten JSON-Datensatzes) und überschreibt +// diese Ausgabe live, ohne dass eine neue Seite geladen wird. +// --------------------------------------------------------------- +$filteredResults = $all_results; +if ($category !== 'all') { + $filteredResults = array_values(array_filter($filteredResults, function ($item) use ($category) { + return ($item['category'] ?? '') === $category; + })); +} + +$totalResultsCount = count($filteredResults); +$totalPages = max(1, (int)ceil($totalResultsCount / $limit)); -// Aktuelle Seite auslesen und validieren $currentPage = isset($_GET['page']) ? (int)$_GET['page'] : 1; if ($currentPage < 1) { $currentPage = 1; @@ -23,42 +53,68 @@ if ($currentPage < 1) { $currentPage = $totalPages; } -// Startpunkt im Array berechnen (Offset) -$offset = ($currentPage - 1) * $limit; - -// Nur die Ergebnisse für die aktuelle Seite ausschneiden -//$results = array_slice($all_results, $offset, $limit); -$results = $all_results; +$offset = ($currentPage - 1) * $limit; +$results = array_slice($filteredResults, $offset, $limit); $resultCount = count($results); +// Hilfsfunktion, um Pagination-Links zu bauen, die alle aktuellen +// Parameter (q, sort, limit, category) beibehalten. +function buildPageUrl(int $page, string $query, string $sort, int $limit, string $category): string +{ + $params = [ + 'pfad' => 'search-results', + 'q' => $query, + 'sort' => $sort, + 'limit' => $limit, + 'category' => $category, + 'page' => $page, + ]; + return 'index.php?' . http_build_query($params); +} + +// --------------------------------------------------------------- +// JSON-Datensatz für die clientseitige Aufbereitung (Filter, +// Sortierung, Pagination) durch js/search-results.js. Enthält ALLE +// Treffer der Suche (unabhängig von Kategorie/Sortierung/Seite), +// damit im Browser ohne weitere Serveranfragen gearbeitet werden kann. +// --------------------------------------------------------------- +$jsonItems = array_map(function ($item) { + $likes = isset($item['likes']) && is_array($item['likes']) ? count($item['likes']) : ($item['likes'] ?? 0); + return [ + 'id' => $item['id'] ?? null, + 'title' => $item['title'] ?? '', + 'author' => $item['author'] ?? '', + 'category' => $item['category'] ?? '', + 'likes' => $likes, + 'creationDate'=> $item['creationDate'] ?? '', + ]; +}, $all_results); ?>
- - +
-

Suchergebnisse

-

Treffer für Ihre Suchanfrage ""

+

+ Treffer für Ihre Suchanfrage "" +

- +
- - +
@@ -141,35 +178,25 @@ $resultCount = count($results);

Von:

- - - ❤️ - +
- - +

Keine Beiträge zu diesem Suchbegriff gefunden.

- +

Unzulässige Suchanfrage

- - - +
- + + + \ No newline at end of file diff --git a/index.php b/index.php index c9a5cfb..1f78d39 100644 --- a/index.php +++ b/index.php @@ -65,11 +65,12 @@ if ($pfad === "deleteAccount") { - - + - + + EduForge diff --git a/js/paginator.js b/js/paginator.js index 378174b..10afb41 100644 --- a/js/paginator.js +++ b/js/paginator.js @@ -1,27 +1,203 @@ -function initPaginator() { - const form = document.getElementById('search-form-id'); - const pageInput = document.getElementById('s-res-page-input'); - const pageButtons = document.querySelectorAll('.s-res-page-navigation .s-res-page-btn'); +/** + * Übernimmt Filterung (Kategorie), Sortierung und Pagination der + * Suchergebnisse vollständig clientseitig, ohne Neuladen der Seite. + * + * Voraussetzung: Im HTML liegt ein