我目前正在使用PHP和HTML,我正在进行多项选择考试,PHP文件包含四个数组,第一个存储问题,第二个存储选项“a)”的可能答案,第三个存储选项“b)”的可能答案,最后一个存储选项“c)的答案”
这里是他创建和填充问题和多重选择的代码。
for($i=0; $i<20; $i++){
echo "$i.".$preguntas[$i]."<BR>";
echo"<input type = 'radio' name='R$i'>a)".$r1[$i]."<br>";
echo"<input type = 'radio' name='R$i'>b)".$r2[$i]."<br>";
echo"<input type = 'radio' name='R$i'>c)".$r3[$i]."<br>";
echo "<BR><BR>";
}
我有一些麻烦要做这项工作,任何想法我如何评分正确的答案
提前致谢
答案 0 :(得分:0)
我认为你应该这样做:
foreach ($preguntas as $key => $pregunta) {
echo ($key + 1) . '. ' . $pregunta . '<br />';
for ($i = 1; $i <= 3; $i++) {
echo '<input type="radio" name="answerForQuestion' . $key . '" value="' . ${'r' . $i}[$key] . '" /><br />'
}
echo '<br /><br />';
}
答案 1 :(得分:0)
我不明白为什么答案必须在不同的数组中。一个考试数据阵列就足够了。看下面的代码。
这是我解决您问题的方法。请参阅注释(我解释了它的每个部分的作用)并尝试理解它。
<?php
// Set questions and answers
$questions = array(
// First question
array(
'question' => 'Question 1?',
// Possible answers for first question
'answers' => array(
array(
'answer' => 'Answer option 1',
'correct' => false
),
array(
'answer' => 'Answer option 2',
'correct' => true
),
array(
'answer' => 'Answer option 3',
'correct' => false
),
)
),
// Second question
array(
'question' => 'Question 2?',
// Possible answers for second question
'answers' => array(
array(
'answer' => 'Answer option 1',
'correct' => false
),
array(
'answer' => 'Answer option 2',
'correct' => false
),
array(
'answer' => 'Answer option 3',
'correct' => true
),
)
)
);
// Print form
echo '<form method="POST">';
$i = 1;
foreach ($questions as $question_key => $questionArray) {
echo $i.'. '.$questionArray['question'].'<br />';
// Answers will be POSTed in one array (answerForQuestion)
// answerForQuestion array keys would be questions keys.
// Values would be keys to our answers array.
// So we can easily check for correct answers. See below.
foreach ($questionArray['answers'] as $answer_key => $answerArray) {
echo '<input id="question_'.$question_key.'_'.$answer_key.'" type="radio" name="answerForQuestion['.$question_key.']" value="'.$answer_key.'"> <label for="question_'.$question_key.'_'.$answer_key.'">'.$answerArray['answer'].'</label><br />';
}
echo '<br /><br />';
// Increase questions order number.
$i++;
}
echo '<input type="submit" name="go" value="Answer!">';
echo '</form>';
// Check for answers, if user POSTed data.
if($_POST['go']){
// Get count of questions
$questionsCount = count($questions);
// This will be increased with correct answer
$correctAnswersCount = 0;
// Check for every question.
// This will quarantee that empty answer will be considered as false.
// Assuming that every question has correct answer.
foreach ($questions as $question_key => $questionArray) {
// Get answer key. Key of our array, so we can easily find selected answer
$answer_key = $_POST['answerForQuestion'][$question_key];
// Get selected answer array
$answerArray = $questionArray['answers'][$answer_key];
// If is correct
if($answerArray['correct']){
echo 'Your answer for question "'.$questionArray['question'].'" is correct!<br />';
$correctAnswersCount++;
}
else{
echo 'Your answer for question "'.$questionArray['question'].'" is incorrect!<br />';
}
}
// Just for statistics. Not in your question, but wount hurt
echo '<br />Correct answers: '.$correctAnswersCount.' out of '.$questionsCount.'. Result: '.round($correctAnswersCount / $questionsCount * 100).'%';
}
?>
答案 2 :(得分:0)
<html>
<body>
<form method="post">
<?php
echo "<pre>";
var_export($_POST['R']);
echo "</pre>";
for($i=0; $i<20; $i++){
echo "$i.".$preguntas[$i]."<BR>";
echo"<input type='radio' name='R[$i]' value='a'>a){$r1[$i]}<br>";
echo"<input type='radio' name='R[$i]' value='b'>b){$r2[$i]}<br>";
echo"<input type='radio' name='R[$i]' value='c'>c){$r3[$i]}<br>";
echo "<BR><BR>";
}
?>
<button>submit</button>
</form>
</body>
</html>
您会收到类似的内容:
array (
0 => 'a',
1 => 'b',
2 => 'b',
)
例如:
<html>
<body>
<form method="post">
<?php
$questions = [
[
'question' => 'Who was first programmer?',
'answers' => [
'a' => 'Alan Turing',
'b' => 'Ada Lovelace',
'c' => 'Rasmus Lerdorf',
'd' => 'James Bond',
],
'correctAnswer' => 'b',
],
[
'question' => 'Who created php?',
'answers' => [
'a' => 'Alan Turing',
'b' => 'Ada Lovelace',
'c' => 'Rasmus Lerdorf',
],
'correctAnswer' => 'c',
],
[
'question' => 'Who created Turing machine?',
'answers' => [
'a' => 'David Beckham',
'b' => 'Rasmus Lerdorf',
'c' => 'Floyd Mayweather, Jr.',
'd' => 'Ada Lovelace',
'e' => 'Alan Turing',
],
'correctAnswer' => 'e',
],
];
if (!empty($_POST['response'])) {
foreach ($_POST['response'] as $questionId => $answerKey) {
echo '<h5>'.$questions[$questionId]['question'].'</h5>';
if ($questions[$questionId]['correctAnswer'] === $answerKey) {
echo 'Correct answer.<br>';
} else {
echo 'Wrong answer.<br>';
}
}
} else {
foreach ($questions as $questionId => $data) {
echo '<h5>'.$data['question'].'</h5>';
foreach ($data['answers'] as $key => $answer) {
echo '<input type="radio" name="response['.$questionId.']" value="'.$key.'">'.$key.') '.$answer.'<br>';
}
echo "<br><br>";
}
}
?>
<button>submit</button>
</form>
</body>
</html>
您可以调整它here