0

I'm working on this project and I keep running into some issues. First off, it's a quiz that needs to calculate the percentage correct and out put it (all the code will be at the end). The issue with it right now is that it's always calculating 0%, even with right answers and a variable is defined for it. My second issue is that it's not sticky and it needs to be. I don't know what's going on with that. I have only one line done right now and it's not sticky. Help?

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Untitled Document</title>
</head>

<body>
<form method="post">
<?php
    $score = 0;
$percent = 0;

//checking submit button
if(isset($_POST['submit'])){ 
}


//multi-dimensional arrays for questions, selections, and answers
$questions = array(
    'q1' => array('question' => 'Which is optional to making Easy Fudge?', 
        'choice 1' => 'Nuts', 
        'choice 2' => 'Condensed Milk', 
        'choice 3' => 'semi-sweet morsels', 
        'choice 4' => 'bakers chocolate <br />', 
        'answer' => 'nuts 
'),
        'q2' => array('question' => 'One square of bakers chocolate is optional to help enhance the flavor and texture?',
        'choice 1' => 'Yes',
        'choice 2' => 'No',
        'choice 3' => 'I do not know what this bakers chocolate is.',
        'choice 4' => 'Maybe. <br />',
        'answer' => 'Yes', ),
    'q3' => array('question' => 'Between prepration time, cooking time, and cooling time, how long does it take to make Easy Fudge?',
        'choice 1' => '130 minutes',
        'choice 2' => '131 minutes',
        'choice 3' => '132 minutes',
        'choice 4' => '133 minutes <br />',
        'answer' => '131 minutes',),
    'q4' => array('question' => 'If divided into 48 pieces, 2 pieces of this fudge is how many calories?',
        'choice 1' => '140 calories',
        'choice 2' => '150 calories',
        'choice 3' => '160 calories',
        'choice 4' => '170 calories <br />',
        'answer' => '160 calories'),
    'q5' => array('question' => 'You combine your morsels and sweetend condensed milk in a medium sauce pan over which heat?',
        'choice 1' => 'low',
        'choice 2' => 'medium-low',
        'choice 3' => 'medium',
        'choice 4' => 'high <br />',
        'answer' => 'low'),
        );

 //radio buttons and selectability
foreach($questions as $key => $question){
echo $question['question'] . '<br />';  

// TODO: check the POST value and compare to this value (ie. "one")
echo '<input type="radio" name = "'.$key.'" value="one"> '  . $question['choice 1'] . '<br />'; 
    //echo (isset($_POST['radio'] and $_POST['radio']== "one") )? "checked" : "";

echo '<input type="radio" name = "'.$key.'" value="two" >' . $question['choice 2'] . '<br />';
echo '<input type="radio" name = "'.$key.'" value="three"> ' . $question['choice 3'] . '<br />';
echo '<input type="radio" name = "'.$key.'" value="four"> ' . $question['choice 4'] . '<br />';

}


//looping through the questions with nested loops

    if (isset($_POST["submit"])) {
foreach($questions as $key => $question){
    if ( $question['answer']== $_POST[$key]){
        $score++;   
    }else{ $score = $score;
    }
}echo $score;
}//= ($score/5)*100 . "%";

?>
<input name="submit" type="submit" />
</form>
</body>
</html>
4

2 回答 2

1

你的问题在这里

echo '<input type="radio" name = "'.$key.'" value="one"> '  . $question['choice 1'] . '<br />'; 

(和其他 3 行类似)。单选按钮的“值”是“一”,因此,如果选择它,作为 POST 变量发送的内容是 q1=one。

然后,稍后,您在线比较它:

if ( $question['answer']== $_POST[$key]){

这相当于:

if ( "nuts" = $_POST['q1'] )

我们知道 $_POST['q1'] 将是“一”,因此即使您选择了正确的答案,它也不会被计算在内。

有2种方法可以治愈这个

  1. 将数组中的所有答案更改为“一”、“二”等...
  2. 更改单选按钮的值,在那里也输出 $question['choice 1']

希望这有助于您走上正确的道路;)

于 2013-02-28T19:01:43.010 回答
0

不知道为什么你会期望 "$question['answer']" (low) 匹配 $_POST[$key] "one" ?

不仅您的处理逻辑不正确(因为您将单选按钮的值设置为“一”、“二”……等等。而且您用于创建单选按钮的逻辑也不正确。

返回并在您的问题数组上打印_r,然后一旦您看到您正在处理的内容,请弄清楚您将如何适当地循环它以构建您的单选按钮。

接下来,您需要在 $_POST 数据上 print_r 并查看 THAT 是如何返回的。

这是一个好的开始:

echo '<input type="radio" name = "'.$key.'" value="' . $question['choice 1'] . '"> '  . $question['choice 1'] . '<br />'; 
于 2013-02-28T18:55:30.860 回答