4

这是我的论坛目前的样子: http: //prntscr.com/73oicl

但是,正如您所看到的,这些类别都说“社区”,这是不应该发生的。“测试”子类别应位于“公告和更新”子类别下。

我知道问题是什么,但我不知道如何解决它。

    function getForums($id) {
    $currentHost = "http://$_SERVER[HTTP_HOST]";
    $query = "SELECT * FROM forums, categories";
    try {
        global $db;
        $stmt = $db->prepare($query); 
        $stmt->execute();
        $result = $stmt->fetchAll();
        foreach($result as $row) {
            $category_title = $row['category_title'];
            $forum_id = $row['forum_id'];
            $forum_title = $row['forum_title'];
            $forum_topic_count = $row['forum_topic_count'];
            $forum_post_count = $row['forum_post_count'];
            $forum_last_topic_id = $row['forum_last_topic_id'];
            $forum_last_topic = $row['forum_last_topic'];
            $forum_last_date = $row['forum_last_date'];
            $forum_last_user = $row['forum_last_user'];
            $fixed_last_topic = substr($forum_last_topic,0,25).'...';
            echo '<div class="forum pleft">
                    <div class="forum-header">
                        <span class="header-text">'.$category_title.'</span>
                    </div>
                    <table>
                        <tr>
                            <td class="title"><a href="'.$currentHost.'/forums/view-forum/index.php?cid='.$category_id.'&fid='.$forum_id.'">'.$forum_title.'</a></td>
                            <td class="topics">'.$forum_topic_count.'</td>
                            <td class="posts">'.$forum_post_count.'</td>
                            <td class="lastpost"><a href="'.$currentHost.'/forums/view-thread/index.php?cid='.$id.'&fid='.$forum_id.'&tid='.forum_last_topic_id.'">'.$fixed_last_topic.'</a> by <a href="'.$currentHost.'/users/index.php?username='.$forum_last_user.'">'.$forum_last_user.'</a> at '.$forum_last_date.'</td>
                        </tr>
                    </table>
                </div>';
        }
    }
    catch(PDOException $ex) {
        die("error");
    }
}

如您所见,对于每个结果,它都会创建一个全新的论坛 div,这意味着为该类别提供的所有子类别不会在一起,每个子类别都会创建一个新论坛,我不想要。有没有一种方法可以让回声爆炸,所以如果 2 个或更多子类别属于 1 个类别,它就不会推出新的 div?

4

1 回答 1

0

我没有测试就写了这个,所以你可能想检查错误。这可能不是最有效的方法,但它应该有效。

function getForums($id) {
    $currentHost = "http://$_SERVER[HTTP_HOST]";
    $query = "SELECT * FROM forums, categories ORDER BY categories ASC"; /* Order by categories to arrange same categories together */
    try {
        global $db;
        $stmt = $db->prepare($query); 
        $stmt->execute();
        $result = $stmt->fetchAll();
        $numrows = $stmt->fetch(PDO::FETCH_NUM); /* Check for total rows of records */

        $count = 0;
        $currentCategory = ""; // Declare container

        foreach($result as $row) {
            $category_title = $row['category_title'];
            $forum_id = $row['forum_id'];
            $forum_title = $row['forum_title'];
            $forum_topic_count = $row['forum_topic_count'];
            $forum_post_count = $row['forum_post_count'];
            $forum_last_topic_id = $row['forum_last_topic_id'];
            $forum_last_topic = $row['forum_last_topic'];
            $forum_last_date = $row['forum_last_date'];
            $forum_last_user = $row['forum_last_user'];
            $fixed_last_topic = substr($forum_last_topic,0,25).'...';

            /* If currentCat is empty OR is not similar to previous */
            if($currentCategory == "" || $currentCategory != $category_title){
                /* If currentcat is not empty AND not similar to previous, close */
                if($currentCategory != "" && $currentCategory != $category_title){
                    echo '</table></div>';
                }

                echo '<div class="forum pleft">
                    <div class="forum-header">
                        <span class="header-text">'.$category_title.'</span>
                    </div>
                    <table>
                        <tr>
                            <td class="title"><a href="'.$currentHost.'/forums/view-forum/index.php?cid='.$category_id.'&fid='.$forum_id.'">'.$forum_title.'</a></td>
                            <td class="topics">'.$forum_topic_count.'</td>
                            <td class="posts">'.$forum_post_count.'</td>
                            <td class="lastpost"><a href="'.$currentHost.'/forums/view-thread/index.php?cid='.$id.'&fid='.$forum_id.'&tid='.forum_last_topic_id.'">'.$fixed_last_topic.'</a> by <a href="'.$currentHost.'/users/index.php?username='.$forum_last_user.'">'.$forum_last_user.'</a> at '.$forum_last_date.'</td>
                        </tr>';
                $count++;

                /* Check if its last row, If yes, end */
                if($count == $numrows){
                    echo '</table></div>';
                }

                $currentCategory = $category_title; /*Set category title */
             }
             /* If next row belong to the same category, If yes, continue */
             else if($currentCategory == $category_title){
                echo '<tr>
                        <td class="title"><a href="'.$currentHost.'/forums/view-forum/index.php?cid='.$category_id.'&fid='.$forum_id.'">'.$forum_title.'</a></td>
                        <td class="topics">'.$forum_topic_count.'</td>
                        <td class="posts">'.$forum_post_count.'</td>
                        <td class="lastpost"><a href="'.$currentHost.'/forums/view-thread/index.php?cid='.$id.'&fid='.$forum_id.'&tid='.forum_last_topic_id.'">'.$fixed_last_topic.'</a> by <a href="'.$currentHost.'/users/index.php?username='.$forum_last_user.'">'.$forum_last_user.'</a> at '.$forum_last_date.'</td>
                    </tr>';
                $count++;

                /* Check if its last row, If yes, end */
                if($count == $numrows){
                    echo '</table></div>';
                }
            }
        }
    }
    catch(PDOException $ex) {
        die("error");
    }
}
于 2015-05-13T23:53:30.497 回答