我想在phpmyadmin上创建一个结果表,其中包含以下字段:用户名,得分,主题和排名。我已经有一个用户和测验表,包含用户名的用户表和包含主题的测验表。提交测验时,它会通过查看用户选择的答案来显示分数,然后在测验表中检查用户是否从可能的4个选项中选择了正确的答案(字段是choice1,choice2,choice3和答案,但它是随机的输出时)。我希望当用户完成测验时,它将存储包括用户名,主题,分数和排名的结果(例如10/10 = A,5/10 = D)。请帮忙!
结果显示视图:
<div id="container">
<h1>Play the Computing Quiz!</h1>
<?php $score =0; ?>
<?php $array1= array(); ?>
<?php $array2= array(); ?>
<?php $array3= array(); ?>
<?php $array4= array(); ?>
<?php $array5= array(); ?>
<?php $array6= array(); ?>
<?php $array7= array(); ?>
<?php $array8= array(); ?>
<?php foreach($checks as $checkans) { ?>
<?php array_push($array1, $checkans); } ?>
<?php foreach($results as $res) { ?>
<?php array_push($array2, $res->answer);
array_push($array3, $res->quizID);
array_push($array4, $res->question);
array_push($array5, $res->choice1);
array_push($array6, $res->choice2);
array_push($array7, $res->choice3);
array_push($array8, $res->answer);
} ?>
<?php
for ($x=0; $x <10; $x++) { ?>
<form method="post" action="<?php echo base_url();?>index.php/welcome/index">
<p><?=$array4[$x]?></p>
<?php if ($array2[$x]!=$array1[$x]) { ?>
<p><span style="background-color: #FF9C9E"><?=$array1[$x]?></span></p>
<p><span style="background-color: #ADFFB4"><?=$array2[$x]?></span></p>
<?php } else { ?>
<p><span style="background-color: #ADFFB4"><?=$array1[$x]?></span></p>
<?php $score = $score + 1; ?>
<?php } } ?>
<br><br>
<p><b>Your Score: </b></p>
<p><b><?=$score?>/10</b></p>
<input type="submit" value="Play Again!">
</form>
</div>
</body>
</html>
控制器:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Questions extends CI_Controller {
function __construct() {
parent::__construct();
$this->load->database();
}
public function quizdisplay()
{
$this->load->model('quizmodel');
$this->data['questions'] = $this->quizmodel->getQuestions();
$this->load->view('play_quiz', $this->data);
}
public function resultdisplay()
{
$this->data['checks'] = array(
'ques1' => $this->input->post('quizid1'),
'ques2' => $this->input->post('quizid2'),
'ques3' => $this->input->post('quizid3'),
'ques4' => $this->input->post('quizid4'),
'ques5' => $this->input->post('quizid5'),
'ques6' => $this->input->post('quizid6'),
'ques7' => $this->input->post('quizid7'),
'ques8' => $this->input->post('quizid8'),
'ques9' => $this->input->post('quizid9'),
'ques10' => $this->input->post('quizid10'),
);
$this->load->model('quizmodel');
$this->data['results'] = $this->quizmodel->getQuestions();
$this->load->view('result_display', $this->data);
}
}
型号:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class quizmodel extends CI_Model {
public function getQuestions()
{
$this->db->select("quizID, question, choice1, choice2, choice3, answer, subject");
$this->db->from("quiz");
$this->db->where("subject",'computing');
$query = $this->db->get();
return $query->result();
$num_data_returned = $query->num_rows;
if ($num_data_returned < 1) {
echo "There is no data in the database";
exit();
}
}
}
答案 0 :(得分:0)
您的方法不正确。我从你的问题中得到的是 当用户进行测验并提交答案时,您将获得一系列答案,例如
第1步:
$quiz=array(
'answer_1' => 4,
'answer_2' => 2,
'answer_3' => 1,
'answer_4' => 3,
.....
.....
'answer_10' =>2,
)
第二步:
我猜您的“测验表”存储了正确答案的数据(您对它的描述不够好)
希望您的测验表存储如下数据,
ans_col - marks_col
answer_1 - 1
answer_2 - 4
answer_3 - 4
answer_4 - 3
answer_5 - 2
answer_6 - 2
answer_7 - 1
answer_8 - 4
answer_9 - 3
answer_10 - 1
运行查询:
SELECT question_number,marks
FROM Quiz
这将返回一个数组,例如$ answer。(检查$ answer数组是否与$ quiz数组具有相同的结构,如果不相同,则使$ answer的结构与$ quiz数组相同。)
现在, 步骤:3
$result = array_diff_assoc($quiz,$answer);
$wrong_ans = count($result);
$marks_scored = 10 - $wrong_answer;
if($marks_scored >= 8){
$grade = 'A';
}
elseif($marks_scored > 5 && $marks_scored < 8)
{
$grade = 'B';
}
elseif($marks_scored > 2 && $marks_scored < 5)
{
$grade = 'c';
}
else
{
$grade = 'd';
}
步骤:4 根据您的要求,
$username = fetch it from user table using user_id;
$subject = fetch it from user table using subject_id;
$score = $marks_scored;
$ranking = $grade;
步骤:5
现在使一个数组表示为$ output,使诸如username,subject,score之类的键按其各自的值排序,然后将此数组'$ output'传递给视图文件。*
注意:检查数组结构。
希望它消除了您的疑问。