PHP测验使用数组生成问题.如何点数

  • 本文关键字:成问题 何点数 数组 PHP php
  • 更新时间 :
  • 英文 :


我如何在这个测验中计算分数?数组显示正确答案,但提交表单后如何访问它?

<?php $Questions = array(
1 => array(
'Question' => 'What is 2+2:',
'Answers' => array(
'A' => '4',
'B' => '5',
'C' => 'Nothing'
),
'CorrectAnswer' => 'A'
),
2 => array(
'Question' => 'Where is Paris?',
'Answers' => array(
'A' => 'Somewhere',
'B' => 'in Rome',
'C' => 'in France'
),
'CorrectAnswer' => 'C'
),
3 => array(
'Question' => 'What is water?',
'Answers' => array(
'A)' => 'hydrogen and nitrogen',
'B)' => 'hydrogen and oxygen',
'C)' => 'hydrocarbon and iron'
),
'CorrectAnswer' => 'B'
)); ?>

这是打印表单的代码:有目的地,我也想"回声;绿色的正确答案和红色的错误答案在下一页。

<form action="" method="post" name="quizz" id="quiz">
<ol>
<?php foreach ($Questions as $QuestionNo => $Value){ ?>
<li>
<h4><?php echo $Value['Question']; ?></h4>
<?php 
foreach ($Value['Answers'] as $Letter => $Answer){ 
$Label = 'question-'.$QuestionNo.'-answers-'.$Letter;
?>
<div>
<input type="radio" name="answers[<?php echo $QuestionNo; ?>]" id="<?php echo $Label; ?>" value="<?php echo $Letter; ?>" />
<label for="<?php echo $Label; ?>"><!--<?php echo $Letter; ?>)--> <?php echo $Answer; ?> </label>
</div>
<?php } ?>
</li>
<?php } ?>
</ol>
<input type="submit" value="Submit Quiz" />
</form>

所以缺少的是用户点击提交按钮后我应该使用的代码。我知道如何在没有数组的情况下进行测验,但这对我来说很有挑战性。我是一名教师,只想为我的学生改进我的课程。感谢您的帮助:(

张贴到同一页面并在问题旁边标记:

<?php 
$questions = array(
1=>array(
'question' => 'What is 2+2?',
'answers' => array(
'a' => '4',
'b' => '5',
'c' => 'nothing'
),
'correct' => 'a'
),
2=>array(
'question' => 'Where is paris?',
'answers' => array(
'a' => 'somewhere',
'b' => 'in rome',
'c' => 'in france'
),
'correct' => 'c'
),
3=>array(
'question' => 'What is water?',
'answers' => array(
'a' => 'hydrogen and nitrogen',
'b' => 'hydrogen and oxygen',
'c' => 'hydrocarbon and iron'
),
'correct' => 'b'
)
);
?>
<?php
if( $_SERVER['REQUEST_METHOD']=='POST' ){


$score=0;
$answers=array();

foreach( $_POST['answers'] as $index => $answer ){
if( array_key_exists( (int)$index, $questions ) ){
if( $questions[ (int)$index ]['correct']==$answer ) {
$score++;
}
$answers[(int)$index]=array(
'question'  =>  $questions[ (int)$index ]['question'],
'answer'    =>  $answer,
'correct'   =>  $questions[ (int)$index ]['correct']==$answer
);
}

}
}
?>
<!DOCTYPE html>
<html lang='en'>
<head>
<meta charset='utf-8' />
<title></title>
</head>
<body>
<form method="post" name="quizz" id="quiz">
<?php
if( $_SERVER['REQUEST_METHOD']=='POST' ){
printf('You scored %s/%s',$score,count($questions));
}
?>
<ol>
<?php foreach( $questions as $questionno => $value ){ ?>
<li>
<h4><?php 
echo $value['question'];
if( $_SERVER['REQUEST_METHOD']=='POST' ){
echo $answers[$questionno]['correct'] ? '<span style="color:green">Correct</span>' : '<span style="color:red">Incorrect</span>';

}
?></h4>
<?php 
foreach( $value['answers'] as $letter => $answer ){ 
$label = 'question-'.$questionno.'-answers-'.$letter;
?>
<div>
<input type="radio" name="answers[ <?php echo $questionno; ?> ]" id="<?php echo $label; ?>" value="<?php echo $letter; ?>" />
<label for="<?php echo $label; ?>"><!--<?php echo $letter; ?>)--> <?php echo $answer; ?> </label>
</div>
<?php } ?>
</li>
<?php } ?>
</ol>
<input type="submit" value="Submit Quiz" />
</form>
</body>
</html>

要将测试发布到a-n-other页面,您需要知道其他页面上的问题和答案。为此,实现这一点的最简单方法可能是在测验和结果页面中都使用一个包含的文件。

