php While 循环在 While 循环内

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18930189/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 18:30:32  来源:igfitidea点击:

While Loop Inside a While Loop

phppostwhile-loopcomments

提问by Bongsky

I have a comment box wherein users can post comments. These comments will be displayed in a while loop.

我有一个评论框,用户可以在其中发表评论。这些注释将显示在一个 while 循环中。

<?php

while ($rowsx=mysql_fetch_array($resultss1)){

echo "<div id='comments'>";
echo "<table class='table11'>";
echo "<tr>";
echo "<td class='sss'><img src='img/user.jpg'>";
echo "<p class='p999'>"  .$rowsx['username'];  
echo  "</p> </br>";
echo "<p class='p9date'>" .$rowsx['date_posted']. "</p> ";
echo "</td>";
echo "<td>";
echo "<p class='p9'>" .$rowsx['comment']. "</p> </br></br></br>";


echo "</td>";

echo "</tr>";
echo "</table>";

echo "<a href='reply1.php?r_comment=".$rowsx['comment']."&prod_id=".$row['prod_id']."'    class='reply_button'> REPLY </a>  ";
echo "</div>";

}

?>

Now other users can also reply to the comments of the user. I want to display their replies below each comments of other users but I can't seem to figure out how to do that.

现在其他用户也可以回复用户的评论了。我想在其他用户的每条评论下方显示他们的回复,但我似乎无法弄清楚如何做到这一点。

Should I use another while loop inside the first while loop?

我应该在第一个 while 循环中使用另一个 while 循环吗?

Sorry if I'm using mysql functions I know it's depreciated but its just a school project.

抱歉,如果我正在使用 mysql 函数,我知道它已折旧,但它只是一个学校项目。

Here is my query for displaying comments:

这是我显示评论的查询:

$display_comments1="Select username,prod_id,comment,DATE_FORMAT(date_posted, '%m/%d/%Y %H:%i:%s' ) AS date_posted from comment where prod_id='$prod_id' order by date_posted DESC";
$resultss1=mysql_query($display_comments1);

if($resultss1 === FALSE) {
die(mysql_error()); // TODO: better error handling
}

回答by LondonAppDev

Yes you can put the while loop inside the while loop looking like this:

是的,您可以将 while 循环放入 while 循环中,如下所示:

<?php

while ($rowsx=mysql_fetch_array($resultss1)){

echo "<div id='comments'>";
echo "<table class='table11'>";
echo "<tr>";
echo "<td class='sss'><img src='img/user.jpg'>";
echo "<p class='p999'>"  .$rowsx['username'];  
echo  "</p> </br>";
echo "<p class='p9date'>" .$rowsx['date_posted']. "</p> ";
echo "</td>";
echo "<td>";
echo "<p class='p9'>" .$rowsx['comment']. "</p> </br></br></br>";


echo "</td>";

echo "</tr>";
echo "</table>";

echo "<a href='reply1.php?r_comment=".$rowsx['comment']."&prod_id=".$row['prod_id']."'    class='reply_button'> REPLY </a>  ";
echo "</div>";

   $commentId = $rowsx['id'];
   $replies = mysql_query('SELECT * FROM replies WHERE comment_id = $commentId');

   while ($rowsx=mysql_fetch_array($replies)){

   }

}

?>

回答by Shakti Patel

create this type code :

创建此类型代码:

<?php

while ($rowsx=mysql_fetch_array($resultss1)){

    print_comment($rowsx)

}

function print_comment($rowsx) {
    echo "<div id='comments'>";
    echo "<table class='table11'>";
    echo "<tr>";
    echo "<td class='sss'><img src='img/user.jpg'>";
    echo "<p class='p999'>"  .$rowsx['username'];
    echo  "</p> </br>";
    echo "<p class='p9date'>" .$rowsx['date_posted']. "</p> ";
    echo "</td>";
    echo "<td>";
    echo "<p class='p9'>" .$rowsx['comment']. "</p> </br></br></br>";


    echo "</td>";

    echo "</tr>";
    echo "</table>";

    echo "<a href='reply1.php?r_comment=".$rowsx['comment']."&prod_id=".$row['prod_id']."'    class='reply_button'> REPLY </a>  ";

    while ($sub_comment_result=mysql_fetch_array($reply)){

        print_comment($sub_comment_result)

    }
    echo "</div>";
}

?>