search-Methode angepasst

This commit is contained in:
2026-06-10 20:42:32 +02:00
parent a19d1db990
commit a5131e33dc
2 changed files with 40 additions and 23 deletions
+1
View File
@@ -80,6 +80,7 @@ interface ArticleManagerDAO
* (Unabhängig von Groß-und Kleinschreibung)
* @param string $keyword Der eingegebene Suchbegriff.
* @return array Ein Array von Artikeln ,die dem Suchkriterium entsprechen. Wenn nichts gefunden wird, ein leeres Array.
* @throws InternalServerErrorException
*/
public function search(string $keyword): array;
+39 -23
View File
@@ -313,34 +313,50 @@ class DatabaseArticleManager implements ArticleManagerDAO {
return [];
}
$query = "SELECT id, title, content, author, category, tags, creationdate
FROM articles
WHERE LOWER(title) LIKE :keyword
OR LOWER(content) LIKE :keyword";
try {
$db = $this->getConnection();
$stmt = $this->db->prepare($query);
$sql = "SELECT id, title, content, author, category, tags, created
FROM articles
WHERE title LIKE :keyword
OR content LIKE :keyword;";
// Wildcards für die LIKE-Suche hinzufügen und Keyword in Kleinbuchstaben umwandeln
$searchParam = '%' . strtolower($cleankeyword) . '%';
$stmt->bindValue(':keyword', $searchParam, PDO::PARAM_STR);
$command = $db->prepare($sql);
if (!$command) {
throw new InternalServerErrorException("internal_error");
}
$stmt->execute();
$articles = $stmt->fetchAll(PDO::FETCH_ASSOC);
// Wildcards für die Suche hinzufügen
$searchParam = '%' . $cleankeyword . '%';
$filteredArticles = [];
foreach ($articles as $article) {
$filteredArticles[] = new Article(
intval($article['id'] ?? 0),
$article['title'] ?? '',
$article['content'] ?? '',
$article['author'] ?? '',
$article['category'] ?? '',
$article['tags'] ?? '',
$article['creationdate'] ?? ''
);
$success = $command->execute([
":keyword" => $searchParam
]);
if (!$success) {
throw new InternalServerErrorException("internal_error");
}
$rows = $command->fetchAll(PDO::FETCH_ASSOC);
$filteredArticles = [];
foreach ($rows as $row) {
$filteredArticles[] = new Article(
intval($row['id']),
$row['title'] ?? '',
$row['content'] ?? '',
$row['author'] ?? '',
$row['category'] ?? '',
$row['tags'] ?? '',
$row['created'] ?? '' // Nutzt 'created' aus deiner DB-Struktur
);
}
return $filteredArticles;
} catch (PDOException $e) {
throw new InternalServerErrorException("internal_error");
}
return $filteredArticles;
}
}