如何显示随机que而不重复?

时间:2013-08-08 16:04:36

标签: php

我想在数据库中显示有关在线能力测试的问题。 我生成代码以随机显示10个问题。

但主要问题是问题在重复。 我不想在问题文件中重复提问。

这是随机显示问题的代码: -

<html>
<body>
<form action="./eval.php" method="post">
<?php

$connect = mysql_connect("localhost" ,"root","");

mysql_select_db("aptitude");



 for ( $i = 1; $i < 11; ++$i )
{

$query = mysql_query("SELECT * FROM `main`  ORDER BY RAND() LIMIT 1 ");




 while($rows = mysql_fetch_array($query)):

    $q = $rows['Q_no'];
    $qus = $rows['Question'];
    $a = $rows['answer1'];
    $b = $rows['answer2'];
    $c = $rows['answer3'];
    $d = $rows['answer4'];
    $ans = $rows['correct'];

     echo "Q$i:-$qus <br>";
    echo "A <input type=radio name = 'answer[$q]' value = '$a'></input>$a &nbsp &nbsp"; 
    echo "B <input type=radio name = 'answer[$q]' value = '$b'></input>$b &nbsp &nbsp"; 
    echo "C <input type=radio name = 'answer[$q]' value = '$c'></input>$c &nbsp &nbsp "; 
    echo "D <input type=radio name = 'answer[$q]' value = '$d'></input>$d <br><br> ";


    endwhile;

 }


?>

<center><input name="cmdSubmit" type="submit" id="cmdSubmit" value="Submit"/>
</center>
</form>
</body>
</html>

2 个答案:

答案 0 :(得分:2)

$q = $rows['Q_no'];
$qus = $rows['Que'];
$a = $rows['A'];
$b = $rows['B'];
$c = $rows['C'];
$d = $rows['D'];
$ans=$row['ans'];

看看上面的事情: - 你已经进入了
$和= $行[ 'ANS'];
它应该是 $ ans = $ rows ['ans'];
$ ques

答案 1 :(得分:0)

根据您帖子中的一些假设对此进行抨击。请注意,这是未经测试的,可能有错误,效率不高,可能不安全等...

看来你在页面上放了30个随机问题,并希望测试每个问题的答案(你的表格和你的问题似乎不匹配100%,所以按照代码) 。

内圈中的

使你的表格看起来像这样

    echo "Q:-$qus <br>";
    echo "A <input type=radio name = 'answer[$q]' value = '$a'></input>$a &nbsp &nbsp"; 
    echo "B <input type=radio name = 'answer[$q]' value = '$b'></input>$b &nbsp &nbsp"; 
    echo "C <input type=radio name = 'answer[$q]' value = '$c'></input>$c &nbsp &nbsp "; 
    echo "D <input type=radio name = 'answer[$q]' value = '$d'></input>$d <br><br> ";

然后在您的表单处理代码中,您可以执行此操作。

    $answers = $_POST['answer'];
    foreach($answers as $question => $answer){
         $query = mysql_query("SELECT ans FROM `main` where Q_no = $question");
         $result  = mysql_fetch_array($query);
         if ($result['ans']==$answer){
              echo "Question $question was correct <br>";
         }else{
              echo "Question $question was incorrect <br>";
         }
    }

应该做的是创建一个答案数组,在数据库中查找每个答案并查看它们是否正确(假设==可以比较2)。正如我所说的不是很有效率。或优雅,但应该让你知道如何进行。