百分比和粘性

时间:2013-02-28 18:28:17

标签: php loops percentage sticky

我正在研究这个项目,并且我一直在遇到一些问题。首先,这是一个测验,需要计算正确的百分比和输出(所有代码将在最后)。它现在的问题在于它总是计算0%,即使有正确的答案,也为它定义了一个变量。我的第二个问题是它不粘,而且需要它。我不知道那是怎么回事。我现在只完成了一条线并且没有粘性。救命?

<!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>

2 个答案:

答案 0 :(得分:1)

你的问题在这里

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

(和其他3行一样)。单选按钮的“值”为“1”,因此,如果选中它,则作为POST变量发送的内容为q1 = 1。

然后,稍后,你在线上比较它:

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

等同于:

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

我们知道$ _POST ['q1']将是'one',所以即使你选择了正确的答案,它也永远不会被计算在内。

有两种方法可以解决这个问题

  1. 将数组中的所有答案更改为“一个”,“两个”等...
  2. 更改单选按钮的值,同时输出$ question ['choice 1']
  3. 希望这有助于您走上正确的道路;)

答案 1 :(得分:0)

不确定为什么你会期望“$ question ['answer']”(低)匹配$ _POST [$ key]“one”??

您的处理逻辑不仅不正确(因为您将单选按钮的值设置为“一”,“两个”......等等。但您用于创建单选按钮的逻辑不正确。

回到你的问题数组上的print_r,然后一旦你看到你正在处理的是什么,弄清楚你将如何适当地循环它以构建你的单选按钮。

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

这是一个好的开始:

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