所以我想使用mvc arhitecture在php和mysql中创建一个小博客。我想添加一个可以在其中报告评论的功能(一切都进行得很好,但我不知道如何保留在同一页面上报告后)
用于显示带有帖子的页面以及该帖子的评论,我正在使用2个功能
{{project_root}}/lib/external_service/foo.rb
这是我的控制器:
public function getPost($postId)
{
$db = $this->dbConnect();
$req = $db->prepare('SELECT id, title, content, DATE_FORMAT(creation_date, \'%d/%m/%Y à %Hh%imin%ss\') AS creation_date_fr FROM posts WHERE id = ?');
$req->execute(array($postId));
$post = $req->fetch();
return $post;
}
public function getComments($postId)
{
$db = $this->dbConnect();
$comments = $db->prepare('SELECT id, author, comment,report_status, DATE_FORMAT(comment_date, \'%d/%m/%Y à %Hh%imin%ss\') AS comment_date_fr FROM comments WHERE post_id = ? ORDER BY comment_date DESC');
$comments->execute(array($postId));
return $comments;
}
我报告评论的功能是
function post(){
$postManager = new PostManager();
$commentManager = new CommentManager();
$post = $postManager->getPost($_GET['id']);
$comments = $commentManager->getComments($_GET['id']);
require('view/frontend/postView.php');
}
和控制器
public function reportComment($id)
{
$db = $this->dbConnect();
$report = $db->prepare('UPDATE comments SET report_status = 1 WHERE id = :id');
$report->bindParam(':id', $id, PDO::PARAM_INT);
$report->execute();
}
}
我的问题是我无法将用户重定向到post(id)页面,因为我已经将新的$ _GET设置为注释ID,而不是帖子ID;我如何存储旧的帖子ID,以便我可以停留在同一页面上...
这是我的index.php,因此您可以看到完整图片
function report(){
$commentManager = new CommentManager();
$commentManager->reportComment($_GET['id']);
答案 0 :(得分:0)
为什么不为$ _GET添加更多参数(例如:$ _GET('post_id'))
并在此代码中修复很少:
if(!empty($_GET['id'])&& $_GET['id'] > 0){
report();
$postId = $_GET('post_id');
///// redirect to post by $postId
}