search-Methode in DatabaseArticleManager

This commit is contained in:
2026-06-10 20:39:27 +02:00
parent b6e7da6808
commit a19d1db990
+35 -2
View File
@@ -306,8 +306,41 @@ class DatabaseArticleManager implements ArticleManagerDAO {
public function search(string $keyword): array
{
// TODO: implement search()
return [];
$cleankeyword = trim($keyword);
// Leeren Suchbegriff sofort abfangen
if ($cleankeyword === '') {
return [];
}
$query = "SELECT id, title, content, author, category, tags, creationdate
FROM articles
WHERE LOWER(title) LIKE :keyword
OR LOWER(content) LIKE :keyword";
$stmt = $this->db->prepare($query);
// Wildcards für die LIKE-Suche hinzufügen und Keyword in Kleinbuchstaben umwandeln
$searchParam = '%' . strtolower($cleankeyword) . '%';
$stmt->bindValue(':keyword', $searchParam, PDO::PARAM_STR);
$stmt->execute();
$articles = $stmt->fetchAll(PDO::FETCH_ASSOC);
$filteredArticles = [];
foreach ($articles as $article) {
$filteredArticles[] = new Article(
intval($article['id'] ?? 0),
$article['title'] ?? '',
$article['content'] ?? '',
$article['author'] ?? '',
$article['category'] ?? '',
$article['tags'] ?? '',
$article['creationdate'] ?? ''
);
}
return $filteredArticles;
}
}