我的查询会产生两个结果,例如
然后我尝试使用带有两个while循环的两个数组拉取结果
while ($rowPost = mysqli_fetch_assoc($grabPostsQuery)) {
$posts[] = $rowPost;
}
while ($rowComment = mysqli_fetch_assoc($grabCommentsQuery)) {
$comments[] = $rowComment;
}
$grabPostsQuery = "SELECT * FROM posts ORDER BY postDate DESC";
$grabPostsQuery = mysqli_query($link, $grabPostsQuery);
$grabCommentsQuery = "SELECT * FROM comments ORDER BY commentDate DESC";
$grabCommentsQuery = mysqli_query($link, $grabCommentsQuery);
然后我像下面这样制作了一个嵌套的foreach
foreach($posts as $post) {
foreach($comments as $comment) {
echo $post['postContent']."<br>";
echo $comment['commentContent']."<br>";
}
}
我可以访问帖子和评论,唯一的问题是foreach循环通过帖子和评论多次循环,然后显示两次
请帮助吗?
答案 0 :(得分:0)
只运行一个循环,而不是一个循环...
foreach($posts as $post) {
echo $post['postContent']."<br>";
}
foreach($comments as $comment) {
echo $comment['commentContent']."<br>";
}