我正在使用codeigniter开发考试系统。我的数据库有一个名为questions的表,其中有7个cols问题,option1,2,3,4,正确的答案和标签。我想获取并匹配结果。
我的视图编码是
<form action="<?php echo base_url() ?>main/economics_validation" name ="f" method="post" accept-charset="utf-8">
<?php
foreach($eco as $economics)
{
?>
<li>
<h3><p>Q<?php echo $economics->id ?> <?php echo $economics->question; ?></p></h3>
<p><?php echo $economics->option1 ?>
<input type="radio" name="<?php echo $economics->id ?>" value="<?php echo $economics->option1 ?>"></p>
<p><?php echo $economics->option2 ?>
<input type="radio" name="<?php echo $economics->id ?>" value="<?php echo $economics->option2 ?>"></p>
<p><?php echo $economics->option3 ?>
<input type="radio" name="<?php echo $economics->id ?>" value="<?php echo $economics->option3 ?>"></p>
<p><?php echo $economics->option4 ?>
<input type="radio" name="<?php echo $economics->id ?>" value="<?php echo $economics->option4 ?>"></p>
</li>
<?php
}
?>
</ul>
</div>
&#13;
我的控制器编码是
public function economics_validation()
{
$result['d']=$this->model_db->calculate_marks();
$this->load->view('marks',$result);
}
我的模型编码是
public function calculate_marks()
{
$query = $this->db->query("SELECT * FROM questions");
$a = $query->result();
$q2 = $this->db->get('questions');
$marks = 0;
foreach($a as $corr)
{
for($i = 1; $i <= $q2->num_rows();$i++)
{
if($corr->correct_answer == $this->input->post($i))
{
$marks = $marks + 1;
}
else
{
}
}
}
return $marks;
}
我无法得到正确的结果。问题在于它说错误答案是正确的。
答案 0 :(得分:1)
模型中的功能需要一些调整。试试这段代码。
public function calculate_marks()
{
$query = $this->db->query("SELECT * FROM questions");
$a = $query->result();
$marks = 0;
foreach($a as $corr)
{
if($corr->correct_answer == $this->input->post($corr->id))
{
$marks = $marks + 1;
}
}
return $marks;
}
就是这样。