NamePros
Welcome, Guest! Ready to make a name for yourself in the domain business? We welcome both the hobbyist and professional domainer to join the discussion as part of the NamePros community.

Click here to create your profile to start earning reputation for posting, and trader ratings for buying & selling in our free e-marketplace. Build your trader rating with each successful sale. Our system has tracked over 100,000 sales and counting!
FAQ & TOS Register Search Today's Posts Mark Forums Read

Go Back   NamePros.com > Website Development Discussion Forums > Programming
Reload this Page [PHP] category/topic markers?

Programming PHP, Perl, Ruby on Rails, AJAX, HTML, XHTML, CSS, JavaScript, MySQL and any other coding topics.

Advanced Search


Closed Thread
 
LinkBack Thread Tools
Old 06-15-2008, 08:39 AM THREAD STARTER               #1 (permalink)
The original NP Emo Kid
 
liam_d's Avatar
Join Date: Jan 2005
Location: Plymouth, UK
Posts: 1,693
liam_d is a name known to allliam_d is a name known to allliam_d is a name known to allliam_d is a name known to allliam_d is a name known to allliam_d is a name known to all
 




[PHP] category/topic markers?


I am making a simple forum and wish to show read/unread forum and topic markers like all other forums seems to have, any dieas on the best way?
liam_d is offline  
Old 06-15-2008, 09:49 AM   #2 (permalink)
NamePros Member
Join Date: Sep 2006
Posts: 99
Bruce_KD will become famous soon enoughBruce_KD will become famous soon enough
 



Assuming you're going by user, not just everyone? (Eg, if Billy reads the topic, it will still be marked as unread for Bobby).

I'm not sure if it would be the best way to go about it, but...
I would have the table of Forums (each has an id, title, etc)
A table for the threads (each has an id, title, then the forum it belongs to)
And a table for replies (also containing an id and the thread it belongs to)

You could then create a table for views, which would contain the user, thread id, and time of view.The first time they view, you would insert a new row. The next, you would update that row.

When deciding if they've viewed or not:

Assuming you had $userid as their id...

Code:
SELECT forums.id AS the_id, forums.title, (
  SELECT Count( threads.id )
  FROM threads
  WHERE threads.forum = the_id
) - (
  SELECT Count( views.thread )
  FROM views
  LEFT JOIN threads ON views.thread = threads.id
  WHERE views.user = '$userid'
  AND threads.forum = the_id
  LIMIT 1 
) AS not_viewed
FROM `forums`
That will return the forum id, the forum title, and the number of threads within the forum that the user has not read. If "not_viewed" is greater than zero, there are unread threads.
????: NamePros.com http://www.namepros.com/programming/481956-php-category-topic-markers.html

Now, if you want to have it mark something as unread every time someone posts in that thread, you could do it a few times...

1. In the thread table, you could have a last_post time. When selecting what they've read, check that last_post time against their last read, eg:
Code:
SELECT forums.id AS the_id, forums.title, (
  SELECT Count( threads.id )
  FROM threads
  WHERE threads.forum = the_id
  LIMIT 1
) - (
  SELECT Count( views.thread )
  FROM views
  LEFT JOIN threads ON views.thread = threads.id
  WHERE views.user = '$userid'
  AND threads.forum = the_id
  AND views.time > threads.last_post
  LIMIT 1 
) AS not_viewed
FROM `forums`
2. When someone posts in a thread, just delete all entries in the views table with that thread.

3. You could bring the views table down to the post level, though this would be messy. I'd choose #1 or #2.


I used subqueries instead of joins because I often find when dealing with large table, it can be fast. Doing 2 subqueries for maybe 10 forums is probably faster than joining up 10 forums with 1,000 threads and 100,000 views.

Personally, I test all my queries for timing to find the fastest (which usually means most efficient). I often insert a realistic amount of fake data in before doing so.

This may not be (and probably isn't, but I've never bothered to code forums when there are decent ones available already) the best way to do it, but it should work.

The code is untested. It is possible it may not work (Or your server might blow up.)


Bruce
Bruce_KD is offline  
Old 06-15-2008, 09:55 AM THREAD STARTER               #3 (permalink)
The original NP Emo Kid
 
liam_d's Avatar
Join Date: Jan 2005
Location: Plymouth, UK
Posts: 1,693
liam_d is a name known to allliam_d is a name known to allliam_d is a name known to allliam_d is a name known to allliam_d is a name known to allliam_d is a name known to all
 




See i thought about that way but then that is pretty database intensive. I will give it a go later and see how it works out though.
liam_d is offline  
Old 06-19-2008, 08:02 AM THREAD STARTER               #4 (permalink)
The original NP Emo Kid
 
liam_d's Avatar
Join Date: Jan 2005
Location: Plymouth, UK
Posts: 1,693
liam_d is a name known to allliam_d is a name known to allliam_d is a name known to allliam_d is a name known to allliam_d is a name known to allliam_d is a name known to all
 




Anyone got any other ideas?
liam_d is offline  
Closed Thread


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools


Liquid Web Smart Servers  
All times are GMT -7. The time now is 06:08 AM.

Managed Web Hosting by Liquid Web
Domain name forum recommended by Domaining.com Powered by: vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.6.0 Ad Management plugin by RedTyger