祝你有美好的一天!
我一直试图解决这个问题好几个小时,但我就是无法让它发挥作用。
我有一个论坛,应该显示所有线程,按最新回复排序。
数据库:
表线程
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>';
}