包含问题的文件(questions.php(

<?php
#questions.php
$questions = array(
1=>array(
'question' => 'What is 2+2?',
'answers' => array(
'a' => '4',
'b' => '5',
'c' => 'nothing'
),
'correct' => 'a'
),
2=>array(
'question' => 'Where is paris?',
'answers' => array(
'a' => 'somewhere',
'b' => 'in rome',
'c' => 'in france'
),
'correct' => 'c'
),
3=>array(
'question' => 'What is water?',
'answers' => array(
'a' => 'hydrogen and nitrogen',
'b' => 'hydrogen and oxygen',
'c' => 'hydrocarbon and iron'
),
'correct' => 'b'
)
);
?>

将表格指向结果页面而不是其本身的测验页面:

<?php
require __DIR__ . '/questions.php';
?>
<!DOCTYPE html>
<html lang='en'>
<head>
<meta charset='utf-8' />
<title></title>
</head>
<body>
<form action='quizresults.php' method="post">
<ol>
<?php foreach( $questions as $questionno => $value ){ ?>
<li>
<h4><?php echo $value['question']; ?></h4>
<?php 
foreach( $value['answers'] as $letter => $answer ){ 
$label = 'question-'.$questionno.'-answers-'.$letter;
?>
<div>
<input type="radio" name="answers[ <?php echo $questionno; ?> ]" id="<?php echo $label; ?>" value="<?php echo $letter; ?>" />
<label for="<?php echo $label; ?>"><!--<?php echo $letter; ?>)--> <?php echo $answer; ?> </label>
</div>
<?php } ?>
</li>
<?php } ?>
</ol>
<input type="submit" value="Submit Quiz" />
</form>
</body>
</html>

以及表单所针对的页面,quizresults.php

<?php
#quiz results
require __DIR__ . '/questions.php';
?>
<?php
if( $_SERVER['REQUEST_METHOD']=='POST' ){

$score=0;

foreach( $_POST['answers'] as $index => $answer ){
if( array_key_exists( (int)$index, $questions ) ){

if( $questions[ (int)$index ]['correct']==$answer ) {
$score++;
}
$answers[(int)$index]=(object)array(
'question'  =>  $questions[ (int)$index ]['question'],
'answer'    =>  $answer,
'correct'   =>  $questions[ (int)$index ]['correct']==$answer
);
}
}

foreach($answers as $index => $arr){
printf(
'<div>[ Question %d ] %s Answer: %s %s</div>', 
$index, 
$arr->question, 
$arr->answer, $arr->correct ? '<span style="color:green">Correct</span>' : '<span style="color:red">Incorrect</span>'
);
}
}
?>

更新:根据评论,实际答案应为绿色/红色

<?php
#quiz results
require __DIR__ . '/questions.php';
?>
<?php
if( $_SERVER['REQUEST_METHOD']=='POST' ){

$score=0;

foreach( $_POST['answers'] as $index => $answer ){
if( array_key_exists( (int)$index, $questions ) ){

if( $questions[ (int)$index ]['correct']==$answer ) {
$score++;
}
$answers[(int)$index]=(object)array(
'question'  =>  $questions[ (int)$index ]['question'],
'answer'    =>  $answer,
'correct'   =>  $questions[ (int)$index ]['correct']==$answer
);
}
}



echo '<ol>';

$percentage=round( ( $score / count( $questions ) ) * 100, 1 );

$message='Appalling!';
if( $percentage > 25 )$message='Bad luck!';
if( $percentage > 50 )$message='Well done!';
if( $percentage > 75 )$message='Congratulations!';
if( $percentage == 100 )$message='Outstanding!';


printf('<h2>%s You scored: %d/%d ( %s%% )</h2>', $message, $score, count( $questions ), $percentage );

foreach( $questions as $questionno => $value ){

$options=array();

foreach( $value['answers'] as $letter => $answer ){
$label = 'question-'.$questionno.'-answers-'.$letter;
$checked='';

if( isset( $answers[ $questionno ] ) && trim( $answers[ $questionno ]->answer )==trim( $letter ) ){
switch( $answers[ $questionno ]->correct ){
case true:
$colour='green';
$adjective=' - Correct';                        
break;
case false:
$colour='red';
$adjective=' - Incorrect';                      
break;
}
$answer=sprintf('<span style="color:%s">%s%s</span>', $colour, $answer,$adjective );
$checked=' checked';
}



$options[]=sprintf(
'<div>
<input type="radio" name="answers[%1$d]" id="%2$s" value="%3$s" %5$s/>
<label for="%2$s">%4$s</label>
</div>',
$questionno,
$label,
$letter,
$answer,
$checked
);
}


printf(
'<li>
<h4>%s</h4>
%s
</li>',
$value['question'],
implode(PHP_EOL,$options)
);
}
echo '</ol><a href="javascript:history.go(-1)">OK - Go back</a>';
}
?>

最新更新