Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/285.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用angularjs php显示每篇文章的所有评论时出现问题_Php_Angularjs - Fatal编程技术网

使用angularjs php显示每篇文章的所有评论时出现问题

使用angularjs php显示每篇文章的所有评论时出现问题,php,angularjs,Php,Angularjs,我使用下面的脚本根据帖子id唯一地显示用户的帖子及其评论 我遇到的问题是,假设在id为1的帖子中,有大约69条带有帖子id的评论。与其使用脚本来显示属于该帖子id的69条评论,不如只为每个帖子显示一条评论。有人能帮我修一下吗我还附上了显示帖子和评论的截图。它显示每篇文章的总评论,但每篇文章只显示一条评论文本/信息 下面是代码 include "config.php"; $data = json_decode(file_get_contents("php://input")); $reques

我使用下面的脚本根据帖子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>

发帖
{{post.title}}
{{post.content}}
显示评论
名称:{post.usern}}注释:{{post.pcom}}
({post.count_comment}}评论) var fetch=angular.module('myapp',[]); controller('fetchCtrl',['$scope','$http',函数($scope,$http){ //获取post数据 $scope.getPosts=函数(){ $http({ 方法:“post”, url:'likeunlike.php', 数据:{请求:1} }).then(函数成功回调(响应){ $scope.posts=response.data; }); } $scope.getPosts();//获取post数据 } ]);

您仅在执行
$comment\u query
中的查询后获取第一个结果。也许您忘记将fetch放入循环(类似于第一个循环)

此外,由于我们将使用一个内部循环,所以我们应该使用一个嵌套数组,这样post及其内容就不会在每个注释中重复

这段代码应该如下所示:

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
。可以找到一些例子。请注意,由于我们可以简单地计算前端的注释数量,因此我们从后端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)

名称:{comment.usern}
注释:{{comment.pcom}}
({post.comments.length}}comments)
我已经实现了您的解决方案,但它仍然会在每篇文章中显示一条评论。似乎循环post记录的查询与循环注释的查询不能正常工作。无论如何,我们可以以不同的方式循环帖子和评论,然后在回显之前加入它们。您需要修改角度前端,以循环浏览结果。我已经有一段时间没有用过这个了。让我看看我是否能为你把代码拿出来。我已经编辑了上面的答案,以反映我认为需要更改的内容