检查答案是否正确

时间:2016-02-29 17:29:53

标签: php

上次我问过,如果从我的代码中我可以检查用户是否回答正确,有些人说我的代码不可能,但我觉得我找到了办法,但不知道怎么写得更好。

我在PHP脚本的表格中有一行

echo "<td>" . $row['question1'] . "</td>";

这个脚本创建了一个包含大量答案的表格。我想,也许我可以做到不一样,比如

If answer === 'ēxample' echo  <td class="greenback">" . $row['question1'] .     "</td>";

else

<td class="redback">" . $row['question1'] . "</td>";

但我怎么能正确写出来?

我对

有所了解
If ($row === 'test') { echo  <td class="greenback"> . $row['question1'] . "     } else { echo  <td class="redback">" . $row['question1'] . 
}

这是正确的吗?

3 个答案:

答案 0 :(得分:0)

我不介意我是否理解你的问题,但我会给你我的建议:

if($answer == 'test')
   echo $row['question1'];
else 
   echo $row['question2'];

答案 1 :(得分:0)

为什么不使用单选按钮来显示相应的答案?

示例:

<?php
      //do the mysqli connection
      //suppose that table
      //Questions('text_question', 'alternative_A', 'alternative_B', 'alternative_C', 'correct_answer')


      $rows = $mysqli_query("SELECT * FROM questions");
      $row = $mysqli_fetch_assoc($rows); //all the info is inside that var 
?>
<form method="POST">
    <?php echo $row['text_question'];?>
    <input type="radio" name="ans" value="A"><?php echo $row['alternative_A'];?>
    <input type="radio" name="ans" value="B"><?php echo $row['alternative_B'];?>
    <input type="radio" name="ans" value="C"><?php echo $row['alternative_C'];?>
    <input type="submit" value="Answer" />
</form>

然后使用$_POST['ans']

访问answer的值
<?php
    if($_POST['ans'] == $row['correct_ans']){
        //code when user choose correct ans
    }
    else{
       //wrong answer
    }

答案 2 :(得分:0)

if($answer == 'test')
   echo '<td class='green'>' . $row['question1'] . '<td>';
else 
   echo '<td class='red'>' . $row['question2'] . '<td>';

试试这样。

相关问题