douyi1966 2015-11-02 06:23
浏览 37

使用PHP显示mysql的输出(json编码)

I'm trying to code a page like facebook-view_posts_page where I need to show the result as POST 1..Comment 1.. Comment 2.. POST 2.. Comment 3

The output of my code is

POST 1
POST 2
comment 1
comment 2
comment 3

How should I re write my code?

<?php
include("connect.php");

$userID=$_REQUEST['userID'];

$Query=("select * from tb_post where userID='$userID'");
$result=mysql_query($Query);
$count=mysql_num_rows($result);


if($count>0)
{
//$post['result']="sucess";

$joinQuery=("select * from tb_post where tb_post.userID='$userID'");
$joinResult=mysql_query($joinQuery);


    while($row=mysql_fetch_assoc($joinResult))
    {

        $posts[]=$row;


            $postid=$row['postID'];
            $commentQuery=("select tb_comment.commentID,tb_comment.userID ,tb_comment.postID ,tb_comment.comment ,tb_comment.date,signup.userName,signup.image from tb_comment,signup where tb_comment.postID='$postid' and signup.userID=tb_comment.userID");
            $commentResult=mysql_query($commentQuery);
                //$post['posts']=$posts;

                while($commentrow=mysql_fetch_assoc($commentResult))
                {

                $comments[]=$commentrow;


                }
    }
    $post=array("result"=>"success","posts"=>$posts,"comments"=>$comments);

}
else
{
    $post['result']="failed";
    $post['error']="no data found";
}
$data='content-type:application/json';
$data=json_encode($post);
echo $data;


?>
  • 写回答

2条回答 默认 最新

  • dongpo4197 2015-11-02 06:43
    关注

    I will explain you the approach.

    You are fetching and appending comments in an array without any direct key reference to the post.

    array comments;
    while (comment = fetch comments) {
      comments[post id][] = comment;
    }
    

    while showing:

    while (post = fetch posts) {
      echo post title;
      foreach (comments[post id] as postComment) {
        echo postComment;
      }
    }
    

    Comments should have reference to the post as key of comments array.

    In your case, you are already fetching comments correctly, just change the following inner while loop to:

    while($commentrow=mysql_fetch_assoc($commentResult)) {
      $comments[$commentrow['postID'][]=$commentrow; // Observe postID
    }
    

    Fully working code:

    <?php
    include("connect.php");
    $userID=$_REQUEST['userID'];
    $Query=("select * from tb_post where userID='$userID'");
    $result=mysql_query($Query);
    $count=mysql_num_rows($result);
    if($count>0) {
      $joinQuery=("select * from tb_post where tb_post.userID='$userID'");
      $joinResult=mysql_query($joinQuery);
      while($row=mysql_fetch_assoc($joinResult)) {
        $posts[]=$row;
        $postid=$row['postID'];
        $commentQuery=("select tb_comment.commentID,tb_comment.userID ,tb_comment.postID ,
          tb_comment.comment ,tb_comment.date,signup.userName,signup.image 
          from tb_comment,signup 
          where tb_comment.postID='$postid' and signup.userID=tb_comment.userID");
        $commentResult=mysql_query($commentQuery);
        while($commentrow=mysql_fetch_assoc($commentResult)) {
          $comments[$commentrow['postID']][] = $commentrow;
        }
      }
      $post=array("result"=>"success","posts"=>$posts,"comments"=>$comments);
    }
    else {
      $post['result']="failed";
      $post['error']="no data found";
    }
    $data='content-type:application/json';
    $data=json_encode($post);
    echo $data;
    ?>
    

    You can print posts and its comments like this:

    <?php
    if (! empty($posts)) {
      foreach ($posts as $post) {
        echo $post['post_title_field'] . "<br/>";
        if (! empty($comments[$post['postID']])) {
          foreach ($comments[$post['postID']] as $postComment) {
            echo postComment . "<br/>";
          }
        }
      }
    }
    ?>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 Mac系统vs code使用phpstudy如何配置debug来调试php
  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)