Can someone correct me my code? The error is " Fatal error: Cannot redeclare Comment::getComment() " if the error is in this file, can you show it to me or correct it? Otherwise tell me how I can fix this Thanks you.
<?php
require_once(__DIR__.'/../model/article_model.php');
require_once(__DIR__.'/../model/comments_model.php');
require_once(__DIR__.'/../model/espaceadmin_model.php');
class CommentsController {
public static function commentsPage () {
if(isset($_POST['deletecomments'])) {
$deletecomments= Comment::getComment((int) $_POST['post_id']);
$deletecomments->deletecomment();
}
$template = file_get_contents(__DIR__.'/../view/backend/template-admin.html');
$view = file_get_contents(__DIR__.'/../view/backend/commentsadmin_view.html');
$loop_content = file_get_contents(__DIR__.'/../view/backend/commentsadmin_articles.html');
$articles = Article::getArticles();
$articleListe = "";
foreach ($articles as $article) {
$articleListe.= str_replace('{ARTICLE_TITLE}', $article->getTitre(), $loop_content);
$articleListe = str_replace('{ARTICLE_DATE}', $article->getDate(), $articleListe);
$articleListe = str_replace('{ARTICLE_CONTENT}', $article->getContenu(), $articleListe);
$comments = Comment::getComments($article->getId());
foreach ($comments as $comment) {
$articleListe = str_replace('{COMMENTS_AUTHOR}', $comment->getAuthor(), $articleListe);
$articleListe = str_replace('{COMMENTS_DATE}', $comment->getComment_Date(), $articleListe);
$articleListe = str_replace('{COMMENTS_COMMENT}',$comment->getComment(), $articleListe);
$articleListe = str_replace('{COMMENT_POSTID}', $comment->getPost_id(), $articleListe);
}
}
$view = str_replace('{BOUCLE}', $articleListe, $view);
$template = str_replace('{CONTENT}', $view, $template);
$template = str_replace('{TITLE}', "Index", $template);
print($template);
}
}
CommentsController::commentsPage();
edit: here is the other file, how can I remove this error? I would like to keep both functions
<?php
class Comment {
private $id = null;
private $post_id;
private $author;
private $comment;
private $comment_date;
public function getId(){
return $this->id;
}
public function getPost_id(){
return $this->post_id;
}
public function setPost_id($post_id){
$this->post_id = $post_id;
}
public function getAuthor(){
return $this->author;
}
public function setAuthor($author){
$this->author = $author;
}
public function getComment(){
return $this->comment;
}
public function setComment($comment){
$this->comment = $comment;
}
public function getComment_date(){
return $this->comment_date;
}
public function setComment_date($comment_date){
$this->comment_date = $comment_date;
}
public function persist(){
if($this->id==null) {
$this->insert();
} else {
$this->update();
}
}
private function insert(){
$bdd = Database::getDb();
$query = "INSERT INTO comments(post_id, author, comment, comment_date) VALUES(?,?,?,?)";
$stmt = $bdd->prepare($query);
$stmt->execute([
$this->post_id,
$this->author,
$this->comment,
$this->comment_date
]);
$this->id=$bdd->lastInsertId();
return true;
}
/* private function update() {
$bdd = Database::getDb();
$query = "UPDATE comments SET comment = :comment WHERE id = :id";
UPDATE ... WHERE id = ?
} */
public static function getComments($id_post) {
$bdd = Database::getDb();
$reponse = $bdd->query('SELECT * FROM comments WHERE post_id = "'.$id_post.'"');
$result= array();
while ($donnees = $reponse->fetch())
{
$currentArticle= new Comment();
$currentArticle->id = $donnees['id'];
$currentArticle->setPost_id($donnees['post_id']);
$currentArticle->setAuthor($donnees['author']);
$currentArticle->setComment($donnees['comment']);
$currentArticle->setComment_date($donnees['comment_date']);
array_push($result, $currentArticle);
}
return $result;
}
public static function getComment(int $id_post) {
$bdd = Database::getDb();
$reponse = $bdd->query('SELECT * FROM comments WHERE post_id = "'.$id_post.'"');
while ($donnees = $reponse->fetch())
{
$currentArticle= new Comment();
$currentArticle->id = $donnees['id'];
$currentArticle->setPost_id($donnees['post_id']);
$currentArticle->setAuthor($donnees['author']);
$currentArticle->setComment($donnees['comment']);
$currentArticle->setComment_date($donnees['comment_date']);
return $currentArticle;
}
return null;
}
public function deletecomment() {
$bdd = Database::getDb();
$bdd->query('DELETE FROM comments WHERE post_id = "'.(int)$id_post.'"');
}
}
?>