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

标签 php mysql wordpress

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

我现在想显示我批准的每个帖子的评论。我已经很长时间没有做过太多编程了,所以我可能在这里遗漏了一些基本概念。我列出了我在 php 开头运行的选择查询,以从表 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 表中该帖子下的所有评论.非常感谢任何反馈或有用的提示。

最佳答案

我相信应该有两个查询来限制要获取的数据量,在第一个查询中我们应该找到最后十个有评论的帖子,在第二个查询中通过它的父 ID 获取评论并将其添加到数组中,这里是示例:

$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);
}

关于php - 如何从 mySQL DB 输出每个帖子下的完整评论列表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9919277/

相关文章:

php - fatal error : Call to a member function fetch_assoc() on string

php - jquery 和 php + 如果 session 已死,则返回错误

javascript - Node.js - 在继续之前从 MySQL 获取返回值

mysql - `from_unixtime` 能否减慢此查询速度?

php - 如何检查哪些优惠券适用于 WooCommerce 中的哪些产品?

jquery - 使用样式(url)查找将 css 添加到类

javascript - 从 select 创建项目列表/数组

php - 创建具有多个属性的节点

MySQL 单行中有多个键

html - 标题右填充不能按预期工作