自定义MySQL& PHP论坛 - 列出类别

时间:2017-05-17 17:12:03

标签: php html mysql

我正在开发一个自定义的MySQL& PHP论坛,我试图让我的主题在各自的类别下坐下/列出。当我在管理面板中创建主题时,我可以设置类别ID,它与类别的ID相对应。我已经在网上和网站上浏览了很多。

我已经能够显示类别并且能够在主题所在的位置,但主题没有显示,我传递的参数保持导致null,并且原因每个类别都表明没有创建主题。

这是我正在处理的功能:

function fetch_forum() {
global $connection;

$query = "SELECT * FROM forum_categories LEFT JOIN forum_topics ON forum_topics.forum_topic_category_id = forum_categories.forum_category_id";
$select_forum_data = mysqli_query($connection, $query);
$new_topic = "";
$pre_topic = "";

while($row = mysqli_fetch_assoc($select_forum_data)) {
    $forum_topic_id = $row['forum_topic_id'];
    $forum_topic_title = $row['forum_topic_title'];
    $forum_topic_description = $row['forum_topic_description'];
    $forum_topic_category_id = $row['forum_topic_category_id'];
    $forum_category_id = $row['forum_category_id'];
    $forum_category_title = $row['forum_category_title'];

    $new_topic = $forum_topic_title;

    if($forum_category_title != null) {
        echo "<thead>";
            echo "<tr>";
                echo "<th><span class='glyphicon glyphicon-file' aria-hidden='true'></span> {$forum_category_title}</th>";
                echo "<th>Topics</th>";
                echo "<th>Posts</th>";
                echo "<th>Last Post</th>";
            echo "</tr>";
        echo "</thead>";
    }

    if($new_topic != $pre_topic) {
        echo "<tbody>";
            echo "<tr>";
                echo "<td><a href='topic.php?t_id=<?php echo $forum_topic_id; ?>'><h5> {$forum_topic_title}</h5></a></td>";
                echo "<td>1</td>";
                echo "<td>1</td>";
                echo "<td>03-28-2017</td>";
            echo "</tr>";
        echo "</tbody>";
    } else {
        echo "<tbody>";
            echo "<tr>";
                echo "<td>No topics have been created</td>";
            echo "</tr>";
        echo "</tbody>";
    }

    $pre_topic = $forum_topic_title;
}
}

这是我的索引页面:

<?php include "includes/header.php"; ?>
<?php include "includes/db.php"; ?>

    <!-- Navigation -->
    <?php include "includes/navigation.php"; ?>

    <!-- Page Content -->
    <div class="container">
        <div class="row">

            <!-- Blog Entries Column -->
            <div class="col-md-8">

                <!-- Forum Navigation -->
                <?php include "includes/forum_navigation.php"; ?>

                <!-- First Blog forum -->
                <table class="table table-hover">
                        <?php fetch_forum(); ?>
                </table>

                <hr>

            </div>

            <!-- Blog Sidebar Widgets Column -->
            <?php include "includes/sidebar.php"; ?>

        </div>

    </div>
    <!-- /.row -->

    <hr>

    <?php include "includes/footer.php"; ?>

这是我的标题页:

<?php ob_start(); ?>
<?php session_start(); ?>
<?php include "functions.php"; ?>
<!DOCTYPE html>
<html lang="en">

<head>

    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta name="description" content="">
    <meta name="author" content="">

    <title>My CMS Framework</title>

    <!-- Bootstrap Core CSS -->
    <link href="../css/bootstrap.min.css" rel="stylesheet">

    <!-- Custom CSS -->
    <link href="../css/blog-home.css" rel="stylesheet">
    <link href="../css/style.css" rel="stylesheet">

    <!-- Custom Fonts -->
    <link href="../admin/font-awesome/css/font-awesome.min.css" rel="stylesheet" type="text/css">

    <!-- HTML5 Shim and Respond.js IE8 support of HTML5 elements and media queries -->
    <!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
    <!--[if lt IE 9]>
        <script src="https://oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script>
        <script src="https://oss.maxcdn.com/libs/respond.js/1.4.2/respond.min.js"></script>
    <![endif]-->

    <script type="text/javascript" src="../../js/autoBreadcrumbs.js"></script>

</head>

<body>

这是我的论坛:http://innovativestudios.net/forum/

2 个答案:

答案 0 :(得分:0)

FWIW您的代码适合我,并产生:

Program output

使用这个假设的DB结构和数据集。DB Join

这可能只是测试数据集的问题吗?

答案 1 :(得分:0)

不得不稍微改变一下这个功能,但这是最终的结果:

    function fetch_forum() {
    global $connection;

    $query = "SELECT * FROM forum_categories LEFT JOIN forum_topics ON forum_topic_category_id = forum_category_id";
    $select_forum_data = mysqli_query($connection, $query);
    $new_topic = "";
    $pre_topic = "";

    while($row = mysqli_fetch_assoc($select_forum_data)) {
        $forum_topic_id = $row['forum_topic_id'];
        $forum_topic_title = $row['forum_topic_title'];
        $forum_topic_description = $row['forum_topic_description'];
        $forum_topic_category_id = $row['forum_topic_category_id'];
        $forum_category_id = $row['forum_category_id'];
        $forum_category_title = $row['forum_category_title'];

        $new_category = $forum_category_title;

        if($new_category != $pre_category) {
            echo "<thead>";
                echo "<tr>";
                    echo "<th><span class='glyphicon glyphicon-file' aria-hidden='true'></span> {$forum_category_title}</th>";
                    echo "<th>Topics</th>";
                    echo "<th>Posts</th>";
                    echo "<th>Last Post</th>";
                echo "</tr>";
            echo "</thead>";
        }

        if($forum_topic_title != null or $forum_topic_description != null) {
            echo "<tbody>";
                echo "<tr>";
                    echo "<td><a href='topic.php?t_id=$forum_topic_id'><h4>{$forum_topic_title}</h4></a><div class='row-fluid'><h6>{$forum_topic_description}</h6></div></td>";
                    echo "<td>1</td>";
                    echo "<td>1</td>";
                    echo "<td>03-28-2017</td>";
                echo "</tr>";
            echo "</tbody>";
        } else {
            echo "<tbody>";
                echo "<tr>";
                    echo "<td>No topics have been created yet.</td>";
                echo "</tr>";
            echo "</tbody>";
        }

        $pre_category = $forum_category_title;
    }
}