2017-06-20 29 views
0

在阵列(临时存储)中存储单选按钮值。创建了10个问题集合,每个集合有4个选项。现在我将它存储在数组中以检查有多少个单选按钮被检查以及有多少是正确的。 现在的问题是目前有10个问题。由于数据库将增加更多的问题将被加载。在Codeigniter中存储值而不是阵列

那么,如何使用其他方法或任何其他技术来存储和检查它们。

新建笨

这里就是我存储(在控制器)和重定向它

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'), 
     'ques11' => $this->input->post('quizid11'),     
);     
    $this->load->model('quizmodel'); 
    $this->data['results'] = $this->quizmodel->getQueanswer(); 
    $this->load->view('result_display', $this->data); 
} 

任何其他方法模型,然后结果视图页面的代码,因为这是不可能写代码创建数组,每一个新的问题,每次和他们的单选按钮

谢谢

回答

0

我会用一个循环。您的单选按钮应该是这样的:

<input type="radio" name="question[1]['a']" value="a"> Question 1, option a<br> 
<input type="radio" name="question[1]['b']" value="b"> Question 2, option b<br> 
... 
<input type="radio" name="question[2]['a']" value="a"> Question 2, option a<br> 
<input type="radio" name="question[2]['b']" value="b"> Question 2, option b<br> 
... 

既然你提到从数据库中提取,我假设你有一个循环,打印出的无线电输入。

您的循环,打印出HTML,会是这个样子:

for($x = 1; $x <= 10; $x++) { // number of questions 
    for($y = "a"; $y <= "e"; $y++) { // number of options for that question 
     echo '<input type="radio" name="question['.$x.'][\''.$y.'\']" value="'.$y.'"> Question '.$x.'<br>'; 
    } 
} 

在PHP中,循环是这样的:

foreach($this->input->post('question') as $index=>$resultArr) { 
// if radio is selected, then $resultArr[0] is the letter option selected and will match the 
// $index value 
} 
0

对于动态的利用这一点,我已经采取以10为例,您可以动态增加。

<form action="/index.php/welcome/getResponse" method="POST"> 
    <?php for($i = 0; $i < 10; $i++){ ?> 
    <div> 
     <label><?php echo ($i+1)."."; ?>&nbsp;&nbsp;</label> 
     <?php for($j = 0; $j < 4; $j++){ ?> 
      <?php echo chr(65+$j); ?><input type="radio" name="<?php echo 'n_'.$i; ?>" value="<?php echo chr(65+$j); ?>">&nbsp;&nbsp; 
     <?php } ?> 
    </div> 
    <br/> 
    <?php } ?> 
    <input type="submit" name="Submit"> 
</form> 

和在控制器

public function getResponse(){ 
    $resp = array(); 
    for($i = 0; $i<10; $i++) 
    { 
     array_push($resp,$this->input->post('n_'.$i)); 
    } 
    print_r($resp); 
} 

输出(样本)

Array ([0] => A [1] => B [2] => A [3] => B [4] => C [5] => A [6] => B [7] => C [8] => D [9] => B)