I'm creating a forum like this one and I need to display my topics by last post. I want to create it so that the topic gets bumped back to the top whenever someone adds a new reply, like most forums including this one.
I have a timestamp on both my topics and replies but have no idea how to write the "SELECT" statement. I need to display the topics ordered by the last reply, or use the timestamp of the topic if there are no replies. I tried a loop to find replies but then it creates an infinite loop through the topics.
My database is set up like:
table 'forum_topic'
id
timestamp
table 'forum_reply'
id
topic_id
timestamp
// loop through topics
$topic_sql = "SELECT * FROM forum_topic";
$topic_result = mysql_query($topic_sql);
while ($topic_row = mysql_fetch_array($topic_result)) {
$topic_id = $topic_row['id'];
// find number of replies
$reply_sql = "SELECT * FROM forum_reply WHERE topic_id = '$topic_id' ORDER BY timestamp DESC LIMIT 1";
$reply_result = mysql_query($reply_sql);
$reply_amount = mysql_num_rows($reply_result);
// if there are no replies, order by timestamp from topic, otherwise order by timestamp from last reply
if ($reply_amount == 0) {
$sql = "SELECT * FROM forum_topic ORDER BY timestamp DESC";
} else {
}
}
And.... I'm completely stumped after that. I don't know how to write the "SELECT" statement to allow topics to be bumped whenever there is a new reply. The topic requires the reply and the reply requires the topic so I don't know what to do. Or if I am doing this completely wrong, please tell me the best way to do it.