使用下面的脚本根据帖子ID显示用户的帖子及其评论。
我遇到的问题是假设在一个id为1的帖子中,该帖子有大约69个帖子ID的评论。而不是显示属于该帖子ID的69条评论的脚本,它只会为每个帖子显示一条评论。有人可以帮我解决这个问题。 我还附上了显示帖子和评论的屏幕截图。它显示每个帖子的总评论,但每个帖子只显示一条评论文本/信息
下面是代码
include "config.php";
$data = json_decode(file_get_contents("php://input"));
$request = $data->request;
$userid = 5;
// Get all posts list and like unlike
if($request == 1){
$response_arr = array();
$query = "SELECT * FROM posts11";
$result = mysqli_query($con,$query);
while($row = mysqli_fetch_array($result)){
$postid = $row['id'];
$title = $row['title'];
$content = $row['content'];
$type = -1;
$comment_query = "SELECT COUNT(*) AS cntComment, username as usern,comment as p_com FROM post_comment WHERE type=1 and postid=".$postid;
$comment_result = mysqli_query($con,$comment_query);
$comment_row = mysqli_fetch_array($comment_result);
$total_comment = $comment_row['cntComment'];
$usern = $comment_row['usern'];
$pcom = $comment_row['p_com'];
$response_arr[] = array("id" => $postid, "title" => $title, "content" => $content, "type" => $type, "count_comment" => $total_comment,"usern"=>$usern,"pcom"=>$pcom);
}
echo json_encode($response_arr);
exit;
}
下面的是angularjs的前端
<!doctype html>
<html>
<head>
<link href="style.css" type="text/css" rel="stylesheet" />
</head>
<body ng-app='myapp'>
<div class="content" ng-controller='fetchCtrl' >
<h2>show posts</h2>
<div class="post" ng-repeat='post in posts'>
<h1 >{{ post.title }}</h1>
<div class="post-text">
{{ post.content }}
</div>
<div>
<h2>show comments</h2>
<b>Name:</b>{{ post.usern}} <b>comments</b>: {{ post.pcom}}<br>
({{ post.count_comment}} comments)
</div>
</div>
</div>
<!-- Script -->
<script src="angular.min.js"></script>
<script>
var fetch = angular.module('myapp', []);
fetch.controller('fetchCtrl', ['$scope', '$http', function ($scope, $http) {
// Fetch post data
$scope.getPosts = function(){
$http({
method: 'post',
url: 'likeunlike.php',
data: {request: 1}
}).then(function successCallback(response) {
$scope.posts = response.data;
});
}
$scope.getPosts(); // Fetch post data
}
]);
</script>
</body>
</html>
答案 0 :(得分:0)
您只在$comment_query
中执行查询后才获取第一个结果。也许您忘了将获取放入循环(类似于第一个循环)。
此外,由于我们将使用内部循环,我们应该使用嵌套数组,以便每个注释都不会重复帖子及其内容。
这段代码应如下所示:
while($row = mysqli_fetch_array($result)) {
$postid = $row['id'];
$title = $row['title'];
$content = $row['content'];
$type = -1;
$post_arr = array(
"id" => $postid,
"title" => $title,
"content" => $content,
"comments" => array()
);
$comment_query = "SELECT username as usern,comment as p_com FROM post_comment WHERE type=1 and postid=".$postid;
$comment_result = mysqli_query($con,$comment_query);
while ($comment_row = mysqli_fetch_array($comment_result)) {
$total_comment = $comment_row['cntComment'];
$usern = $comment_row['usern'];
$pcom = $comment_row['p_com'];
$post_arr["comments"][] = array(
"type" => $type,
"usern" => $usern,
"pcom" => $pcom
);
}
}
此外,您还需要修改前端代码才能使用ng-repeat
。可以找到示例here。请注意,由于我们可以简单地计算前端上的注释数量,因此我们从后端PHP代码中删除了SELECT COUNT(*)
。
<div ng-repeat="comment in post.comments">
<b>Name:</b>{{ comment.usern }}
<b>comments</b>: {{ comment.pcom }}<br>
</div>
({{ post.comments.length }} comments)