获取论坛主题/回复的查询 - 仅显示最新的主题/回复

时间:2013-04-23 11:39:19

标签: php mysqli forum

祝你好运!

我一直试图弄清楚这几个小时,但我无法让它发挥作用 我有一个论坛,应该显示所有主题,按最新回复排序 的数据库:
表线程
thread_id,category_id,title,created,author,status

表回复
reply_id,thread_id,created,text

查询:

$query = mysqli_query($link, "SELECT * FROM threads, replies 
WHERE threads.thread_id=replies.thread_id AND threads.category_id=$cat 
AND threads.status=1 ORDER BY replies.reply_id DESC");

问题: 它只显示带有最新回复的帖子。我使用while循环来回显所有行。 mysqli_num_rows($ query)显示3,但只打印一个线程。 while循环只包含:

while($r = mysqli_fetch_assoc($query))
{
   $thread_id = $r['thread_id'];
                    $total_comments = mysqli_num_rows(mysqli_query($link, "SELECT thread_id FROM replies WHERE thread_id=$thread_id"));
                    $last_comment = mysqli_query($link, "SELECT * FROM replies WHERE thread_id=$thread_id ORDER BY reply_id DESC LIMIT 0,1");
                    $rLastComment = mysqli_fetch_assoc($last_comment);
    $query = mysqli_query($link, "SELECT author FROM threads WHERE thread_id=$thread_id");
    $a = mysqli_fetch_assoc($query);
    echo '<tr>
     <td><img src="" alt=""></td>
     <td><a href="/forum/thread/'.$thread_id.'">'.$status.''.(substr($r['title'], 0, 15)).'</a> </td>
     <td>'.$a['author'].'</td>
     <td>'.($total_comments).'</td>
     <td>'.$lastComment.'</td>
     </tr>';
}

1 个答案:

答案 0 :(得分:0)

您需要反复拨打mysqli_fetch_assoc(),每行一次。

查看PHP文档,了解如何使用while循环输出结果的示例:http://php.net/manual/en/mysqli-result.fetch-assoc.php