search-Methode in DatabaseArticleManager
This commit is contained in:
@@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user