tags:

views:

88

answers:

3

On the home page of my website I want to display the latest posts to the forum however I don't want to show the same topic twice. How can I modify the code below to do this?

http://punbb.informer.com/wiki/punbb13/integration#recent_10_posts

Basically show the latest posts, but only once for each forum topic/thread.

+1  A: 

change this line

'SELECT'    => 'p.id, p.message, t.subject',

to

'SELECT DISTINCT'   => 'p.id, p.message, t.subject',
Charlie
didn't seem to work. Does mysql support this?
MMAMail.com
MySQL should support it, but this won't work, because `p.id` is always going to be distinct for each post.
Alison R.
+3  A: 

Add a condition to keep only records where the post is the last post in the topic:

WHERE p.id = (
  SELECT pp.id 
  FROM posts AS pp
  WHERE pp.topic_id = t.id ORDER BY pp.posted DESC LIMIT 1
)
Leventix
I am not good at sql, so I am a bit confused where this "where" statement goes, can you please give me the full sql statement?
MMAMail.com
It goes between JOINS and ORDER BY like this: `'WHERE' => 'p.id = (...)',`
Leventix
unfortunately I don't know how that php library constructs the sql statements so the above did not work. I am using the PunBB forum application.
MMAMail.com
+1  A: 

If you want only one value per topic, you could group by topic, and from each topic select the most recent post. Then, you could choose the top 10 topics.

I'll write it in SQL, and you can translate that to PHP:

SELECT p.id, p.message, o.subject
FROM
((SELECT t.id
  FROM posts AS p LEFT JOIN topics AS t ON p.topic_id = t.id
  GROUP BY t.id
  HAVING p.posted = MAX(p.posted) ) ids LEFT JOIN topics AS t ON ids.id = t.id) o
                                        LEFT JOIN posts AS p ON o.id = posts.topic_id
ORDER BY p.posted DESC
LIMIT '0,10'
Anna