Stefany93 Posted December 26, 2012 Share Posted December 26, 2012 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 Link to comment https://forums.phpfreaks.com/topic/272379-displaying-comments-below-blog-posts/ Share on other sites More sharing options...
Barand Posted December 26, 2012 Share Posted December 26, 2012 Standard method is to check for a change in the blog, only outputting blog when you get a new one prev = ''; while fetch { if (prev != blog) { output blog set prev = blog } output comment } Link to comment https://forums.phpfreaks.com/topic/272379-displaying-comments-below-blog-posts/#findComment-1401357 Share on other sites More sharing options...
Stefany93 Posted January 2, 2013 Author Share Posted January 2, 2013 Thanks a bunch! Link to comment https://forums.phpfreaks.com/topic/272379-displaying-comments-below-blog-posts/#findComment-1402830 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.