自动递增 $this->input->post() 变量名和输入名



我正在创建一个由100个问题组成的测试项目。我正在使用CODEIGNITER HMVC

这是我的视图/显示中的脚本,它生成并自动递增要在控制器中发送的问题选择和问题id的名称。

<script>
function submit_answer(){
    $.post('<?php echo site_url('iqtest_stud_ans/submit_answer'); ?>',
         {
            <?php
            $i = 100;
            for($c=1;$c<=$i;$c++){
            ?>
            qchoice<?php echo $c; ?>    : $('input[name=qchoice<?php echo $c; ?>]:checked').val(),
            q_id<?php echo $c; ?>       : $('#q_id<?php echo $c; ?>').val(),
            <?php } ?>
            take_no                     : $('#take_no').val(),
         }
    );
}

在我的剧本中,我没有任何问题,一切都很正常。我能够发送控制器中的所有数据。我的问题来了。因为我不想让我的生活变得悲惨,所以我想像在剧本中一样使用for循环。

这是我控制器中的代码。

$i=100;
        $q_id101 = $this->input->post('q_id1');
        for($c=1;$c<=$i;$c++){
            ${'q_id' . $c} = $this->input->post('{"qchoice" . $c}');
            ${'choice' . $c} = $this->input->post("{'qchoice' . $c}");
        }//below codes are working.
        $e=100;
        for($d=1;$d<=$e;$d++){
            $data = array(
                'q_id'      => ${'q_id' . $d},
                'answer'    => ${'choice' . $d},
                'stud_no'   => $stud_no,
                'take_no'   => $take_no
                );
            print_r($data);
            $update = $this->mdl_stud_ans->_insert($data);
        }

变量运行良好${'q_id'.$c},但post内部的增量不起作用$this->input->post('{"qchoice".$c}');

我的问题是…有没有办法增加post()中输入/字段的名称?

我认为它应该像这个

${'q_id' . $c} = $this->input->post('qchoice' . $c);
${'choice' . $c} = $this->input->post('qchoice' . $c);

感谢Shaiful的帮助。不管怎样,我就是这样解决的。

$i=100;
    for($c=1;$c<=$i;$c++){
        $varq_id = 'q_id'.$c; //I add this
        $varqchoice = 'qchoice'.$c; //I add this
        ${'q_id' . $c} = $this->input->post($varq_id); //and place the variable inside the post().
        ${'choice' . $c} = $this->input->post($varqchoice);
    }

我希望这将帮助其他开发人员。

快乐编码!

最新更新