如何检查查询结果中的变量以更改同一个查询的while循环?

时间:2015-12-12 17:05:20

标签: php mysql database

我根本无法解决这个问题。我很确定我的做法是错误的,但我对PHP和SQL还是比较陌生的。

我正在做的是在while循环中生成这些HTML'问题块',这些包含一些查询信息,包括投票按钮。如果用户已经投票,我想要删除该投票按钮。

我正在使用查询检查用户是否已使用QuestionID和用户的IP地址投票。

我可以阻止用户投票,(按钮不执行任何操作),但我不能为我的生活,删除用户已经投票的问题的按钮。

下面是我试图编写的代码(IP地址在别处获得)

//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>";

具有重要元素的表格:
问题 - QuestionID(PK),QuestionText
答案 - AnswerID(PK),Answer1Text,Answer2Text
question_answers - QuestionAnswerID(PK),AnswerID(FK),QuestionID(FK),QuestionVotes
user_votes - QuestionAnswerID(FK),UserID

1 个答案:

答案 0 :(得分:1)

首先,我将循环中的$ stmt和$ result更改为$ stmt2和$ result2,以便不覆盖原始结果集,如果用户尚未投票,则使用if生成投票按钮。

但是,我不喜欢在循环中运行查询,所以我们可以使用左连接并检查我们是否有匹配的UserID。此外,我修改了您的查询以明确选择字段。这可以防止字段名称冲突,并且有助于性能限制结果的大小。

//Query for selecting the information I want in the blocks.
$stmt = $conn->prepare("SELECT qa.QuestionAnswerID, a.Answer1Text, a.Answer2Text, q.QuestionText, uv.UserID, qa.QuestionVotes
    FROM question_answers qa
    INNER JOIN question q
    ON qa.QuestionID=q.QuestionID
    INNER JOIN answers a
    ON qa.AnswerID=a.AnswerID
    LEFT JOIN user_votes uv
    ON uv.UserID = ? AND uv.QuestionAnswerID = qa.QuestionAnswerID
    WHERE HasBeenTray = 0 OR HasBeenTray = 1 AND qa.QuestionVotes > 2000
    ORDER BY qa.QuestionVotes DESC LIMIT 8");
    $stmt->bind_param('s', $ip_long);
    $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"];

        $counter++;
        echo "<div class=\"col-md-3\"><h3>". $row["Answer1Text"]. " vs. ". $row["Answer2Text"]. " </h3><p>". $row["QuestionText"]. "</p>";
//If user hasn't voted, generate vote button 
        if (is_null($row['UserID']) {
            echo "<p><a  data-ks-load-selector=\"#change\" href=\"index.php?id=". $row["QuestionAnswerID"]. "\" class=\"btn btn-success\"> " . $row["QuestionVotes"] . "</a></p>";
        }
        echo "</div>";
        if($counter % 4 == 0) {
          echo "</div><div class=\"row tabs\">";
        }
      }echo "</div>";