0
祝您有個美好的一天!查詢獲得論壇帖子/回覆 - 只顯示最新的帖子
我一直在試圖弄清楚這幾個小時,但我不能得到它的工作。
我有一個論壇,應該顯示所有主題,由最新的答覆排序。
數據庫:
表線程
的thread_id,CATEGORY_ID,稱號,創建,作者,狀態
表回覆
reply_id,thread_id單,創建文本
查詢:
$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>';
}
我使用一段時間($ R = mysqli_fetch_assoc($查詢)),內環路僅僅是爲了得到作者爲線程。 – user2311073 2013-04-23 11:44:34