簡體   English   中英

自動遞增$ this-> input-> post()變量名和輸入名

[英]auto increment $this->input->post() variable name and input name

我正在創建一個包含100個問題的Test項目。 我正在使用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);
    }

我希望這會對其他開發人員有所幫助。

編碼愉快!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM