繁体   English   中英

如何在php中为每个帖子获得总评论?

[英]how to get total comment in php for each post?

我有一个带有注释名称的表。 从这里我想显示每个帖子的总评论。 我试过了,但它显示了所有帖子的总评论。 但我只想对每个帖子发表评论

function commeNT(){
        global $conn;
         $sql = "SELECT COUNT(`post_id`) as `totalComment` FROM `comments` WHERE post_id = `post_id`";
            $result = $conn->query($sql);                    
            if(mysqli_num_rows($result) > 0){
            while($comm= mysqli_fetch_array($result)){                  
            echo $comm['totalComment'];
           }
        }
    }

我有一个带有注释名称的表。 从这里我想显示每个帖子的总评论。 我试过了,但它显示了所有帖子的总评论。 但我只想对每个帖子发表评论。

//This will get you a list of all posts and total comments for each.
function all_post_comments() {
    global $conn;
    $sql = "SELECT COUNT(`post_id`) as `totalComment`, `post_id` FROM `comments` GROUP BY `post_id`";
    $result = $conn->query($sql);
    if ($result->num_rows > 0) {
        while ($comm = $result->fetch_array()) {
            echo $comm['post_id'] . ' Total Comments = ' . $comm['totalComment'];
        }
    }
}

//This will get you total comments for a specific post.  You have to pass post id when calling the function.
function comment_count($postId) {
    global $conn;
    $sql = "SELECT COUNT(`post_id`) as `totalComment` FROM `comments` WHERE `post_id` = $postId";
    $result = $conn->query($sql);
    if ($result->num_rows > 0) {
        $comm = $result->fetch_array();
        echo $postId . ' Total Comments = ' . $comm['totalComment'];
    } else {
        echo "No Post Found with that id";
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM