Hi Hi! Guys, I try to make a test generator in php, which takes random questions from the database by the block number and outputs 4 random answers to them, one of which is necessarily the correct one. There is a table of questions with columns: ID, Vopros, Block and a table of answers: ID, Otvet, ID_vopr, Znach - 0 or 1 to indicate the correct answer. According to your mind, you need to make several types of questions: with one option (radio), with several options (checkbox) and at least with a choice from the drop-down list.
//Здесь выводится случайный вопрос из таблицы Vopr while ($num<=5) { $query = "SELECT * FROM Vopr WHERE Block='$num'"; $q=@mysql_query ($query); if(!$q) break; while ($row=@mysql_fetch_array($q)) {$quest[]=$row;} $current=$quest[rand(0,count($quest)-1)]; echo "<hr>"; echo $current['Vopros']; echo "</hr>"; echo "<input type='hidden' name='q[$num]' value='".$current['ID']."'>"; //а вот тут начинаются проблемы /*если выводить одним запросом 4 ответа случайных - то в них может не войти правильный ответ, и чем больше вариантов ответа - там больше шанс промаха*/ /*в качестве костыля я сделал вывод отдельно правильного и неправильного варианта, пока не придумал нормального решения*/ $query2= "SELECT * FROM Otv_easy WHERE ID_vopr='".$current['ID']."' AND Znach='1'"; $query3= "SELECT * FROM Otv_easy WHERE ID_vopr='".$current['ID']."' AND Znach='0' ORDER BY RAND() limit 3"; $a=@mysql_query ($query2); $b=@mysql_query ($query3); while ($arow=@mysql_fetch_array($a)) { echo "<p><input type='checkbox' name='a[$num]' value='".$arow ['ID']."'> ".$arow ['Otvet']." </p>"; } while ($arow=@mysql_fetch_array($b)) { echo "<p><input type='checkbox' name='a[$num]' value='".$arow ['ID']."'> ".$arow ['Otvet']." </p>"; } unset($quest); $num++; // переходим к следующему вопросу } echo "<INPUT TYPE=submit VALUE='Отправить'>"; This is where the first question arises: How can I get rid of the crutch and put the results of the queries $ query2 and $ query3 into an array, then sort it and output as answers?
I seemingly figured out with checking the correct answers for radio buttons. Here is a form that validates the correct answers and counts points:
for ($i=1; $i<=count($q); $i++) {$query="SELECT * FROM otv WHERE ID_vopr='".$q[$i]."' AND Znach='1'"; $p=@mysql_query($query); $row=@mysql_fetch_array($p); $answer = $row['ID']; if($a[$i]==$answer) $ball++; unset($query,$p,$row); } But with its help, it is impossible to correctly count points for a checkbox, because there is a check up to the first element and if there are two answers in the checkbox, the answer is considered not correct, if only the second correct answer is marked - the answer is considered incorrect.
Already tried a bunch of options and he himself managed to get confused. The third day I fight this. How to make a score for the correct answers for the checkbox?
I want to make a reservation right away that I am performing the task by virtue of necessity, and I didn’t have to deal with php before this point, so I don’t have the necessary knowledge. And I would be glad to study a huge tolm by language, but it will take not a day, not two, and not a week: (Help out, brothers!
select * from Vopr order by rand() limit 1will give you exactly 1 random entry from the database. Do not doselect *. never. Clearly select only the required columns. In php, use PDO functions, not mysql_ *. Never writevalue='".$arow ['ID']."use bind_param (PDO). PDO by the way can return immediately an array from request. Requests 2 and 3 should be merged into one - Mikeorder by if(Znach=1,-1,rand()) limit 4guaranteed to give the correct answer as a result. though he will always be first. But you already have the right answer anyway. to avoid this, we need to wrap the request in one more select and give order by rand () again - Mike