php - 无法根据 SQL 输出创建单独的论坛类别

标签 php mysql forum webbbs

我遇到了一些问题,我真的不确定我做错了什么。我想做的是创建一个专有论坛,我可以将其用于我的客户和我自己的项目。我同意尝试重新发明轮子而不是简单地拆散 phpBB 是很愚蠢的,但这也是我从错误中吸取教训的好方法。

问题 问题在于 PHP 脚本循环了类别。例如,我们在 SQL 中有 3 个类别。 cookies 、蛋糕和咖啡现在我们有 2 个论坛,每个论坛都可以在适当的论坛中表示喜欢和不喜欢,它们看起来就是这样的

Cookie

  • 喜欢
  • 不喜欢

蛋糕

  • 喜欢
  • 不喜欢

等等。然而在我看来,它看起来像这样:

Cookie

  • 喜欢

Cookie

  • 不喜欢

代码 好的,这里是 StackOverflow 人员的代码,可以用来 rip appart 并向我解释我做错了什么,我可能错过了什么,或者我应该做什么才能使其按原样运行打算这样做。

<?php
//create_cat.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,
            forums.forum_admin,
            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>';

                if ($_SESSION['user_level'] != 9 AND $row['forum_admin'] == 1) { 

                echo "<div style='padding:8px;background-color:#fae7af;'>Sorry but you this is for Admins only.</div><br>";

                } else {

                echo '<td class="leftpart">';
                    echo '<h3><a href="viewforum.php?f=' . $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?t=' . $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';
?>

SQL

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

|2|Test 2
|3|Test 3
|1|Test 1


**Forum**
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|More Test | Just a 2nd test forum
|3|1|Bugs|Bugs and related go here

here is what i mean by categories being looped

就是这样。

最佳答案

在我看来,每个类别( cookies 、蛋糕等)都有两个论坛(喜欢、不喜欢)。我没看错吗?

如果这是真的,您应该执行 OUTER JOIN 而不是 LEFT JOIN 来生成综合列表,如下所示:

   ...
        FROM categories
        JOIN forums
    GROUP BY forums.forum_name, forums.forum_desc, forums.forum_id
    ORDER BY categories.cat_id, forums.forum_id

请注意,ON 子句已消失。这意味着您将把每个论坛行连接到每个类别行。

另请注意,如果您希望保证论坛在每个类别下以相同的顺序显示,则需要按类别和论坛进行排序。

如果每个类别都没有出现所有论坛,而是每个类别都有您的论坛的子集,则您向我们展示的架构无法胜任这项工作。论坛和类别表之间存在多对多关系。为了完成这项工作,您将需要另一个表,如下所示:

      forums_categories
        cat_id int(8) not null
        forum_id int(8) not null
        PRIMARY KEY (cat_id, forum_id)

对于每个允许的论坛和类别组合,该表需要一行。您的原因变为:

   ...
        FROM forums_categories fc
        JOIN categories ON (fc.cat_id = categories.cat_id)
        JOIN forums     ON (fc.forum_id = forums.forum_id)
    GROUP BY forums.forum_name, forums.forum_desc, forums.forum_id
    ORDER BY categories.cat_id, forums.forum_id

希望这有帮助。

关于php - 无法根据 SQL 输出创建单独的论坛类别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13240168/

相关文章:

php 内容从未被浏览器缓存?

MySQL 查询 : Getting the most recent forum post

MySQL 查询有助于在表之间移动数据

database - 如果实现 hellbanning,什么是有效的方法?

php - Xcode- swift -NSURL : "fatal error: unexpectedly found nil while unwrapping an Optional value"

javascript - 当 PHP 内联到 html(或其他 Web 文件)中时会发生什么?

php - Netbeans - 导入命名空间的快捷方式

mysql - MySQL 写入是否受表大小影响?

php - 以两种方式对连接表进行排序搜索

mysql - 按限制更新订单给我一个错误