使用 PHP 和 MySQL 设计一个应用程序,帮助教师进行在线选择题测试。每门考试有 100 道题,每道题有 4 种以上的选择。
当超过 1000 名用户参加考试时,当系统管理员进入“统计”查看平均分数或其他统计信息时,经过一番思考,系统没有显示任何内容:没有结果,没有统计信息。
计算:100个问题*4个选项*1000个用户得到平均分。
当我们在更强大的 PC 上运行 Web 服务器时,它会显示统计信息,但在共享服务器中它不会显示任何内容。
表架构是 InnoDB。
如何计算平均分数和/或将结果导出到 CSV 文件,或将它们添加到数组并排序?
这是在导出到 csv 文件之前计算最终分数的代码:
$result_list = array();
foreach($UserTestList as $userTestProp)
{
$result_list[$userTestProp['utid']]=array();
....
$result_list[$userTestProp['utid']]['utid'] = $userTestProp['utid'];
$result_list[$userTestProp['utid']]['utlength'] = gmdate("H:i:s", strtotime($userTestProp['utlast_activity'])-strtotime($userTestProp['utstart_datetime']));
$result_list[$userTestProp['utid']]['utuser_ip'] = $userTestProp['utuser_ip'];
$result_list[$userTestProp['utid']]['utbrowser'] = $userTestProp['utbrowser'];
...
$result_list[$userTestProp['utid']]['correct'] = $test->GetNumOfAnswers('correct',$userTestProp['utid']);
$result_list[$userTestProp['utid']]['incorrect'] = $test->GetNumOfAnswers('incorrect',$userTestProp['utid']);
$result_list[$userTestProp['utid']]['none'] = $test->GetNumOfAnswers('none',$userTestProp['utid']);
$result_list[$userTestProp['utid']]['final_score'] = ($testInfo['tminus_mark']==1?round(($result_list[$userTestProp['utid']]['correct']-($result_list[$userTestProp['utid']]['incorrect']/3)),2):$result_list[$userTestProp['utid']]['correct']);
}
这是 GetNumOfAnswers 方法:
function GetNumOfAnswers($type,$utid)
{
global $table_prefix;
$total_correct_answers=$total_incorrect_answers=$total_none_answered=0;
$QuestionList = $this->GetLessonQuestionListByUTID($utid,NULL);
foreach ($QuestionList as $QuestionInfo)
{
switch ($this->IsCorrect($utid,$QuestionInfo['qid']))
{
case '1':
$total_correct_answers++;
break;
case '0':
$total_incorrect_answers++;
break;
default:
$total_none_answered++;
break;
}
}
switch ($type) {
case 'correct':
return $total_correct_answers;
break;
case 'incorrect':
return $total_incorrect_answers;
break;
case 'none':
return $total_none_answered;
break;
case 'all':
return count($QuestionList);
break;
}
}
这是 IsCorrect 方法:
function IsCorrect($utid,$qid)
{
global $table_prefix;
$query = $this->link->prepare("SELECT `qcid` FROM `".$table_prefix."user_test_question` INNER JOIN `".$table_prefix."user_test_question_choice`
ON `".$table_prefix."user_test_question`.`utqid`=`".$table_prefix."user_test_question_choice`.`utqid` WHERE `utid`=? AND `qid`=?");
$values = array($utid,$qid);
$query->execute($values);
$qcid_chosen = $query->fetchAll();
if(empty($qcid_chosen))
return 2;
$query = $this->link->prepare("SELECT `qcid` FROM `".$table_prefix."questions` INNER JOIN `".$table_prefix."questions_choices`
ON `".$table_prefix."questions`.`qid`=`".$table_prefix."questions_choices`.`qid` WHERE `".$table_prefix."questions`.`qid`=? AND `qcis_answer`=1");
$values = array($qid);
$query->execute($values);
$qcid_correct = $query->fetchAll();
if ($qcid_chosen==$qcid_correct)
return 1;
else
return 0;
return $counts;
}