简体   繁体   English

论坛帖子显示加载缓慢

[英]forum posts display loading slowly

I am new to php coding and am coding a website (with forums) to learn. 我是php编码的新手,并且正在编码一个网站(带有论坛)进行学习。

Currently I have one dynamic page that handles all the forum aspects from displaying the topics, all the forums and then each post itself. 目前,我有一个动态页面,可处理所有论坛方面的问题,包括显示主题,所有论坛以及每个帖子本身。

Each of these topics/forums and posts are held in mySQL and accessed when needed. 这些主题/论坛和帖子均保存在mySQL中,并在需要时进行访问。

the first two sections (Display all topics and Display all forums) loads perfectly fine however when we get to the third section (Display all posts for the forum) the loading is very slow. 前两个部分(显示所有主题和显示所有论坛)的加载非常好,但是当我们进入第三部分(显示该论坛的所有帖子)时,加载速度很慢。 My test page only has 16 posts and very little text however takes while to load. 我的测试页只有16个帖子,几乎没有文本,但是加载需要花费一些时间。

I have paging so each page only holds 25 posts however it is slow at 16 with little text so hard to image 25 posts with loads of texts/images. 我有分页,所以每个页面仅包含25个帖子,但是由于很少的文字,它的速度很慢,只有16个,因此很难对25个带有大量文本/图像的帖子进行图像显示。

Thanks for any help and suggestions in advance :). 感谢您的任何帮助和建议提前:)。

Here is a reduced code from the forums page (Only showing the necessary parts): 这是论坛页面上的简化代码(仅显示必要的部分):

<?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);

Within the forumsData.php I access the database and put the data into arrays depending on the TopicID and forumID. 在forumsData.php中,我访问数据库并将数据放入数组中,具体取决于TopicID和forumID。 eg: 例如:

    $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++
    }

Question partially resolved by checking permissions once outside the loop, here is the part of code: 通过在循环外检查权限来部分解决问题,这是代码的一部分:

            // 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>';
            }

Use microtime(true) to get some timings. 使用microtime(true)以获得一些计时。 Determine which of these is the problem: (1) SELECT ( mysqli_query call), (2) looping through the rows, (3) building the page, or (4) stuff elsewhere. 确定是哪个问题:(1) SELECTmysqli_query调用),(2)遍历行,(3)构建页面,或(4)其他地方。

If it is the SELECT , then you possibly need this composite index (in either order): 如果是SELECT ,则可能需要此复合索引(以任何顺序):

INDEX(topicidcol, forumidcol)

Also, you should tack on and ORDER BY and a LIMIT 25 , even if there are only 16 (since you don't know there are only 16). 另外,即使只有16个,也应该加上ORDER BYLIMIT 25 (因为您不知道只有16个)。

25 rows should not be a problem. 25行应该没问题。 The 100th page of 25 posts will be a problem, but you will come back later to ask why. 第100页(共25篇帖子)将是一个问题,但是您稍后会回来问为什么。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM