Jump to content

Order by latest post conundrum


joe92

Recommended Posts

I'm trying to create a basic forum to pin onto my site.

 

I've hit a problem with the page which displays all the threads of a certain board.

 

Similarly to phpBB and many others, I want the threads/topics to be ordered by the latest post. I am having trouble with this query

 

//get all the thread id's from this board
$thread_query = mysql_query("SELECT DISTINCT threadID FROM forum_posts WHERE boardID = '$bid' ORDER BY postTime");

 

The above query only gets the 1st unique threadID and ignores the rest meaning the order is incorrect.

 

How can I rewrite this query so it will select the unique thread ID's, but order the thread ID's by the latest postTime?

 

Cheers, Joe

Link to comment
Share on other sites

Hi

 

Do you just have a table of posts, or do you have a table of threads as well?

 

$thread_query = mysql_query("SELECT threadID, MAX(postTime) AS MaxPostTime FROM forum_posts WHERE boardID = '$bid' GROUP BY threadID ORDER BY MaxPostTime");

 

All the bst

 

Keith

Link to comment
Share on other sites

$thread_query = mysql_query("SELECT threadID, MAX(postTime) AS MaxPostTime FROM forum_posts WHERE boardID = '$bid' GROUP BY threadID ORDER BY MaxPostTime");

 

Thank you very much! Worked a treat. I just needed to add a DESC to the end to get my desired result:

$thread_query = mysql_query("SELECT threadID, MAX(postTime) AS MaxPostTime FROM forum_posts WHERE boardID = '$bid' GROUP BY threadID ORDER BY MaxPostTime DESC");

 

[attachment deleted by admin]

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.