试图让论坛显示类别为

时间:2015-05-10 16:09:36

标签: php html css explode forums

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

但是,正如您所看到的,这些类别都表示"社区",这不应该发生。 "测试"子类别应该在"公告&更新与#34;子类别。

我知道问题所在,但我不知道如何解决它。

    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?

1 个答案:

答案 0 :(得分: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");
    }
}