【问题标题】:Dynamically create and populate input radio with PHP使用 PHP 动态创建和填充输入单选
【发布时间】:2015-04-17 06:26:37
【问题描述】:

我目前正在使用 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>";

}  

我在完成这项工作时遇到了一些麻烦,不知道我如何为正确答案评分

提前致谢

【问题讨论】:

  • 考虑将问题及其潜在答案添加到一个多维数组中。这将更容易匹配正确的答案。因此编辑您的 HTML。

标签: php html loops


【解决方案1】:
<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

【讨论】:

  • 您好,先生,我有一个问题,您的代码为什么会返回一个数组,我该如何操作它的元素以便获得一些统计数据。提前致谢。
【解决方案2】:

我认为你应该这样做:

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 />';
}

【讨论】:

    【解决方案3】:

    我不明白为什么答案必须在不同的数组中。一个数组用于检查数据就足够了。看下面的代码。

    这是我为您解决问题的方法。查看 cmets(我解释了它的每个部分的作用)并尝试理解它。

    <?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).'%';
    
    }
    
    ?>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-04-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多