我是php编码的新手,正在编写一个网站(有论坛)来学习。
目前我有一个动态页面可以处理所有论坛方面,包括显示主题,所有论坛以及每个帖子本身。
这些主题/论坛和帖子中的每一个都保存在mySQL中,并在需要时进行访问。
前两个部分(显示所有主题和显示所有论坛)加载完全正常但是当我们到达第三部分(显示论坛的所有帖子)时,加载非常慢。我的测试页面只有16个帖子,但加载时只需要很少的文本。
我有分页,所以每个页面只有25个帖子,但是它很慢,只有很少的文字,很难用25个帖子加载文本/图像。
提前感谢您的任何帮助和建议:)。
以下是论坛页面中的简化代码(仅显示必要部分):
<?php
$topicID = $_GET['topicID'];
$forumID = $_GET['forumID'];
include_once '../includes/forumsData.php';
sec_session_start();
$username = $_SESSION['username'];
include_once 'header.php';
include_once 'navbar.php';
?>
<div class="bg">
<!-- Body -->
<div class='body'>
<div class="forum-title">Forums</div>
<?php
// No topic chosen
// Display all topics
if(!isset($topicID)) {
for ($x = 0; $x < count($headers); $x++) {
echo '<div class="forum-header">' . $headers[$x][0] . '</div>';
for ($z = 0; $z < count($topics); $z++) {
if($topics[$z][6] == $headers[$x][1]) {
echo '<div class="forums">
<div class="topics">
<a href="?topicID=' . $topics[$z][7] . '">' . $topics[$z][0] . '</a></font>
<br>
' . $topics[$z][1] . '
</div>';
if ($topics[$z][3] == ""){
echo '<div class="info">No new Posts</div>';
}
else {
echo '<div class="info"><a href = "../profile/?profile=' . $topics[$z][4] . '"><img src="../images/profileA/' . $topics[$z][2] . '" height="45" width="45" /></a><p><a href="?topicID=' . $topics[$z][7] . '&forumID=' . $topics[$z][8] . '">' . $topics[$z][3] . '</a><br><a href="../profile/?profile=' . $topics[$z][4] . '">' . $topics[$z][4] . '</a> - ' . get_date_diff($mysqli, $topics[$z][5]) . '</p></div>';
}
echo '</div>';
}
}
}
}
// Topic has been chosen
// Display all the forum posts
if(isset($topicID) && check_topic_exists($mysqli, $topicID) && empty($forumID)) {
echo '<br><a href="./">Forums</a> > <a href="./?topicID='. $topicID . '">' . get_topic_name($mysqli, $topicID) . '</a><br><br>
<a href="posting.php?topicID='. $topicID . '&mode=post">Create New Topic</a><br>';
echo '<div class="forum-topicHeader">Topic</div>
<div class="forum-repliesHeader">Replies</div>
<div class="forum-lpHeader">Latest post</div>';
if (count($forums) > 0) {
for ($x = 0; $x < count($forums); $x++) {
$replies = $forums[$x][6] -1;
echo '
<div class="forums">
<div class="topics">
<a href="?topicID=' . $topicID . '&forumID=' . $forums[$x][0] . '">' . $forums[$x][2] . '</a><br><a href="../profile/?profile=' . $forums[$x][4] . '">' . $forums[$x][4] . '</a></td>
</div>
<div class="replies">
' . $replies . '
</div>
<div class="info">
<a href="../profile/?profile=' . get_latest_user_reply($mysqli, $topicID, $forums[$x][0]) . '">' . get_latest_user_reply($mysqli, $topicID, $forums[$x][0]) . '</a><br> ' . get_date_diff($mysqli, $forums[$x][5]) . '
</div>
</div>';
}
}
}
// Topic has been chosen
// Forum has been chosen
// Display all posts for the forum
if(isset($topicID) && check_topic_exists($mysqli, $topicID) && isset($forumID)) {
require '../BBCode/BBCodeParser.php';
$bbcode = new Golonka\BBCode\BBCodeParser;
echo '<br><a href="./">Forums</a> > <a href="./?topicID='. $topicID . '">' . get_topic_name($mysqli, $topicID) . '</a> > <a href="./?topicID='. $topicID . '&forumID=' . $forumID . '">' . get_forum_name($mysqli, $topicID, $forumID) . '</a>';
echo '<br><br>
<a href="posting.php?topicID='. $topicID . '&forumID=' . $forumID . '&mode=post">Post New Reply</a>
<div class="post-title ">' . get_forum_name($mysqli, $topicID, $forumID) . '</div><br>';
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>
<a href="#">Add img here soon</a>
<br>
Other user infos?
</div>
<div class="post-text">' . $bbcode->parse($posts[$x][4]) . '</div>
</div><br>';
}
}
echo '<a href="posting.php?topicID='. $topicID . '&forumID=' . $forumID . '&mode=post">Post New Reply</a><br><br>';
}
// If we have selected a topic that doesn't exist
if(!empty($topicID) && !check_topic_exists($mysqli, $topicID))
{
include_once 'topicnotfound.php';
}
?>
</div>
</div>
</body>
</html>
<?php
mysqli_close($mysqli);
在forumsData.php中,我访问数据库并根据TopicID和forumID将数据放入数组中。 e.g:
$nquery = 'SELECT * FROM tableexample WHERE topicidcol = ' . $topicID . ' AND forumidcol = ' . $forumID;
$ndata = mysqli_query($mysqli, $nquery);
while($row = mysqli_fetch_array($ndata))
{
// insert date into array
$myarray[$z] = array($row['datacol1'], $row['datacol2']);
$z++
}
答案 0 :(得分:2)
通过在循环外部检查权限来部分解决问题,这是代码的一部分:
// Topic has been chosen
// Forum has been chosen
// Display all posts for the forum
if(isset($topicID) && check_topic_exists($mysqli, $topicID) && isset($forumID)) {
require '../BBCode/BBCodeParser.php';
$bbcode = new Golonka\BBCode\BBCodeParser;
echo '<br><a href="./">Forums</a> > <a href="./?topicID='. $topicID . '">' . get_topic_name($mysqli, $topicID) . '</a> > <a href="./?topicID='. $topicID . '&forumID=' . $forumID . '">' . get_forum_name($mysqli, $topicID, $forumID) . '</a>';
echo '<br><br>
<a href="posting.php?topicID='. $topicID . '&forumID=' . $forumID . '&mode=post">Post New Reply</a>
<div class="post-title ">' . get_forum_name($mysqli, $topicID, $forumID) . '</div><br>';
if (count($posts) > 0) {
// checking user login & privileges
$isLogged = login_check($mysqli);
$hasPrivileges = (permission_check($mysqli) == 2 || permission_check($mysqli) == 3);
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 ">';
// changed condition, avoiding frequent mysql request
if($isLogged && ($hasPrivileges || $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>
<a href="#">Add img here soon</a>
<br>
Other user infos?
</div>
<div class="post-text">' . $bbcode->parse($posts[$x][4]) . '</div>
</div><br>';
}
}
echo '<a href="posting.php?topicID='. $topicID . '&forumID=' . $forumID . '&mode=post">Post New Reply</a><br><br>';
}
答案 1 :(得分:0)
使用microtime(true)
获取一些时间。确定这些问题中的哪一个:(1)SELECT
(mysqli_query
调用),(2)循环遍历行,(3)构建页面,或(4)其他地方。
如果它是SELECT
,那么你可能需要这个复合索引(按任意顺序):
INDEX(topicidcol, forumidcol)
另外,你应该坚持ORDER BY
和LIMIT 25
,即使只有16个(因为你不知道只有16个)。
25行应该不是问题。 25篇文章的第100页将是一个问题,但您稍后会回来询问原因。