add search functionality

This commit is contained in:
Eric Blommel
2024-12-10 19:36:54 +01:00
parent c8c9c64ef2
commit b6a9585125
3 changed files with 130 additions and 47 deletions

40
webseite/search.php Normal file
View File

@@ -0,0 +1,40 @@
<?php
require_once("classes/SubjectData.php");
require_once("classes/TopicData.php");
if (!isset($_GET['query'])) {
die(json_encode([]));
}
$query = strtolower(trim($_GET['query']));
$subjects = SubjectData::getAll();
$results = [];
foreach ($subjects as $subject) {
if (strpos(strtolower($subject->displayName), $query) !== false) {
$results[] = [
'type' => 'subject',
'id' => $subject->id,
'displayName' => $subject->displayName
];
}
foreach ($subject->topics as $topic) {
if (
strpos(strtolower($subject->displayName), $query) !== false ||
strpos(strtolower($topic->displayName), $query) !== false ||
strpos(strtolower($topic->description), $query) !== false ||
strpos(strtolower($topic->article), $query) !== false
) {
$results[] = [
'type' => 'topic',
'subjectId' => $topic->subjectId,
'id' => $topic->id,
'displayName' => $subject->displayName . ' - ' . $topic->displayName
];
}
}
}
header('Content-Type: application/json');
echo json_encode($results);
?>