将静态多个单选按钮阵列更改为动态Codeigniter



如何将多个单选按钮更改为动态?

这是我的单选按钮及其在查看上的值

A. <input type="radio" name="quizId<?= $question->id ?>" value="<?= $ans_array[0] ?>" required /> <?= $ans_array[0] ?> <br>
B. <input type="radio" name="quizId<?= $question->id ?>" value="<?= $ans_array[1] ?>" /> <?= $ans_array[1] ?> <br>
C. <input type="radio" name="quizId<?= $question->id ?>" value="<?= $ans_array[2] ?>" /> <?= $ans_array[2] ?> <br>
D. <input type="radio" name="quizId<?= $question->id ?>" value="<?= $ans_array[3] ?>" /> <?= $ans_array[3] ?> <br>

这是我的控制器如何动态获取它们?而不调用每一项

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')
);
$this->load->model('quiz_model');
$this->data['results'] = $this->quiz_model->getQuestions();
$this->load->view('templates/header');
$this->load->view('activity/result_display', $this->data);
$this->load->view('templates/footer');
}

这会产生错误

将控制器更改为后

$this->data['checks'] = array(
'ques' => $this->input->post('quizId');
);

<?php 
for($x=0; $x < 5; $x++) {
?>
<form method="post" action="<?php echo base_url(); ?>Pages/index" >

<p><?= $array3[$x] ?>.<?= $array4[$x] ?></p>
<?php if($array2[$x] != $array1[$x]) {?>
<p><span class="badge" style="background-color: #FF9C9E"><?=$array1[$x]?></span></p>
<p><span class="badge" style="background-color: #ADFFB4"><?=$array2[$x]?></span></p>
<?php } else { ?>
<p><span class="badge" style="background-color: #ADFFB4"><?=$array1[$x]?></span></p>
<?php $score = $score + 1; ?>
<?php 
}
}
?>
<hr>
<h5>Your score: </h5>
<h3><?= $score ?>/5</h3>
<input type="submit" class="btn btn-info btn-md" value="Back" />
</form>
<?php $i = 1;
foreach($ans_array AS $array_value): 
?>
<?= $i; ?>.&nbsp; <input type="radio" name="quizId<?= $question->id ?>" value="<?= $array_value ?>" required /> <?= $array_value ?> <br>
<?php 
$i++; 
endforeach; 
?>

希望这会有所帮助。

使用数组会更有效率。你可以这样做:

<input type="radio" name="quizId[<?= $question->id ?>]" value="<?= $array_value ?>" required /> <?= $array_value ?> <br />

因此,在post之后,您可以进行整个循环,并获得您的帖子结果,如:

foreach($this->input->post('quizId') as $val){
//work with each value here
} 

就像我看到的,你可以这样更新控制器:

$this->data['checks'] = $this->input->post('quizId');

您已将整个阵列发送到view。现在,在view文件中,您需要指定类似quizId . $key的键值。

最新更新