Reputation: 13
I want to display the Questions from the database for online aptitude test. i generate code to display 10 question randomly.
but major problem is Question is repeating. i don't want repeated question in question paper.
here is a code for display question randomly:-
<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    ";
echo "B <input type=radio name = 'answer[$q]' value = '$b'></input>$b    ";
echo "C <input type=radio name = 'answer[$q]' value = '$c'></input>$c     ";
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>
Upvotes: 1
Views: 293
Reputation: 20909
remove the for-loop, and fetch all 10 questions at once. As Long as there are no double entries in the database, this will give you 10 unique questions.
$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    ";
echo "B <input type=radio name = 'answer[$q]' value = '$b'></input>$b    ";
echo "C <input type=radio name = 'answer[$q]' value = '$c'></input>$c     ";
echo "D <input type=radio name = 'answer[$q]' value = '$d'></input>$d <br><br> ";
}
}
Upvotes: 1