PHP MySQL创建讨论板

时间:2015-10-06 17:51:55

标签: php mysql

我目前正致力于开发讨论区。我的问题是它似乎为每个类别分配了4的id,然后显示了类别4中的论坛。(这没有得到很好的解释,但希望你能理解)......

<?php
  /* CATEGORIES */
    $query = "SELECT * FROM bkg_categories";
        try {
            $stmt = $db->prepare($query);
            $result = $stmt->execute();
        } catch(PDOException $e) {
            $error[] = "An error has occured. Please try again later.";
        }
        $categories = $stmt->fetchAll();

        /* FORUMS */
        foreach($categories as $category) {
            $catid = $category['category_id'];

            $query = "SELECT * FROM bkg_forums WHERE category_id = :catid";
            $query_params = array(':catid' => $catid);

            try {
                $stmt = $db->prepare($query);
                $result = $stmt->execute($query_params);
            } catch(PDOException $e) {
                    $error[] = "An error has occured. Please try again later.";
            }
            $forums = $stmt->fetchAll();

            foreach($forums as $forum) {

                print $forum['forum_id'];

            }

        }

        ?>

和html在页面上显示所有内容。

 <?php foreach($categories as $category): ?>
            <div><?php echo $category['category_name']; ?><?php echo $category['category_id']; ?></div>
            <table width="100%">
                <tr> 
                    <td colspan="2">Forum</td>
                    <td>Lastest Post</td>
                    <td>Topics</td>
                    <td>Posts</td>
                </tr>
                <?php foreach($forums as $forum): ?>
                    <tr>
                        <td width="5%"></td>
                        <td><a href="viewforum.php?f=<?php echo $forum['forum_id']; ?>"><?php echo $forum['forum_name']; ?></a></td>
                        <td width="15%">
                            <!--<?php foreach($posts as $post): ?>
                                <a href="viewtopic.php?f=<?php echo $post['forum_id']; ?>&t=<?php echo $post['topic_id']; ?>#<?php echo $post['post_id']; ?>"><?php echo substr($post['post_subject'], 0, 10);  ?></a>
                            <?php endforeach; ?>-->

                        </td>
                        <td width="5%" class="text-center"></td>
                        <td width="5%" class="text-center"></td>
                    </tr>
                <?php endforeach; ?>
            </table>
        <?php endforeach; ?>

编辑:

我创建了四个类别

一般 发展 赌博 题外话

和1个category_id为4的论坛,以及1个论坛,显示在每个类别中。

1 个答案:

答案 0 :(得分:1)

你每次都在覆盖$论坛。

您可以执行类似

的操作

$forums[$catid] = $stmt->fetchAll();

然后

foreach($forums[$category['category_id']] as $forum):