PHP + MySQL类别未显示为应该

时间:2012-08-06 00:12:15

标签: php mysql treeview forum

我不清楚为什么我的脚本的子类别“论坛”没有被浏览到相应的类别。相反,子类别强制脚本重复类别代码。

如下图所示,有一个类别Test,该类别包括Test和Bugs。然而,Bugs滑入第二个表,它不应该是两个测试类别都是一个,而是看起来像数据库中有两个类别叫做Test。我的代码中有什么错误,Bugs被推入拷贝类别“测试”而不是正确的测试 - 只是一个简单的测试论坛?

enter image description here

这是代码。

<?php

include 'connect.php';
include 'header.php';

$sql = "SELECT
            categories.cat_id,
            categories.cat_name,
            forums.forum_id,
            forums.forum_cat,
            forums.forum_name,
            forums.forum_desc,
            COUNT(forums.forum_id) AS forums
        FROM
            categories
        LEFT JOIN
            forums
        ON
            forums.forum_cat = categories.cat_id
        GROUP BY
            forums.forum_name, forums.forum_desc, forums.forum_id
        ORDER BY
            categories.cat_id ASC
        ";

$result = mysql_query($sql);

if(!$result)
{
    echo 'The categories could not be displayed, please try again later.';
}
else
{
    if(mysql_num_rows($result) == 0)
    {
        echo 'No categories defined yet.';
    }
    else
    {
        //prepare the table         
        while($row = mysql_fetch_assoc($result))
        {   

        echo '<table border="1">
              <tr>
                <th>' . $row['cat_name'] . '</th><th></th>
                </tr>';     
            echo '<tr>';

                echo '<td class="leftpart">';
                    echo '<h3><a href="viewforum.php?id=' . $row['forum_id'] . '">' . $row['forum_name'] . '</a></h3>' . $row['forum_desc'];
                echo '</td>';
                echo '<td class="rightpart">';

                //fetch last topic for each cat
                    $topicsql = "SELECT
                                    topic_id,
                                    topic_subject,
                                    topic_date,
                                    topic_cat
                                FROM
                                    topics
                                WHERE
                                    topic_cat = " . $row['forum_id'] . "
                                ORDER BY
                                    topic_date
                                DESC
                                LIMIT
                                    1";

                    $topicsresult = mysql_query($topicsql);

                    if(!$topicsresult)
                    {
                        echo 'Last topic could not be displayed.';
                    }
                    else
                    {
                        if(mysql_num_rows($topicsresult) == 0)
                        {
                            echo 'no topics';
                        }
                        else
                        {
                            while($topicrow = mysql_fetch_array($topicsresult))
                            echo '<a href="viewtopic.php?id=' . $topicrow['topic_id'] . '">' . $topicrow['topic_subject'] . '</a> at ' . date('d-m-Y', strtotime($topicrow['topic_date']));
                        }
                    }
                echo '</td>';
            echo '</tr>';
            echo '</br>';
        }
    }
}

include 'footer.php';
?>

表格结构

分类

cat_id     | int(8) | primary | auto incr
cat_name   | var(255)

|2|Damm
|3|Hmm
|1|Test

论坛

forum_id   | int(8) | primary | auto_incr
forum_cat  | int(8) <-- forum cat "category" is just ID of category it belongs to
forum_name | var(255)
forum_desc | var(255)

|1|1|Test|Just a simple forum test
|2|3|Lol
|3|1|Bugs|Bugs and related go here

主题

topic_id   | int(8) | primary | auto_incr
topic_subject | var(255)
topic_date    | datetime
topic_cat     | int(8) 
topic_by      | int(8)

|1|Test|2012-07-31 23:12:47|1|1

我个人尝试了不同的事情,但仍然无法弄清楚为什么会这样。

1 个答案:

答案 0 :(得分:1)

您将获得

的结果
cat_id 1, forum_id 1
cat_id 1, forum_id 2
cat_id 1, forum_id 3
cat_id 2, forum_id 4

但是你在一个循环中输出所有内容,所以你需要做的是记住显示的最后一个标题,如果你在一个新的类别中,只显示一个新标题。

以下是一个例子,并不完美,但会给你一个开始

// Start the table here - outside the loop
echo '<table border="1">  

// Here's how you track if you need the header
$lastCatID = -1

// Now loop
while($row = mysql_fetch_assoc($result))  
{     
    // Only show cat header on condition
    if ($lastCatID <> $row['cat_id']) {
        echo '
          <tr>  
            <th>' . $row['cat_name'] . '</th><th></th>  
            </tr>';

        // Note that you've shows this header so you don't show it again.
        $lastCatID = $row['cat_id'];
    }

    // Now output the rest       
    echo '<tr>';  
    echo '<td class="leftpart">';  
        etc....
    echo '</tr>';  
}

// Now close the table now you're all done looping.
echo '</table>';

希望有所帮助。你应该能够从那里扩展以适应你的风格。