使用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;


?>

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/>";
      }
    }
  }
}
?>

Thanks everyone for helping me.My friend came up with another solution, which is the code I'm looking for.And here is the code:

<?php
    include("connect.php");
    $sel_post=mysql_query("SELECT * FROM tb_post WHERE userID='".$_REQUEST['userID']."'");
    if(mysql_num_rows($sel_post)>0)
    {
        while($row=mysql_fetch_assoc($sel_post))
        {
            $sel_post_owner=mysql_query("SELECT name,image FROM signup WHERE userID='".$row['userID']."'");
            if(mysql_num_rows($sel_post_owner)>0)
            {
                while($row_owner=mysql_fetch_array($sel_post_owner))
                {
                    $row['post_owner_name']=$row_owner['name'];
                    $row['post_owner_image']=$row_owner['image'];
                }
            }
            else
            {
                    $row['post_owner_name']="";
                    $row['post_owner_image']="";
            }
            $sel_comments=mysql_query("SELECT * FROM tb_comment WHERE postID='".$row['postID']."'");
            if(mysql_num_rows($sel_comments)>0)
            {
                $comments=array();
                while($row_comment=mysql_fetch_assoc($sel_comments))
                {
                    $sel_comment_owner=mysql_query("SELECT name,image FROM signup WHERE userID='".$row_comment['userID']."'");
                    if(mysql_num_rows($sel_post_owner)>0)
                    {
                        while($row_comment_owner=mysql_fetch_array($sel_comment_owner))
                        {
                            $row_comment['comment_owner_name']=$row_comment_owner['name'];
                            $row_comment['comment_owner_image']=$row_comment_owner['image'];
                        }
                    }
                    else
                    {
                            $row_comment['comment_owner_name']="";
                            $row_comment['comment_owner_image']="";
                    }
                    $comments[]=$row_comment;
                }
                $row['comments']=$comments;
            }
            else
            {
                $row['comments']=array();
            }

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

    ?>