Compare commits
2 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 830b57d245 | |||
| 06930644a6 |
@@ -76,9 +76,11 @@ $resultCount = count($results);
|
||||
<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>
|
||||
<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>
|
||||
|
||||
+1
-1
@@ -2,7 +2,7 @@
|
||||
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">
|
||||
|
||||
|
||||
@@ -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,42 @@ class LocalArticleManager implements ArticleManagerDAO {
|
||||
return $filteredArticles;
|
||||
}
|
||||
|
||||
public function search(string $keyword): array
|
||||
{
|
||||
$results = [];
|
||||
$contentFolder = __DIR__ . '/../content/';
|
||||
$files = glob($contentFolder . "*.php");
|
||||
// getarticlesbyauthor nutzen um auch nach kategorien zu suchen
|
||||
public function search(string $keyword): array {
|
||||
$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
|
||||
];
|
||||
// Sicherheits-Check: Falls getAllArticles aus irgendeinem Grund kein Array liefert
|
||||
if (!is_array($articles)) {
|
||||
return [];
|
||||
}
|
||||
|
||||
// Suchbegriff trimmen und in Kleinbuchstaben umwandeln für besseren Vergleich
|
||||
$cleanKeyword = mb_strtolower(trim($keyword));
|
||||
|
||||
foreach ($articles as $article) {
|
||||
// Sicherstellen, dass die Felder existieren und Strings sind
|
||||
$title = isset($article['title']) ? mb_strtolower((string)$article['title']) : '';
|
||||
$content = isset($article['content']) ? mb_strtolower((string)$article['content']) : '';
|
||||
|
||||
// Suche im Titel ODER im Inhalt
|
||||
if (($cleanKeyword !== '' && strpos($title, $cleanKeyword) !== false) ||
|
||||
($cleanKeyword !== '' && strpos($content, $cleanKeyword) !== false)) {
|
||||
|
||||
// Wir mappen die Daten auf das Article-Objekt (wie in deiner getArticlesByAuthor)
|
||||
$filteredArticles[] = new Article(
|
||||
intval($article['id'] ?? 0),
|
||||
$article['title'] ?? '',
|
||||
$article['content'] ?? '',
|
||||
$article['author'] ?? '',
|
||||
$article['category'] ?? '',
|
||||
$article['tags'] ?? '',
|
||||
$article['creationDate'] ?? ''
|
||||
);
|
||||
}
|
||||
}
|
||||
return $results;
|
||||
|
||||
return $filteredArticles;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user