2017-06-20 37 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)