我正在努力建立一个评论系统。
数据库结构
TABLE `posts` (
`post_id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`post_text` text NOT NULL,
`user_id` int(11) NOT NULL,
`post_creation` TIMESTAMP NOT NULL ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (`post_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
TABLE `comments` (
`comment_id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`comment_text` text NOT NULL,
`comment_creation` TIMESTAMP NOT NULL ON UPDATE CURRENT_TIMESTAMP,
`user_id` int(11) NOT NULL,
`post_id` int(11) unsigned NOT NULL,
PRIMARY KEY (`comment_id`),
FOREIGN KEY (`post_id`) REFERENCES posts(post_id) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
目标
我想只显示那些评论,其中评论的post_id等于帖子的post_id。
模型
/**
* Get all comments from a certain post
* @return array an array with several objects (the comments)
*/
public static function getComments($post_id)
{
$database = DatabaseFactory::getFactory()->getConnection();
$sql = "SELECT * FROM comments WHERE post_id = :post_id";
$query = $database->prepare($sql);
$query->execute(array(':post_id' => $post_id));
return $query->fetchAll();
}
控制器
public function index()
{
$this->View->render('index/index', array(
'posts' => PostModel::getAllPosts(),
'comments' => CommentModel::getComments($post_id)
));
}
视图
<!-- Display posts -->
<?php if ($this->posts) { ?>
<?php foreach($this->posts as $post) { ?>
<?= htmlentities($post->post_text); ?><br>
<?php } ?>
<!-- Comment section -->
<small>
<form method="post" action="<?php echo Config::get('URL');?>comment/create">
<input type="hidden" name="post_id" value="<?php echo htmlentities($post->post_id); ?>" />
<input type="text" name="comment_text" placeholder="comment..." />
<input type="submit" value='comment' autocomplete="off" />
</form><br>
<?php if ($this->comments) { ?>
<?php foreach($this->comments as $comment) { ?>
<?= htmlentities($comment->comment_text); ?><br>
<?php } ?>
<?php } ?>
</small><br><br>
<?php } ?>
<?php } else { ?>
<div>Get some friends to view posts.</div>
<?php } ?>
问题
$ post_id变量未定义。如何获取每个帖子的post_id以将它们与评论联系起来?
任何帮助将非常感谢!谢谢!
的更新
控制器
public function index()
{
$posts = PostModel::getAllPosts();
$comments = array();
foreach ($posts as $post) {
$comments[$post->post_id][] = CommentModel::getComments($post->post_id);
}
$this->View->render('index/index', array(
'posts' => $posts,
'comments' => $comments
));
}
视图
<?php foreach($this->comments[$post->post_id] as $comment) { ?>
<?= htmlentities($comment->comment_text); ?><br>
<?php if ($comment->user_id == Session::get('user_id')) { ?>
<small><a href="<?= Config::get('URL') . 'comment/delete/' . $comment->comment_id; ?>">Löschen</a></small><br>
<?php } ?>
<?php } ?>
错误消息
尝试在视图中获取非对象($ comment)的属性
var_dump的输出($ comments);在控制器的索引函数中(我有一个评论和2个帖子(post_id = 22和15)):
array(2) {
[22]=> array(1) {
[0]=> array(1) {
[0]=> object(stdClass)#8 (5) {
["comment_id"]=> string(1) "1"
["comment_text"]=> string(17) "this is a comment"
["comment_creation"]=> string(19) "2015-02-28 19:44:09"
["user_id"]=> string(1) "1"
["post_id"]=> string(2) "22" } } }
[15]=> array(1) { [0]=> array(0) { } } }
var_dump的输出($ comments);在视图中:
NULL
答案 0 :(得分:1)
尝试类似的东西:
public function index()
{
$posts = PostModel::getAllPosts();
$comments = array();
foreach ($posts as $post)
{
$comments[$post->post_id] = CommentModel::getComments($post->post_id);
}
$this->View->render('index/index', array(
'posts' => $posts,
'comments' => $comments;
));
}
然后在你看来,做一个
foreach($ this-&gt; comments [$ post-&gt; post_id] as $ comment)
以显示与帖子相关联的所有评论。