gpt4 book ai didi

php - 嵌套的 while 循环,注释相互嵌套

转载 作者:行者123 更新时间:2023-11-30 00:15:39 25 4
gpt4 key购买 nike

我想获取帖子下的每条评论。如何正确嵌套 while 循环?

我现在的结果是这样的。

*Johannes: Johannes post nummer 1 2014-05-14 16:57:58 27

Victoria comments johannes post<br/>
Johannes comments Victorias post<br/>
Micke comments Mickes post<br/>
Christer comments Mickes post<br/>

但我想要这样的:

*Johannes: Johannes post nummer 1 2014-05-14 16:57:58 27<br/>
Victoria comments johannes post

*Victoria: Victorias post nummer 1 2014-05-14 16:57:58 28<br/>
Johannes comments Victorias post

我的代码如下所示:

$commentsql = "SELECT comment,comment_post, post, post_id<br/>
FROM comments,posts<br/>
WHERE comment_post = post_id";<br/>

$result_sql = mysql_query($commentsql);<br/>
while($row = mysql_fetch_array($result)) {
$commentid = $row['post_id'];
echo '<li>' .$row['username'] .': ' . $row['post'] . ' ' . $row['date'] . ' ' . $row['post_id'] . '</li>' . '<br/>';

while($comment = mysql_fetch_array($result_sql)) {
echo $comment['comment'] . '<br/>' ;
}
}

最佳答案

您将需要使用 2 个单独的查询来实现此结果(或一些右连接魔术作为高级选项):

$post_resource = mysql_query("SELECT post_id, post FROM posts");
while($row = mysql_fetch_assoc($post_resource)) {
echo $row['post'] . "\n";
$comments_resource = mysql_query("SELECT comment FROM comments WHERE comment_post=$row[post_id]");
while($comment = mysql_fetch_assoc($comment_resource)) {
echo $comment['comment'] ."\n";
}
echo "\n\n";
}

关于php - 嵌套的 while 循环,注释相互嵌套,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23681572/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com