页面未输出与 id 关联的正确数据

Page is not outputting correct data associated with an id

我正在设计一个论坛。我要提到的页面是 discussions.php 和 forum_view_category.php

讨论是显示不同类别的页面。

forum_view_categories 是显示该类别内主题的页面。

截至目前,我只是对此进行测试,并且只有两个类别。它们的 ID 为 1 和 2。每当我单击 link 转到 forum_view_category 页面时,正确的 link 会显示为我单击的页面的相应 ID。

我的问题是第 1 类的帖子显示在第 1 类和第 2 类中。我在第 2 类中没有任何主题,所以我的 else 语句应该回显。

我的讨论页面有此 link 以转到适当的 forum_view_category 页面..

$categories = "<a href='forum_view_category.php?cid=".$categoryid."'>" . $categoryTitle . "</a>";

那么这是forum_view_category页面。

$cid = $_GET['cid'];
$userid = ( isset( $_SESSION['user'] ) ? $_SESSION['user'] : "" );

if(isset( $_SESSION['user'])) {
    $logged = " | <a href='forum_create_topic.php?cid=".$cid."'>Create a new topic</a>";
}
$query = mysqli_query($con,"SELECT * FROM forum_categories WHERE id='".$cid."' LIMIT 1");
$numrows = mysqli_num_rows($query);
if($numrows == 1){
    //$query2 = mysqli_query($con,"SELECT * FROM forum_topics WHERE `category_id` ='".$cid."' ORDER BY topic_reply_date DESC")
    $query2 = mysqli_query($con,"SELECT t.*, COUNT(p.topic_id) AS tid2 
FROM forum_topics AS t JOIN forum_posts AS p on t.id = p.topic_id 
GROUP BY t.id DESC")
    or die ("Query2 failed: %s\n".($query2->error));
    $numrows2 = mysqli_num_rows($query2);
    //if ( false===$query2 ) {
        // die(' Query2 failed: ' . htmlspecialchars($query2->error));
    //}
    if($numrows2 > 0){
        $topics .= "<table width='100%' style='border-collapse: collapse;'>";
        //Change link once discussion page is made
        $topics .= "<tr><td colspan='3'><a href='discussions.php'>Return to Discussion Index</a>".$logged."<hr /></td></tr>";
        $topics .= "<tr style='background-color: #dddddd;'><td>Topic Title</td><td width='65' align='center'>Replies</td><td width='65' 
        align='center'>Views</td></tr>";
        $topics .= "<tr><td colspan='3'><hr /></td></tr>";
        while($row = mysqli_fetch_assoc($query2)){
            $tid = $row['id'];
            $title = $row['topic_title'];
            $views = $row['topic_views'];
            $replies = $row['tid2'];
            $date = $row['topic_date'];
            $date = fixDate($date);
            $creator = $row['topic_creator'];
            $topics .= "<tr><td><a href='forum_view_topic.php?cid=".$cid."&tid=".$tid."'>".$title."</a><br /><span class='post_info'>Posted 
            by: ". $creator. " " . $date."</span></td><td align='center'>".$replies."</td><td align='center'>".$views."</td></tr>";
            $topics .= "<tr><td colspan='3'><hr /></td></tr>";
        }
        $topics .="</table>";
        echo $topics;
    } else {
        echo "<a href='discussions.php'>Return to Discussions page</a><hr />";
        echo "<p>There are no topics in this category yet. ".$logged." </p>";
    }
} else {
    echo "<a href='discussions.php'>Return to Discussions page</a><hr />";
    echo "<p>You are trying to view a category that does not exist yet.</p>";
}

我将一些查询更改为 JOIN,以便我可以在整个站点中获取其他信息,我不确定我是否在其中做错了什么。所以,我不确定为什么它获取了正确的 ID,但没有显示其中的实际内容。有人知道吗?

我认为它必须专门针对此查询...

//$query2 = mysqli_query($con,"SELECT * FROM forum_topics WHERE `category_id` ='".$cid."' ORDER BY topic_reply_date DESC")
$query2 = mysqli_query($con,"SELECT t.*, COUNT(p.topic_id) AS tid2 

FROM forum_topics AS t JOIN forum_posts AS p on t.id = p.topic_id 按 t.id 降序")

分组

被注释掉的查询是我的旧查询。如何将旧查询与新查询合并?

简单的说这个-

mysqli_query($con,"SELECT t.*, COUNT(p.topic_id) AS tid2 FROM forum_topics AS t JOIN forum_posts AS p on t.id = p.topic_id WHERE t.category_id = ".$cid." GROUP BY t.id DESC")
or die ("Query2 failed: %s\n".($query2->error));

在您的查询结束时2