如何通过评论从数据库中获取新闻列表

时间:2015-07-06 14:50:47

标签: php android json

我要为Android制作新闻应用。我把json_encode写到我的php文件中。但我只想在其中添加评论。我到处搜索它,但我找不到。你能帮我么。

这是我的PHP代码

if (!empty($result)) {

        // check for empty result
        if ($result->num_rows > 0) {

            $response["success"] = true;

            $response["news"] = array();
            while($row = $result->fetch_object()){
            $news = array();
            $news["id"] = $row->id;
            $news["title"] = $row->title;
            $news["details"] = $row->short;
            $news["thumbURL"] = $thumb.$row->images;
            $news["LargeImageURL"] = $big.$row->images;

            array_push($response["news"], $news);
            }

            // echoing JSON response
            echo json_encode($response);
        } else {
            // no news found
            $response["success"] = false;
            $response["message"] = "No news found";

            // echo no users JSON
            echo json_encode($response);
        }
    } else {
        // no news found
        $response["success"] = false;
        $response["message"] = "No news found";

        // echo no users JSON
        echo json_encode($response);
    }

我希望在照片中添加项目评论

  

http://i.stack.imgur.com/RnaOJ.jpg

1 个答案:

答案 0 :(得分:0)

我发现它的工作很好。谢谢你的帮助。

if (!empty($result)) {

        // check for empty result
        if ($result->num_rows > 0) {

            $response["success"] = true;

            $response["news"] = array();
            while($row = $result->fetch_object()){
            $commentsql = $db->query("SELECT * FROM comment where cid='".$row->id."' order by id desc limit 0,10");
            $comments = array();
            while($comm = $commentsql->fetch_object()){
            $comments[] = array(
                'id'    => $comm->id,
                'text'  => $comm->comment
                );
            }
            $news = array();
            $news["id"] = $row->id;
            $news["title"] = $row->title;
            $news["details"] = $row->short;
            $news["thumbURL"] = $thumb.$row->images;
            $news["LargeImageURL"] = $big.$row->images;
            $news["comment"] = $comments;

            array_push($response["news"], $news);
            }

            // echoing JSON response
            echo json_encode($response);
        } else {
            // no news found
            $response["success"] = false;
            $response["message"] = "No news found";

            // echo no users JSON
            echo json_encode($response);
        }
    } else {
        // no news found
        $response["success"] = false;
        $response["message"] = "No news found";

        // echo no users JSON
        echo json_encode($response);
    }