如何从mySQL DB输出每个帖子下的完整评论列表?

时间:2012-03-29 04:56:58

标签: php mysql wordpress

我一直致力于从WordPress博客中提取数据库内容,以显示在外部网站上。基本上,我目前在我的网站上有一个页面,它从我的WordPress数据库中抓取所有博客帖子,如博客标题,日期和博客内容,并按降序显示。

我现在想表明我赞同每篇文章的评论。我在很长一段时间内没有做太多编程,所以也许我在这里遗漏了一些基本概念。我列出了我在php开头运行的select查询,以从表wp_postswp_comments中获取所需的内容。

$query = "SELECT * FROM wp_comments
          LEFT OUTER JOIN wp_posts 
          ON wp_comments.comment_post_ID = wp_posts.ID
          WHERE
            comment_approved = '1' AND
            comment_type = ''
          ORDER BY post_date DESC
          LIMIT 10";

$query_result = mysql_query($query);
$num_rows = mysql_numrows($query_result);

然后我使用for循环,如下所示。

<?php  

//start a loop that starts $i at 0, and make increase until it's at the number of rows  
for($i=0; $i< $num_rows; $i++){   

    //assign data to variables, $i is the row number, which increases with each run of the loop  
    $blog_permalink = mysql_result($query_result, $i, "post_name");
    $blog_date = mysql_result($query_result, $i, "post_date");  
    $blog_title = mysql_result($query_result, $i, "post_title");  
    $blog_content = mysql_result($query_result, $i, "post_content");  
    $blog_permalink = mysql_result($query_result, $i, "post_name");
    $blog_comment = mysql_result($query_result, $i, "comment_content");
    $blog_comment_author = mysql_result($query_result, $i, "comment_author");
    $blog_comment_date = mysql_result($query_result, $i, "comment_date");

    //format date  
    $blog_date = strtotime($blog_date);  
    $blog_date = strftime("%Y-%m-%d", $blog_date);  

    //the following HTML content will be generated on the page as many times as the loop runs.   
?>  

</body>  
<div class="post"></div>  

       <span class="date"> <b><?php echo $blog_date; ?>:</b></code></span><br /><hr />   

        <b><?php echo $blog_title; ?></b> <br /><br />

        <?php echo $blog_content; ?> <br /> <br />

        <b><span>Comments</span></b> <br />
        <?php echo $blog_comment_date; ?> <br />
        <?php echo $blog_comment_author; ?> <br />  
        <?php echo $blog_comment; ?> <br /><br />  

我的代码目前正在显示带有评论的博文。但是,不是在单个帖子下显示所有5条评论,而是在博文中显示5次,并在该帖子的每个实例中使用不同的评论。

我在这个for循环中尝试了一些条件if语句,用于在单个帖子下显示所有注释但是不成功。理想情况下,我想创建一个条件,如果post_title表的comment_count大于零,则显示comment_content表中该帖子下的所有注释。任何反馈或有用的提示将不胜感激。

1 个答案:

答案 0 :(得分:0)

我认为应该有两个查询来限制数据获取的数量,在第一个查询中我们应该找到最后十个帖子有注释,然后在第二个获取注释中通过它的父ID并将其添加到数组,这里是示例:< / p>

$posts = array();

$SQL = "SELECT p.ID AS post_id, p.post_date, p.post_title, Count(c.comment_ID) AS total
FROM wp_posts AS p
INNER JOIN wp_comments AS c ON p.ID = c.comment_post_ID
WHERE c.comment_approved = 1 AND c.comment_type = ''
GROUP BY p.ID, p.post_date, p.post_title
HAVING total > 0
ORDER BY p.post_date DESC
LIMIT 10";

$result = mysql_query($SQL);

if(mysql_numrows($result))
{
    while($row = mysql_fetch_assoc($result))
    {
        //create empty array for comments
        $row['comments'] = array();

        $posts[$row['post_id']] = $row;
    }
}

mysql_free_result($result);

//check if there is any posts with comments
if($posts)
{
    //prepare post ids for IN statement
    $ids = implode(', ', array_keys($posts));

    $SQL2 = "SELECT c.* 
    FROM wp_comments AS c
    WHERE c.comment_approved = 1 AND c.comment_type = '' AND c.comment_post_ID IN ({$ids})";

    $result = mysql_query($SQL2);

    if (mysql_numrows($result))
    {
        while ($row = mysql_fetch_assoc($result))
        {
            //assign comments to posts
            $posts[$row['comment_post_id']]['comments'][] = $row;
        }
    }

    mysql_free_result($result);
}