只有php查询的结果显示在html表中,其余显示在外面

时间:2016-11-12 15:35:23

标签: php mysql html-table

我正在建立一个用于学习目的的论坛。我成功地从数据库中重新定义论坛类别并将其显示在表格中,但只有第一个类别显示在表格中,其余类别显示在表格之外。我将发布我的代码和图片的屏幕截图。

<?php
include 'connect.php';
include 'header.php';

$sql = "SELECT categories.cat_id,categories.cat_name,
                categories.cat_description, COUNT(topics.topic_id) AS topics
        FROM categories
            LEFT JOIN topics ON topics.topic_id = categories.cat_id
        GROUP BY categories.cat_name, categories.cat_description, 
                categories.cat_id";

$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
        echo '
            <div class="container">
            <table class="table forum tale-striped table-hover">
            <thead>
            <tr>
                <th class="cell-stat"></th>
                <th><h3>Category</h3></th>
                <th><h3>Last topic</h3></th>
            </tr>
            </thead>';  

        while($row = mysql_fetch_assoc($result)) {          
            echo '<tbody>';
            echo '<tr >';
            echo '<td class="text-center"><i class="fa fa-exclamation fa-2x text-danger">     </i></td>';
            echo '<td><h3><a href="category.php?id=' . $row['cat_id'] . '">' . $row['cat_name'] . '</a></h3>' . $row['cat_description'].'</td>';

            echo '<td class="float-xs-right">';

            //fetch last topic for each cat
            $topicsql = "SELECT topic_id, topic_subject, topic_date, topic_cat
                        FROM topics
                        WHERE topic_cat = " . $row['cat_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_assoc($topicsresult))
                        echo '<a href="topic.php?id=' . $topicrow['topic_id'] . '">' .  $topicrow['topic_subject'] . '</a> at ' . date('d-m-Y',  strtotime($topicrow['topic_date']));
                    }
                }
                echo '</td>';
                echo '</tr>';
                echo '</tbody>';
                echo '</table>';
                echo '</div>'; //container 
            }
        }
    }

include 'footer.php';
?>

https://www.dropbox.com/s/c1dgijuafgv9jzu/Capture.PNG?dl=0

2 个答案:

答案 0 :(得分:0)

因为你正在关闭<table>循环中的while标记。此外,<tbody>也应该在循环之外,因为在这样的表中应该只有一个<tbody>

答案 1 :(得分:0)

很简单,你在while循环中有一个结束</table>标记,所以一旦输出第一行你关闭表。只需将它移动到while循环之外,同样开口<tbody>也需要移动到while循环之上

<?php
include 'connect.php';
include 'header.php';

$sql = "SELECT categories.cat_id,categories.cat_name,
                categories.cat_description, COUNT(topics.topic_id) AS topics
        FROM categories
            LEFT JOIN topics ON topics.topic_id = categories.cat_id
        GROUP BY categories.cat_name, categories.cat_description, 
                categories.cat_id";

$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
        echo '
            <div class="container">
            <table class="table forum tale-striped table-hover">
            <thead>
            <tr>
                <th class="cell-stat"></th>
                <th><h3>Category</h3></th>
                <th><h3>Last topic</h3></th>
            </tr>
            </thead>
            <tbody>';   //<-- moved

        while($row = mysql_fetch_assoc($result)) {          

            echo '<tr >';
            echo '<td class="text-center"><i class="fa fa-exclamation fa-2x text-danger">     </i></td>';
            echo '<td><h3><a href="category.php?id=' . $row['cat_id'] . '">' . $row['cat_name'] . '</a></h3>' . $row['cat_description'].'</td>';

            echo '<td class="float-xs-right">';

            //fetch last topic for each cat
            $topicsql = "SELECT topic_id, topic_subject, topic_date, topic_cat
                        FROM topics
                        WHERE topic_cat = " . $row['cat_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_assoc($topicsresult))
                        echo '<a href="topic.php?id=' . $topicrow['topic_id'] . '">' .  $topicrow['topic_subject'] . '</a> at ' . date('d-m-Y',  strtotime($topicrow['topic_date']));
                    }
                }
                echo '</td>';
                echo '</tr>';
            }
        }
        echo '</tbody>';            //<-- moved
        echo '</table>';            //<-- moved
        echo '</div>'; //container  //<-- moved

    }

include 'footer.php';
?>