Php小测验如何增加答案权重的值

Php小测验如何增加答案权重的值,php,Php,我已经使用此页面创建了我自己的测验 对于这个例子,每个问题值1分。 我想把每个问题改为10分 我尝试过改变价值,但没有运气, 有什么想法吗 <?php $answer1 = $_POST['question-1-answers']; $answer2 = $_POST['question-2-answers']; $answer3 = $_POST['question-3-answers']; $answer4 = $_POST['question-4-answers'];

我已经使用此页面创建了我自己的测验

对于这个例子,每个问题值1分。 我想把每个问题改为10分

我尝试过改变价值,但没有运气, 有什么想法吗

<?php 

$answer1 = $_POST['question-1-answers']; 
$answer2 = $_POST['question-2-answers']; 
$answer3 = $_POST['question-3-answers']; 
$answer4 = $_POST['question-4-answers']; 
$answer5 = $_POST['question-5-answers']; 

$totalCorrect = 0; 

if ($answer1 == "A") { $totalCorrect++; } 
if ($answer1 == "B") { $totalCorrect++; } 
if ($answer2 == "A") { $totalCorrect++; } 
if ($answer3 == "C") { $totalCorrect++; } 
if ($answer4 == "D") { $totalCorrect++; } 
if ($answer5) { $totalCorrect++; } 

echo "<div id='results'>$totalCorrect / 100%</div>"

?> 
更改所有

$totalCorrect++;


$totalCorrect
每个正确答案递增1。

只需将
$totalCorrect++
更改为
$totalCorrect+=10(这相当于
$totalCorrect=$totalCorrect+10
)增加10点


有关增量运算符的更多信息,请阅读。

这取决于您要问多少问题以及这些问题的总和。10道题各得10分,得100分。在这种情况下,您可以通过执行
$totalCorrect+=10,简单地将$totalCorrect增加10点

但是,如果您有任何场景,其中所有点的总和不是100,那么您应该添加一些额外的代码来显示正确结果的百分比

请允许我建议对代码进行一些更改: 它仍然可以使用一些改进,但可以给你一个想法去哪里

$questions = array(
    '1' => array('answer' => 'A', 'weight' => 10),
    '2' => array('answer' => 'B', 'weight' => 20),
    '3' => array('answer' => 'A', 'weight' => 5),
    '4' => array('answer' => 'A', 'weight' => 15),
);

$points_good = 0;
$total_points = 0;

foreach($questions as $idx => $question)
{
    if(isset($_POST['question-'.$idx.'-answers']))
    {
        $answer = $_POST['question-'.$idx.'-answers'];
        if($answer == $question['answer'])
        {
             $points_good += $question['weight'];
        }
    }
    $total_points += $question['weight'];
}

$totalCorrect = round($points_good / $total_points * 100)
echo "<div id='results'>$totalCorrect / 100%</div>"
$questions=array(
“1”=>array('answer'=>A','weight'=>10),
“2”=>array('answer'=>B','weight'=>20),
“3”=>array('answer'=>A','weight'=>5),
“4”=>array('answer'=>A','weight'=>15),
);
$points\u good=0;
$total_points=0;
foreach($idx=>$QUOTE形式的问题)
{
如果(isset($_POST['question-.$idx.'-answers']))
{
$answer=$_POST['question-.$idx.'-answers'];
如果($answer==$question['answer']))
{
$points_good+=$question['weight'];
}
}
$total_points+=$question['weight'];
}
$totalCorrect=round($points\u good/$total\u points*100)
回显“$totalCorrect/100%”

有什么代码可以帮你吗?你应该把你的代码放在“问题”主体中,使用格式标签让它更“可读”。我回答了这个问题,但也投了反对票。寻找这个答案很容易。在发布问题之前先搜索。我确实尝试过搜索答案,但找不到
$questions = array(
    '1' => array('answer' => 'A', 'weight' => 10),
    '2' => array('answer' => 'B', 'weight' => 20),
    '3' => array('answer' => 'A', 'weight' => 5),
    '4' => array('answer' => 'A', 'weight' => 15),
);

$points_good = 0;
$total_points = 0;

foreach($questions as $idx => $question)
{
    if(isset($_POST['question-'.$idx.'-answers']))
    {
        $answer = $_POST['question-'.$idx.'-answers'];
        if($answer == $question['answer'])
        {
             $points_good += $question['weight'];
        }
    }
    $total_points += $question['weight'];
}

$totalCorrect = round($points_good / $total_points * 100)
echo "<div id='results'>$totalCorrect / 100%</div>"