Jump to content

Displaying Comments Below Blog Posts


Stefany93

Recommended Posts

Hi fellows,

 

I am building up a blog application and I am stuck. I am trying to make the appropriate comments display below a single blog post in a single query.

The problem is that if the number of comments is more than 1, the blog post is being repeated as well which is not what I want. I want only 1 blog post to be displayed on a single page and all the comments that associate with the post be displayed as well.

 

Here is the code of the query

 


[size=5]$blog_post = (isset($_GET['blog_post'])) ? filter_input(INPUT_GET, 'blog_post', FILTER_SANITIZE_NUMBER_INT) : 0;
$query = $db->prepare("SELECT posts.post_id, posts.title, posts.contents, DATE_FORMAT(posts.date_posted, '%d %M %Y') AS date_posted,
comments.comments_name, comments.comment, DATE_FORMAT(comments.date_commented, '%d %M %Y') AS date_commented
FROM posts
LEFT JOIN comments
ON comments.post_id = posts.post_id
WHERE posts.post_id = :blog_post
OR posts.post_id = (SELECT MAX(posts.post_id) FROM posts)");
$query->bindParam(':blog_post', $blog_post, PDO::PARAM_INT);
$query->execute();[/size]

 

And here is part of the HTML page where I am trying to make things work

 



[size=5]<article class="post">
<div class="post-data">
<?php while(list($post_id, $title, $contents, $date_posted, $comments_name, $comment, $date_commented) = $query->fetch(PDO::FETCH_NUM)){ ?>
<h2><?php echo $title;?></h2>
<p> <?php echo $date_posted;?></p>

</div>
<p>
<?php echo nl2br($contents);?>
</p>

<hr></hr>
<!-- Blog comments -->
</article>

<article class="post">
<div class="title">
<h3>
<?php echo '<strong>',$comments_name,'</strong>'; ?>
</h3>

</div>
<p>
<?php echo $date_commented; ?>
</p>
<p>
<?php echo $comment; ?>
</p>
<?php }?>
</article>[/size]

 

 

Thank you very much!

EDIT - Sorry but somehow the font size of the code posted here is very small for some reason...

Best Regards

Stefany

Edited by Stefany93
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.