I can simply not figure this out. I am quite sure I am doing it the wrong way, but I am fairly new to PHP and SQL.
What I am doing is generating these HTML 'question blocks' in a while loop, these contain a little query information, including a voting button. What I want to do is to remove that voting button if the user has already voted.
I am using a query to check if the User has already voted using the QuestionID and the user's IP address.
I am able to stop the user from voting, (the button does nothing), but I can't for the life of me, remove the button for the questions the user has already voted on.
Below is a code I have tried to write to accomplish this (IP address is gotten elsewhere)
//Query for selecting the information I want in the blocks.
$stmt = $conn->prepare("SELECT * FROM question_answers
INNER JOIN question
ON question_answers.QuestionID=question.QuestionID
INNER JOIN answers
ON question_answers.AnswerID=answers.AnswerID
WHERE HasBeenTray = 0 OR HasBeenTray = 1 AND QuestionVotes > 2000
ORDER BY QuestionVotes DESC LIMIT 8");
$stmt->execute();
$result = $stmt->get_result();
//Checking to see if Query has generated any result (rows)
if ($result->num_rows > 0) {
//Counter for generating HTML at the right place
$counter = 0;
echo "<div class=\"row tabs\">";
echo "<h2>Top 8 over stillede spørgsmål:</h2>";
//Use results from first query to generate HTML
while($row = $result->fetch_assoc()) {
//Save QuestionAnswerID - Id of the question block clicked
$id = $row["QuestionAnswerID"];
//Second query to check if QuestionAnswerID and UserID (IP Address) has already been paired
$stmt = $conn->prepare("SELECT * FROM user_votes where UserID = ? and QuestionAnswerID = ?");
$stmt->bind_param('ss', $ip_long, $id);
$stmt->execute();
$result = $stmt->get_result();
//Second while loop to generate 'question blocks' without vote button
while($row = $result->fetch_assoc()) {
$counter++;
echo "<div class=\"col-md-3\"><h3>". $row["Answer1Text"]. " vs. ". $row["Answer2Text"]. " </h3><p>". $row["QuestionText"]. "</p></div>";
if($counter % 4 == 0) {
echo "</div><div class=\"row tabs\">";
}
}
//Generate rest of 'question blocks' with voting buttons
$counter++;
echo "<div class=\"col-md-3\"><h3>". $row["Answer1Text"]. " vs. ". $row["Answer2Text"]. " </h3><p>". $row["QuestionText"]. "</p><p><a data-ks-load-selector=\"#change\" href=\"index.php?id=". $row["QuestionAnswerID"]. "\" class=\"btn btn-success\"> " . $row["QuestionVotes"] . "</a></p></div>";
if($counter % 4 == 0) {
echo "</div><div class=\"row tabs\">";
}
}echo "</div>";
Tables with elements of importance:
question - QuestionID(PK), QuestionText
answers - AnswerID(PK), Answer1Text, Answer2Text
question_answers - QuestionAnswerID(PK), AnswerID(FK), QuestionID(FK), QuestionVotes
user_votes - QuestionAnswerID(FK), UserID