search-Methode angepasst
This commit is contained in:
@@ -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;
|
||||
|
||||
|
||||
@@ -313,34 +313,50 @@ class DatabaseArticleManager implements ArticleManagerDAO {
|
||||
return [];
|
||||
}
|
||||
|
||||
$query = "SELECT id, title, content, author, category, tags, creationdate
|
||||
try {
|
||||
$db = $this->getConnection();
|
||||
|
||||
$sql = "SELECT id, title, content, author, category, tags, created
|
||||
FROM articles
|
||||
WHERE LOWER(title) LIKE :keyword
|
||||
OR LOWER(content) LIKE :keyword";
|
||||
WHERE title LIKE :keyword
|
||||
OR content LIKE :keyword;";
|
||||
|
||||
$stmt = $this->db->prepare($query);
|
||||
$command = $db->prepare($sql);
|
||||
if (!$command) {
|
||||
throw new InternalServerErrorException("internal_error");
|
||||
}
|
||||
|
||||
// Wildcards für die LIKE-Suche hinzufügen und Keyword in Kleinbuchstaben umwandeln
|
||||
$searchParam = '%' . strtolower($cleankeyword) . '%';
|
||||
$stmt->bindValue(':keyword', $searchParam, PDO::PARAM_STR);
|
||||
// Wildcards für die Suche hinzufügen
|
||||
$searchParam = '%' . $cleankeyword . '%';
|
||||
|
||||
$stmt->execute();
|
||||
$articles = $stmt->fetchAll(PDO::FETCH_ASSOC);
|
||||
$success = $command->execute([
|
||||
":keyword" => $searchParam
|
||||
]);
|
||||
|
||||
if (!$success) {
|
||||
throw new InternalServerErrorException("internal_error");
|
||||
}
|
||||
|
||||
$rows = $command->fetchAll(PDO::FETCH_ASSOC);
|
||||
$filteredArticles = [];
|
||||
foreach ($articles as $article) {
|
||||
|
||||
foreach ($rows as $row) {
|
||||
$filteredArticles[] = new Article(
|
||||
intval($article['id'] ?? 0),
|
||||
$article['title'] ?? '',
|
||||
$article['content'] ?? '',
|
||||
$article['author'] ?? '',
|
||||
$article['category'] ?? '',
|
||||
$article['tags'] ?? '',
|
||||
$article['creationdate'] ?? ''
|
||||
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");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user