1

我想显示数据库中的问题以进行在线能力倾向测试。我生成代码以随机显示 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>
4

1 回答 1

1

删除 for 循环,一次获取所有 10 个问题。只要数据库中没有重复条目,这将为您提供 10 个独特的问题。

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

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


    }

 }
于 2013-08-14T17:02:27.837 回答