Implementierung von neuem such algorithmus und entsprechenden anpassungen in anderen dateien
This commit is contained in:
@@ -75,10 +75,12 @@ $resultCount = count($results);
|
||||
<?php foreach ($results as $item): ?>
|
||||
<div class="s-res-item">
|
||||
<div class="s-res-content">
|
||||
<h2 class="s-res-item-title">
|
||||
<a href="<?php echo $item['link']; ?>" class="s-res-link"><?php echo $item['title']; ?></a>
|
||||
<h2 class="s-res-item-title">
|
||||
<a href="index.php?pfad=showArticle&id=<?php echo $item->id; ?>" class="s-res-link">
|
||||
<?php echo htmlspecialchars($item->title); ?>
|
||||
</a>
|
||||
</h2>
|
||||
<p class="s-res-author">Kategorie: <span class="s-res-author-name">Beitrag</span></p>
|
||||
<p class="s-res-author">Von: <span class="s-res-author-name"><?php echo htmlspecialchars($item->author); ?></span></p>
|
||||
</div>
|
||||
<div class="s-res-arrow">→</div>
|
||||
</div>
|
||||
|
||||
+2
-2
@@ -2,10 +2,10 @@
|
||||
Suchleiste. Wird via PHP später in alle Seiten eingebunden
|
||||
-->
|
||||
<!--<label for="site-search">Suche</label>-->
|
||||
<form action="index.php" method="GET" class="search-form">
|
||||
<form action="php/controller/search-results-controller.php" method="GET" class="search-form">
|
||||
|
||||
<input type="hidden" name="pfad" value="search-results">
|
||||
|
||||
<input type="search" id="site-search" name="q" placeholder="Suchen..." class="nav__search" required>
|
||||
<button type="submit" class="nav__search-button">Suchen</button>
|
||||
</form>
|
||||
</form>
|
||||
|
||||
@@ -24,6 +24,8 @@ if ($_SERVER["REQUEST_METHOD"] === "GET" && isset($_GET["q"])) {
|
||||
$_SESSION["message"] = "internal_error";
|
||||
}
|
||||
}
|
||||
header("Location: ../../index.php?pfad=search-results");
|
||||
exit();
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -169,24 +169,34 @@ class LocalArticleManager implements ArticleManagerDAO {
|
||||
return $filteredArticles;
|
||||
}
|
||||
|
||||
public function search(string $keyword): array
|
||||
// getarticlesbyauthor nutzen um auch nach kategorien zu suchen
|
||||
public function search(string $keyword): array
|
||||
{
|
||||
$results = [];
|
||||
$contentFolder = __DIR__ . '/../content/';
|
||||
$files = glob($contentFolder . "*.php");
|
||||
// 1. Alle Artikel aus der JSON-Datei laden
|
||||
$articles = $this->getAllArticles();
|
||||
$filteredArticles = [];
|
||||
|
||||
foreach ($files as $file) {
|
||||
$content = file_get_contents($file);
|
||||
if (stripos($content, $keyword) !== false) {
|
||||
$filename = basename($file, ".php");
|
||||
$results[] = [
|
||||
'title' => ucfirst($filename),
|
||||
'link' => "index.php?pfad=" . $filename
|
||||
];
|
||||
// 2. Artikel durchlaufen und filtern
|
||||
foreach ($articles as $article) {
|
||||
$inTitle = isset($article['title']) && stripos($article['title'], $keyword) !== false;
|
||||
$inContent = isset($article['content']) && stripos($article['content'], $keyword) !== false;
|
||||
|
||||
// Wenn das Keyword im Titel ODER im Inhalt vorkommt
|
||||
if ($inTitle || $inContent) {
|
||||
// Wir mappen die JSON-Daten auf ein echtes Article-Objekt (genau wie bei getArticlesByAuthor)
|
||||
$filteredArticles[] = new Article(
|
||||
intval($article['id']),
|
||||
$article['title'],
|
||||
$article['content'],
|
||||
$article['author'],
|
||||
$article['category'],
|
||||
$article['tags'],
|
||||
$article['creationDate']
|
||||
);
|
||||
}
|
||||
}
|
||||
return $results;
|
||||
|
||||
return $filteredArticles;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user