php - 使用复选框的发票生成器 - 未定义的偏移量 [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);
解决方案
替换for
为foreach
:
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);
}
推荐阅读
- c++ - 密文不是块大小的倍数
- sql - 如何构造多选查询错误:关系“rxg”不存在
- android - 无法解决:com.android.support:appcompat-v7:28 并且拖动时在设计中看不到按钮
- git - Xcode 10 Git 如何更新 .gitignore 以忽略与 cocoapods 相关的新项目文件?
- c++ - 单元测试:静态创建方法
- java - 将数据从 RecyclerView 发送到 Fragment
- javascript - 如何在 JavaScript 中读取选定元素的值
- c# - 从实体框架 SqlQuery 访问未映射的实体
- c# - WebClient 下载后正在使用的文件
- c# - .net core 2.x 的 UI