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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/70.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
如何打印打印文章、评论和回复,如何在php中使用循环?_Php_Mysql - Fatal编程技术网

如何打印打印文章、评论和回复,如何在php中使用循环?

如何打印打印文章、评论和回复,如何在php中使用循环?,php,mysql,Php,Mysql,我有三张桌子 post post_id | user_id | post_data | post_date 1 | 2 | feeling not good | xxxxxxxxx 2 | 6 | i am at xyz location | xxxxxxxxx 3 | 78 | I need some help frirnds

我有三张桌子

post

post_id | user_id |   post_data                   |  post_date
1       |   2     |  feeling not good             | xxxxxxxxx 
2       |   6     |  i am at xyz location         | xxxxxxxxx
3       |   78    |  I need some help frirnds     |  xxxxxxxxx
4       |   9     |  going to watch cricket match |  xxxxxxxxx
5       |   1     |  need some help               |  xxxxxxxxx


post_comment

comment_id | user_id    post_id     comment                            comment_date   
   1       |    2            1         what happned dude??                  xxxxxxxxx 
   2       |    6            1         whats wrong with you??               xxxxxxxxx
   3       |    78           1         we will go for movie                xxxxxxxxx
   4       |    80           3         how may i help you


post_reply   
reply_id       user_id      comment_id      reply                         | reply_date

   1                5             1             he has a problem          |  xxxxxxx
   2                40            4             call him he will tell you.| 
   3                45            1              you dont know....        |
我已经编写了获取记录的查询

select p.post_id,p.post_data,p.post_date,c.comment,c.comment_date,c.comment_id,r.reply,r.reply_date from post as p left join post_comment as c on c.post_id=p.post_id left join post_reply as r on r.comment_id=c.comment_id
我正在尝试按照以下格式打印数据

post no 1          
           comment no 1 

                       reply no 1
                       reply no 2

           comment no 2        

           comment no 3

 post no 2
          if(no comment then)         

  post no 3 

  and so on

我不知道如何使用loop来打印这样的数据

你需要像

$count = 0;
While(isset(p.post_id[$count]))
{
   Echo $p.post_data[$count];
   $i = 0;
   While($i < sizeof(c.comment_id))
   {
        If(c.post_id[$i] == p.post_id[$count])
        {
            Echo $c.comment[$i];
            $j = 0;
            While($i < sizeof(c.comment_id))
            {
                 If(r.comment_id[$j] == c.comment_id[$i])
                 {
                      Echo $r.reply[$j];
                 }
                  j++;
         }
         $i++;
    }

    $count++;
}

也可以使用某种递归函数。您提供的查询中的数据需要像上面的示例一样循环到数组中。

$result=mysql\u query$query;虽然$row=mysql\u fetch\u assoc$result{}我不知道解决方案如何排序??当我使用while循环时