Php 循环结束后回音,而不是循环时回音
我正在编写自己的论坛代码(出于学习原因),并试图找到以下问题的解决方案: 我将论坛帖子存储在数据库中,当我想显示这些帖子时,我首先将mySQL中的每个帖子放入一个数组($posts[][]),然后循环通过这些数组将它们输出到htmlpage:Php 循环结束后回音,而不是循环时回音,php,loops,mysqli,echo,forum,Php,Loops,Mysqli,Echo,Forum,我正在编写自己的论坛代码(出于学习原因),并试图找到以下问题的解决方案: 我将论坛帖子存储在数据库中,当我想显示这些帖子时,我首先将mySQL中的每个帖子放入一个数组($posts[][]),然后循环通过这些数组将它们输出到htmlpage: if (count($posts) > 0) { for ($x = 0; $x < count($posts); $x++) { echo ' <div class="post-header"&g
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>';
}
}
if(计数($posts)>0){
对于($x=0;$x”;
}
回声'
“.$bbcode->parse($posts[$x][4])”
",;
}
}
虽然这运行得很好,但我希望回声仅在循环完成后显示。目前,这会实时回响,每个论坛帖子都会被添加到html中,直到循环完成,看起来加载速度非常慢。请尝试此方法
$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;
$responseString='';
如果(计数($posts)>0){
对于($x=0;$x';
}
$responseString.='
“.$bbcode->parse($posts[$x][4])”
",;
}
}
回音$responseString;
请试试这个
$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;
$responseString='';
如果(计数($posts)>0){
对于($x=0;$x';
}
$responseString.='
“.$bbcode->parse($posts[$x][4])”
",;
}
}
回音$responseString;
要回答您的问题:
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;
}
if(计数($posts)>0){
$sexy='';
对于($x=0;$x';
}
$性感。='
“.$bbcode->parse($posts[$x][4])”
",;
}
性感的回声;
}
这将在循环结束时回响。但是,如果您发现加载速度慢,请选择“分页”您的结果,例如,每页仅显示50个,并在底部显示一些导航。另一种可能是增加责任感,并在它到来时展示一切,用flush:回答您的问题:
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;
}
if(计数($posts)>0){
$sexy='';
对于($x=0;$x';
}
$性感。='
“.$bbcode->parse($posts[$x][4])”
",;
}
性感的回声;
}
这将在循环结束时回响。但是,如果您发现加载速度慢,请选择“分页”您的结果,例如,每页仅显示50个,并在底部显示一些导航。另一种可能是增加责任,并在它到达时显示所有内容,使用flush:为什么不将其存储在var中?当循环结束时,你说的“看起来加载速度很慢”是什么意思?当涉及到速度时,如果你在环路内或环路外回音并不重要。您仍然在回显相同数量的数据,并且循环中的迭代次数相同。为什么不将其存储在var中呢?当循环结束时,你说的“看起来加载速度很慢”是什么意思?当涉及到速度时,如果你在环路内或环路外回音并不重要。您仍在回显相同数量的数据,并且循环中的迭代次数相同。您只需创建一个变量,而不是使用回显,您只需将值分配给该变量,并在整个循环中不断追加。循环完成后。你可以重复同样的回答,这完美地回答了我的问题,@magnus Eriksson说,虽然这会一直显示信息到最后(谢谢!),但我仍然感觉加载速度很慢。我只写了16篇文章,几乎没有文字。我是否应该在页面加载之前将所有帖子添加到一个数组中,然后在请求时显示所需内容?很抱歉问你这些问题!您只需要创建一个变量,而不是使用echo,您只需将值赋给该变量并在整个循环中不断追加即可。循环完成后。你可以重复同样的回答,这完美地回答了我的问题,@magnus Eriksson说,虽然这会一直显示信息到最后(谢谢!),但我仍然感觉加载速度很慢。我只写了16篇文章,几乎没有文字。嘘