首页 > 解决方案 > 使用复选框的发票生成器 - 未定义的偏移量 [2] PHP Codeigniter

问题描述

我正在尝试在学校管理系统中生成多张发票。这是问题:

例如,我在 DB 中有 10 个学生

如果学生按连续顺序选择,我可以生成发票。学生 1、学生 2、学生 3、学生 4

但是一旦我从连续顺序中错过了一个学生并尝试以随机顺序生成它会给我一个未定义的偏移错误:学生 1、学生 4、学生 5、学生 7

这是代码:

    for($x = 1; $x <= count($this->input->post('studentId')); $x++) {                               
        $insert_data = array(                                   
            'class_id'      => $this->input->post('className'),
            'section_id'    => $this->input->post('sectionName'),
            'student_id'    => $this->input->post('studentId')[$x],         
            'payment_name_id' => $payment_name_id
        );

        $status = $this->db->insert('payment', $insert_data);
    }

    return ($status === true ? true : false);

这同样适用于在生成发票后对其进行编辑。我什至尝试将 for 循环更改为该循环的 while 循环,这不会给我任何错误,但如果选择的学生是随机顺序,它就不会保存:

            $x = 1;
            $form_fields = count($this->input->post('editStudentId')); 

            while($x <= $form_fields) { 

                    if(!empty($this->input->post('editStudentId')[$x])) {
                        $update_payment_data = array(                                   
                            'class_id'      => $this->input->post('editClassName'),
                            'section_id'    => $this->input->post('editSectionName'),
                            'student_id'    => $this->input->post('editStudentId')[$x],         
                            'payment_name_id' => $id
                        );

                        $status = $this->db->insert('payment', $update_payment_data);
                    }

                $x++;
            }

            return ($status === true ? true : false);

图片 - 选择学生: 选择学生

图像-我选择的 4 个中只有 2 个保存后: 发布保存

标签: phpcodeigniter

解决方案


替换forforeach

foreach ($this->input->post('studentId') as $studentId) {
    $insert_data = array(                                   
        'class_id'      => $this->input->post('className'),
        'section_id'    => $this->input->post('sectionName'),
        'student_id'    => $studentId,         
        'payment_name_id' => $payment_name_id
    );

    $status = $this->db->insert('payment', $insert_data);
}

推荐阅读