循环结束后的回声,而不是循环时回声


echo after loop finishes rather than while looping

我正在编写自己的论坛(出于学习原因),并试图找到以下解决方案:

有论坛帖子存储在数据库中,当我想显示这些帖子时,我首先将mySQL中的每个帖子放入一个数组($posts[][]),然后遍历这些数组以将它们输出到html页面上:

if (count($posts) > 0) {
    for ($x = 0; $x < count($posts); $x++) {
        echo '
        <div class="post-header">' . $posts[$x][3] . '<text style="float:right">#' . ($x+1) . '</text></div>
        <div class="post">
            <div class="post-user ">';
            if(login_check($mysqli) && (permission_check($mysqli) == 2 || permission_check($mysqli) == 3 || $username == $posts[$x][5])) {
                echo '<a href="posting.php?topicID='. $topicID . '&forumID=' . $forumID . '&postID=' . $posts[$x][1] . '&mode=edit">Edit</a><br>';
            }
            echo '<a href="../profile/?profile="' . $posts[$x][5] . '">' . $posts[$x][5] . '</a>
            <br>
            </div>
            <div class="post-text">' . $bbcode->parse($posts[$x][4]) . '</div>
        </div><br>';
    }
}

虽然这运行良好,但我希望回声仅在循环完成后显示。目前,这会实时回响,每个论坛帖子都会添加到 html 中,直到循环完成,看起来加载速度非常慢。

回答您的问题:

            if (count($posts) > 0) {
                $sexy = '';
                for ($x = 0; $x < count($posts); $x++) {
                    $sexy .='
                    <div class="post-header">' . $posts[$x][3] . '<text style="float:right">#' . ($x+1) . '</text></div>
                    <div class="post">
                        <div class="post-user ">';
                        if(login_check($mysqli) && (permission_check($mysqli) == 2 || permission_check($mysqli) == 3 || $username == $posts[$x][5])) {
                            $sexy .='<a href="posting.php?topicID='. $topicID . '&forumID=' . $forumID . '&postID=' . $posts[$x][1] . '&mode=edit">Edit</a><br>';
                        }
                        $sexy .='<a href="../profile/?profile="' . $posts[$x][5] . '">' . $posts[$x][5] . '</a>
                        <br>
                        </div>
                        <div class="post-text">' . $bbcode->parse($posts[$x][4]) . '</div>
                    </div><br>';
                }
                echo $sexy;
            }

这将在循环结束时回显。但是,如果您认识到加载缓慢,请对结果进行"分页",例如每页仅显示 50 个并在底部显示一些导航。另一种增加责任并在到达后显示所有内容的可能性,并带有同花顺:http://php.net/manual/de/function.flush.php<</p>

div class="answers">

请尝试这个

$responseString = '';
if (count($posts) > 0) {
    for ($x = 0; $x < count($posts); $x++) {
        $responseString .= '
        <div class="post-header">' . $posts[$x][3] . '<text style="float:right">#' . ($x+1) . '</text></div>
        <div class="post">
            <div class="post-user ">';
            if(login_check($mysqli) && (permission_check($mysqli) == 2 || permission_check($mysqli) == 3 || $username == $posts[$x][5])) {
                $responseString .= '<a href="posting.php?topicID='. $topicID . '&forumID=' . $forumID . '&postID=' . $posts[$x][1] . '&mode=edit">Edit</a><br>';
            }
            $responseString .= '<a href="../profile/?profile="' . $posts[$x][5] . '">' . $posts[$x][5] . '</a>
            <br>
            </div>
            <div class="post-text">' . $bbcode->parse($posts[$x][4]) . '</div>
        </div><br>';
    }
}
echo $responseString;