Reputation: 4904
I am trying to build a comment system.
The database structure
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;
The Goal
I would like to display just those comments, where the post_id of the comment is equal to the posts' post_id.
The Model
/**
* 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();
}
The Controller
public function index()
{
$this->View->render('index/index', array(
'posts' => PostModel::getAllPosts(),
'comments' => CommentModel::getComments($post_id)
));
}
The View
<!-- 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 } ?>
The problem
The $post_id variable is undefined. How can I get the post_id of every post to link them with the comments?
Any help would be highly appreciated! Thank you!
UPDATE
The Controller
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
));
}
The View
<?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 } ?>
Error Message
Trying to get property of non-object ($comment) in the View
The output for var_dump($comments); in the index function in the controller (I have one comment and 2 posts (post_id= 22 and 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) { } } }
The output for var_dump($comments); in the view:
NULL
Upvotes: 0
Views: 1115
Reputation: 1170
Try something like that :
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;
));
}
Then in your view, just do a
foreach($this->comments[$post->post_id] as $comment)
in order to display all comments associated to a post.
Upvotes: 1