I have a system where registered users can vote up/vote down comments for a picture. It’s very similar to Stack Overflow’s voting system.
I store the votes in a table with values as such:
vote_id | vote_comment_id | vote_user_id | vote_date | vote_type
Now I have a few questions concerning the speed and efficiency for the following:
PROBLEM: Once a user opens the picture page with comments, I need if that user has already voted UP/DOWN on a comment to show it like; “you voted up” or “you voted down” next to the comment (in Stack Overflow the vote image is highlighted).
MY POSSIBLE SOLUTION: Right now when I open a picture page I loop through each comment, and I loop through my table of votes as well and check if a user has voted and show the status (I compare the vote_user_id with the user’s session).
How efficient is this? Does anyone have a better approach to tackle this kind of problem?
You are looping through the table of votes? Are you reading the entire database into memory and then looping through it?
Have you tried querying the database only for the relevant data?