Php 在Codeigniter中存储值而不是数组
在数组中存储单选按钮值(临时存储)。创建了10个问题集,这10个问题集各有4个选项。现在我将它存储在数组中,以检查有多少单选按钮被选中,以及有多少是正确的。 现在这个问题有10个问题。随着数据库的增加,将加载更多的问题 那么,如何使用另一种方法或任何其他技术来存储和检查它们呢 Codeigniter的新功能 下面是我存储的代码(在控制器中),并将其重定向到模型,然后是结果视图页面Php 在Codeigniter中存储值而不是数组,php,arrays,codeigniter,radio-button,storage,Php,Arrays,Codeigniter,Radio Button,Storage,在数组中存储单选按钮值(临时存储)。创建了10个问题集,这10个问题集各有4个选项。现在我将它存储在数组中,以检查有多少单选按钮被选中,以及有多少是正确的。 现在这个问题有10个问题。随着数据库的增加,将加载更多的问题 那么,如何使用另一种方法或任何其他技术来存储和检查它们呢 Codeigniter的新功能 下面是我存储的代码(在控制器中),并将其重定向到模型,然后是结果视图页面 public function resultdisplay() { $this->data['chec
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);
}
任何其他方法,因为不可能每次编写代码为每个新问题及其单选按钮创建数组
谢谢我会使用循环。您的单选按钮如下所示:
<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>
...
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>';
}
}
对于DynamicOne,我以10为例,您可以动态增加
<form action="/index.php/welcome/getResponse" method="POST">
<?php for($i = 0; $i < 10; $i++){ ?>
<div>
<label><?php echo ($i+1)."."; ?> </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); ?>">
<?php } ?>
</div>
<br/>
<?php } ?>
<input type="submit" name="Submit">
</form>
<form action="/index.php/welcome/getResponse" method="POST">
<?php for($i = 0; $i < 10; $i++){ ?>
<div>
<label><?php echo ($i+1)."."; ?> </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); ?>">
<?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 )