繁体   English   中英

我如何从带有评论的数据库中获取新闻列表

[英]How can i get list of news from database with comments

我要制作适用于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

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

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

暂无
暂无

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

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