我正在进行测验。我想计算用户提交尝试后用户获得的总分数。如何创建显示用户提交答案后获得的标记的结果页面。我如何获得总分数,使得他们获得的每个正确答案为1 /(测验中的问题总数)?
这是我到目前为止所做的,而且我似乎无法让它发挥作用。
$count = 0;
foreach ($quizHistoryQ as $index => $arr){
if($count++ == 1) continue;
}
这是我的问题脚本
<?php
$Titles = array("quizHistoryQ"=>"History Quiz", "quizMathQ" =>" Math Quiz", "quizHTMLq" => "HTML Quiz");
$quizHistoryQ = [
"Q1" => ["questions"=>"ABC?",
"options" => ["this is option 1",
"this is option 2",
"this is option 3"],
"answer" =>2
],
"Q2" => ["questions"=>"This is the Question String for question 2",
"options" => ["this is option A",
"this is option B",
"this is option C"],
"answer" =>1
],
"Q3" =>["questions"=>"This is the Question String for question 3",
"options" => ["this is option X",
"this is option Y",
"this is option Z"],
"answer" =>0
]
];
echo 'Title:'. $Titles['quizHistoryQ'];
foreach( $quizHistoryQ as $index => $arr ){
$question=$arr['questions'];
$options=$arr['options'];
$answer=$arr['answer'];
echo '<h3>Question: '.$index.': '.$question.'</h3>';
echo '<ul>';
foreach ($options as $i => $options) {
echo " <br> <input type='radio' name='{$index}[]' value='{$i}'/>{$options}";
}
echo '</ul>';
}
任何建议或帮助都将不胜感激。
答案 0 :(得分:0)
这个问题是我以前见过的 - 也许接下来应该有所帮助。
<html>
<head>
<title>quiz</title>
<style>
form{ display:block; float:none; width:90%; margin:1rem auto; box-sizing:content-box; padding:1rem; border:1px solid black; }
ul{}
li{}
h3{ margin:1rem auto; }
section{margin:1rem auto;}
section:nth-of-type(odd){background:whitesmoke;}
output{margin:2rem auto;}
</style>
</head>
<body>
<?php
$quizHistoryQ = array(
"q1" => array(
"question"=>"This is the First question",
"options" => array(
"this is option 1",
"this is option 2",
"this is option 3"
),
"answer" => 2
),
"q2" => array(
"question"=>"This is the Question String for question 2",
"options" => array(
"this is option A",
"this is option B",
"this is option C"
),
"answer" => 1
),
"q3" =>array(
"question"=>"This is the Question String for question 3",
"options" => array(
"this is option X",
"this is option Y",
"this is option Z"
),
"answer" => 0
)
);
echo "
<form name='quiz' method='post'>";
foreach( $quizHistoryQ as $index => $arr ){
$question=$arr['question'];
$options=$arr['options'];
$answer=$arr['answer'];
echo '<h3>Question: '.$index.': '.$question.'</h3>';
echo '<ul>';
foreach( $options as $i => $option ) echo "<li><input type='radio' name='{$index}[]' value='{$i}'/>{$option}";
echo '</ul>';
}
echo "
<input type='submit' name='sub' value='Submit'/>
</form>";
if( $_SERVER['REQUEST_METHOD']=='POST' ){
$points=0;
foreach( $_POST as $i => $answer ) {
if( $i!=='sub' ){
$success='Incorrect';
$option=$answer[0];
$answer=$quizHistoryQ[ $i ]['options'][ $option ];
$question=$quizHistoryQ[ $i ]['question'];
$correct=$quizHistoryQ[ $i ]['answer'];
if( intval( $correct )===intval( $option ) ){
$points++;
$success='Correct';
}
echo '<section>Question[ '.$i.' ]: '.$question.' - Your Answer: '.$answer.' [ '.$success.' ]</section>';
}
}
echo '<output>You scored:'. $points.'/'.count($quizHistoryQ).'</output>';
}
?>
</body>
</html>