I have two tables that generates an messageboard output. The first table uses a while loop to show all answers in a given topic from table posts. This works fine.
Then I have aniother table named thumbsup which collects when a user hits the "thumbs up" icon to a post in the messageboard. I only want the spesific user to be able to press the "thumbs up" button once.
I am trying to fetch data from table thumbsup to determine if the user has pressed the button or not for each post in the posts table.
I have tried the following sql statement:
SELECT topic, user FROM thumbsup WHERE topic = ".$posts_row['id']." AND user = ".$_SESSION['username']."
the table thumbsup looks like this:
id | topic | user
1 | 1 | bill
2 | 3 | rachel
3 | 5 | bill
4 | 5 | rachel
5 | 7 | rachel
my problem here is that I can't get the sql to sort by both topic and username. I can get it to sort by topic, but then it only outputs the first entry for the given post; in this case it would output "bill" on post number 5.
I think I maybe need a new while loop inside the existing while loop, but I haven't got that to work either.
Help much appreciated!
EDIT:
Ok, so I have this query to fetch the posts, AND to check if the user has a thumbsup record:
"SELECT posts.topic, posts.id, posts.content, posts.date, posts.user, posts.moderation, thumbsup.topic, thumbsup.user FROM posts LEFT OUTER JOIN thumbsup ON posts.id = thumbsup.topic AND thumbsup.user = ".$_SESSION['username']." WHERE posts.topic = " . $mysqli->real_escape_string($_GET['id']).""
- This results in none posts in the while loop, but if I remove the username, it shows only the posts again. Is there something wrong in my query?
EDIT2 - SOLUTION:
I finally got it right, much thanks to Kickstart.
Here is my final sql query:
SELECT posts.topic, posts.id, posts.content, posts.date, (posts.user) AS users, posts.moderation, thumbsup.topic, thumbsup.user FROM posts LEFT OUTER JOIN thumbsup ON posts.id = AA_forum_thumbsup.topic AND thumbsup.user = '".$_SESSION['username']."' WHERE posts.topic = " . $mysqli->real_escape_string($_GET['id'])."
As Kickstart mentioned, I needed extra quotes around the username as it is a string, and I also had to define the useres from the posts table as "users". Else the result would only output data on the posts where the username like $_SESSION['username'].
Thank you very much, and I hope this is useful for others as well!
usernameon a certaintopic. You don't need a while loop if you want to know only if this person clicked thumbs up on that post