如何打印打印帖子,评论和逐个回复如何在PHP中使用循环


how to print printing post,comment and reply one by one how to use loop for that in php?

我有三个表

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

我不知道如何利用循环来打印此类数据

你需要类似的东西....

$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++;
}

你也可以使用某种递归函数。您提供的查询中的数据需要循环到数组中,如上例所示。