Jump to content

Prevent Duplicate Outputs From Concurrent Voting


TheFilmGod
 Share

Recommended Posts

I wasn't sure which thread to put this question. It seemed more of miscellaneous help. I've googled but haven't found anything yet. Perhaps I'm using the wrong keywords.

 

I am creating a social-networking website where people can vote UP/DOWN a set of "stickies" (basically comments). To make navigation very easy the system would initially pull the top 20 most popular posts first. If the user clicks on "view more" than the next 20 records would be pulled using AJAX (in descending order of popularity).

 

Now I can do that no biggie. HOWEVER, the posts may gain/lose their rating score from concurrent voting and thus when the system queries for the next 20 records, a duplicate record may come up since a post initially in spot 20 might get shifted to spot 21 and be fed to the AJAX feed.

 

How do I prevent such duplicate records from being shown to the user? Is there a way around this?

 

Thank you in advance and apologizes if this question gets moved into a different thread.

 

Link to comment
Share on other sites

Maybe instead of fetching the next 20 via AJAX, you could fetch the original 20 + the next 20 (or next 40, 60, etc) every time. 

 

So say I'm currently viewing the original 20, clicking 'more' would now fetch the top 40 comments and show those.  Clicking 'more' again would then fetch the top 60 comments, etc.

 

You could also just store an array (client or server side) of comment IDs the user is seeing and filter out duplicates before output each time 'more' comments are requested.

Link to comment
Share on other sites

Terrific chrisdburns! After some thought I was thinking of doing something just like that.

 

Follow up question: for performance issues where should I store the array of comment ids originally fetched? Furthermore, a user could potentially have more than one page to my website open so I would either have to save a bunch of different arrays at the same time or have the array upload via AJAX the first time the user clicks on "view more."

 

Lastly, is there an "efficient" method in which I can make mysql do the dirty work of fetching recent comments except those with the comment_id of 1, 20, 2239, 12, etc. Or should this filtering be done inside the php application?

 

Thank you in advance! This is extremely helpful.

 

Link to comment
Share on other sites

The issue of having multiple windows open would be better solved by storing the array server side via javascript.  So you would use AJAX to get the thread list, filter out any duplicates with Javascript after the call, then output (or do whatever you do with em).  Each open window would have it's own stored array.  Performance would vary per computer, but I don't see something this small really mattering. 

 

There may be better ways, but this is what I would do with my current knowledge.

Link to comment
Share on other sites

This is the type of problem session variables are tailor made for.  Read out the list of results into an array variable and store that in a session variable, and base your display off that. 

 

This makes multiple windows/tabs being open complicated.  You'd have to have multiple session arrays for each open window somehow.

Link to comment
Share on other sites

I'm inclined to say that session variables would be better. Yes, a multidimensional array would be necessary with all that data stored. The only reason I am leaning to this method is because I do not want AJAX to consistently have to upload the list of already fetched ids. Or worse yet, have javascript filter out content after a user downloads additional rows AND previously fetched data. That would be counter-intuitive (performance wise).

 

Anyone else with suggestions? And thank you to the comments thus far! This greatly helps me figure out a solution!

Link to comment
Share on other sites

How many comments are you talking about?  Would it make more sense to just load them all in a js object and traverse that instead of doing ajax?

 

Good note. For performance I would be cautious to load all comments at once. In some cases where I know that the total number of "comments" or objects would be less than 50 for sure, I would load all of them at once. For anything that is ever-growing and could potentially reach several thousand, I would load only the most recent comments and use AJAX.

Link to comment
Share on other sites

This thread is more than a year old.

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.

 Share

×
×
  • 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.