Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 12 of 12
  1. #1
    Senior Coder
    Join Date
    Jun 2008
    Location
    New Jersey
    Posts
    2,536
    Thanks
    45
    Thanked 259 Times in 256 Posts

    Could use help figuring some logic: forums' last read data

    I've been working on building a forum for quite a while, for use in a site as well as expanding my technique library.

    I'm currently adding in tools that would allow a forum admin to move threads between forums, and I've reached a bit of a road block with how to store the information on what threads a user has ready/the last post a user read.

    What I'm doing now is I store a serialized array onto the DB containing the read data. The array is formatted as such:

    Code:
    Array
    (
    	[1] => Array
    	(
    		[forums] => Array
    		(
    			[2] => Array
    			(
    				[forums] => Array
    				(
    				)
    
    				[threads] => Array
    				(
    					[4] => Array
    					(
    						[lastRead] => 58
    						[lastPost] => 59
    					)
    
    					[5] => Array
    					(
    						[lastRead] => 12
    						[lastPost] => 12
    					)
    				)
    
    				[markedRead] => 58
    			)
    		)
    
    		[threads] => Array
    		(
    			[1] => Array
    			(
    				[lastRead] => 0
    				[lastPost] => 18
    			)
    
    			[2] => Array
    			(
    				[lastRead] => 76
    				[lastPost] => 76
    			)
    		)
    
    		[markedRead] => 76
    	)
    
    	[2] => Array
    	(
    		[forums] => Array
    		(
    		)
    
    		[threads] => Array
    		(
    			[3] => Array
    			(
    				[lastRead] => 0
    				[lastPost] => 35
    			)
    		)
    
    		[markedRead] => 0
    	)
    )
    Basically, each array contains 3 indexes: 'forums', 'threads', and 'markedRead'. Each element in forums is another array of the same format. Through simple looping and comparison, I can find out if a forum contains unread posts, and by grabbing the latest post made, and comparing to the 'markedRead' (which is always the highest of the 'lastRead' in the thread arrays), I can find out if a new post has been made. Simple and easy.

    The issue I've run into is if I move a thread, moving it from one thread array to another means going into EVERY users read data, unserializing it, restructuring it, then researlizing it and putting it back into the DB... Messy, intensive, not good.

    I'm not sure how I can keep track of the data required, and change it, without messy DB work. And its obvious the big forums store this kinda data on the DB side... I can read a post here on my desktop, then go to my laptop, and the read data is still there...

    Any thoughts on how I can do this? I know its kind of a DB question, but I feel like if I can figure out the data structure, which is more the PHP end, I can work out the DB entries. However, if people feel this is a DB question, I'll ask a mod to move it to the mySQL section.

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    Yep you bet. This is why you shouldn't be storing serialized arrays, and should be storing normalized data only.
    As soon as you've stored a flattened array you no longer have the ability to search, query, modify or delete it from the database side. Your only options are to either normalize the database or iterate every array and update it (not recommended if there is a server imposed query limit).

  • #3
    Senior Coder
    Join Date
    Jun 2008
    Location
    New Jersey
    Posts
    2,536
    Thanks
    45
    Thanked 259 Times in 256 Posts
    That's just it... I've been trying to figure out how things like PHPBB and vBulletin do this, but I don't see a table just for read data... so I donno if I'm missing something, if they're unflattening and putting new data in, or something else all together... I could go for a normalized table, but is that worth it?

  • #4
    Super Moderator Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,647
    Thanks
    2
    Thanked 406 Times in 398 Posts
    I believe forums generally use cookies to keep track of read data. The only thing that gets stored in the database usually is the last time a page was loaded.

  • #5
    Senior Coder
    Join Date
    Jun 2008
    Location
    New Jersey
    Posts
    2,536
    Thanks
    45
    Thanked 259 Times in 256 Posts
    Hm... I guess I will go back to the board on it then... I coulda sworn I've seen the data move between computers, and even after wiping cookies... maybe I'm wrong... Storing the data to a cookie would make it much easier to work with... Although if its in a cookie, my data structure above would still have issues if a thread is moved from one forum to another...

  • #6
    Super Moderator Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,647
    Thanks
    2
    Thanked 406 Times in 398 Posts
    Don't make it 3-dimensional with the forum ID, just keep IDs of threads that have been read in a 2 dimensional array with the last time or last post id as the value.

  • #7
    Senior Coder
    Join Date
    Jun 2008
    Location
    New Jersey
    Posts
    2,536
    Thanks
    45
    Thanked 259 Times in 256 Posts
    Ok, definitely not just cookies... I'm posting this from my dad's computer, which has never been to CodingForums before, and the forum read data is here (eg, it showed which posts I've read and haven't).

  • #8
    Senior Coder
    Join Date
    Jun 2008
    Location
    New Jersey
    Posts
    2,536
    Thanks
    45
    Thanked 259 Times in 256 Posts
    Quote Originally Posted by Inigoesdr View Post
    Don't make it 3-dimensional with the forum ID, just keep IDs of threads that have been read in a 2 dimensional array with the last time or last post id as the value.
    But then to check if a forum has a new post, I'd have to do a query to check every thread in a forum after a point, wouldn't I? Basically, with a 2 dimensional structure, how would I check to see if an entire forum (not just thread) had a new post, and wouldn't that create a compounding issue with subforums? eg, the forum itself has no new posts, but a child forum, or a child of a child, does?

  • #9
    Super Moderator Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,647
    Thanks
    2
    Thanked 406 Times in 398 Posts
    Well, you don't need to go back a long way, you are paging your results, right? Limiting to 50 or less per page? All you would have to do is check the database or cookie, whichever you are using, as your output the thread. You shouldn't store a row for each user & thread either.

  • #10
    Senior Coder
    Join Date
    Jun 2008
    Location
    New Jersey
    Posts
    2,536
    Thanks
    45
    Thanked 259 Times in 256 Posts
    Figuring out if a thread has new posts on it is the easy part with a 2d array... figuring out if an entire forum has a new post is harder with a 2d array.

    I think the best solution is to create 2 arrays. One would contain a list of forums, in the recursive structure above, just storing the markedRead data and what children it has. The second would contain a 2d list of the threads, as an array of the forum their in, their lastPost, and their lastRead.

    Then, if I need to find out if a forum has new posts, I can get the last posts to a forum, loop through the list of threads and checking if any of the last post is greater then the last post of all the threads (the greatest last post of the list of threads). Compared to my current method, this has the disadvantage of having to loop through the entire list of threads to find out which ones apply and don't.

    But if a thread is moved, this one is much simpler to work with: once serialized, the forumID location on the string will be uniform, and thus i can use mySQL's REPLACE method to search for the the portion of the string containing the thread info, and replace the forumID listed.

    I still feel there may be an easier way, but so far, I got nothin else. And my google fu has failed to tell me how tech like these forums do it.

  • #11
    Super Moderator Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,647
    Thanks
    2
    Thanked 406 Times in 398 Posts
    Quote Originally Posted by Keleth View Post
    I still feel there may be an easier way, but so far, I got nothin else. And my google fu has failed to tell me how tech like these forums do it.
    You could always check the phpBB source.

  • #12
    Senior Coder
    Join Date
    Jun 2008
    Location
    New Jersey
    Posts
    2,536
    Thanks
    45
    Thanked 259 Times in 256 Posts
    Quote Originally Posted by Inigoesdr View Post
    You could always check the phpBB source.
    I tried... so far nothing, its complex :P


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •