Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/293.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/codeigniter/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/jsp/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 自动递增$this->input->post()变量名和输入名_Php_Codeigniter_Increment - Fatal编程技术网

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

Php 自动递增$this->input->post()变量名和输入名,php,codeigniter,increment,Php,Codeigniter,Increment,我正在创建一个由100个问题组成的测试项目。我正在使用CODEIGNITER HMVC 下面是我的视图/显示中的脚本,它生成并自动递增要发送到控制器中的问题选择名称和问题id <script> function submit_answer(){ $.post('<?php echo site_url('iqtest_stud_ans/submit_answer'); ?>', { <?php

我正在创建一个由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(),
         }
    );
}
$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);
        }
在我的脚本中,我没有任何问题,一切正常。我能够发送控制器中的所有数据。我的问题来了。因为我不想让我的生活变得悲惨,所以我想像我在脚本中那样使用for循环

这是我控制器中的代码

<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(),
         }
    );
}
$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}'


我的问题是。。。有没有办法在帖子中增加输入/字段的名称?

我想应该是这样的

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

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

$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);
    }
我希望这将帮助其他开发者


快乐编码

如果你打印$this->input->post,你会得到什么;回显/打印此$this->input->post'qchoice'$C无/空白。但是如果$this->input->post'qchoice1';我能够得到价值。确保$c有价值;这样检查$c=1;echo$this->input->post'qchoice'$C基本上$this->input->post'qchoice1';和$this->input->post'qchoice'$C如果$c==1,则相同