name; ?> position; ?> -6ren">
gpt4 book ai didi

php - 使用 Codeigniter 将选定的复选框从数组数据传递到数据库

转载 作者:行者123 更新时间:2023-11-29 10:26:33 25 4
gpt4 key购买 nike

我是 codeigniter 的新手。我试图将数据出勤率传递到数据库中。

这是我的 View 代码

<?php $no=1; foreach($employee AS $list_emp) { ?>
<tr>
<td><?= $no ?></td>
<td><input type="hidden" name="employee[]" value="<?php echo $list_emp->sn; ?>"><?= $list_emp->name; ?></td>
<td><?= $list_emp->position; ?></td>
<?php foreach ($attend_detail AS $list) {?>
<td><input type="checkbox" name="detail[]" value="<?php echo $list['details']"></td>
<?php } ?>
<td><input type="text" name="note[]"></td>
<input type="hidden" name="location[]" value="<?php echo $list_emp->branch; ?>">
</tr>
<?php $no++;} ?>

my view

当我检查 1 名员工的出勤情况(例如 4630 是工作)时,数据可以传递到数据库,但结果如下(参见图 2)

image 2

所有数据 View 输入到数据库,而不是1之前检查时的数据,并备注WORK insert into row 1。

这是我的 Controller

function add_attend()
{
$employee = $this->input->post('employee');
$location = $this->input->post('location');
$detail = $this->input->post('detail');
$note = $this->input->post('note');
$total = count($employee);
if (empty($detail) === true) { $errors['detail'] = 'please select one';}

if (!empty($errors)){
$info['success'] = false;
$info['errors'] = $errors;
}

else {
for ($x=0; $x<$total; $x++){
$data = array(
'sn' => $employee[$x],
'lab' => $location[$x],
'stat' => $detail[$x],
'note' => $note[$x]
);
$this->m_human_capital->insert_attend($data);
}

$info['success'] = true;
}
$this->output->set_content_type('application/json')->set_output(json_encode($info));
}

这是我的模型

function insert_attend($data)
{
$this->db->insert('tb_attend_tes',$data);
}

我只想插入我检查过的员工出勤情况。请帮忙

感谢任何人的帮助。

对不起,我的英语不好

最佳答案

在员工出勤输入名称上添加标识符,以便每个员工都有自己独特的出勤数据集。

查看:

    ...
<td><input type="checkbox" name="detail[<?php echo $no-1 ?>][]" value="<?php echo $list['details']"></td>
...

由于考勤输入是一个多维数组,对于验证,您可以使用array_filter检查整个考勤数组。

由于您要将数组数据类型插入到单个列中,因此需要将其连接起来,可以使用 implode() 函数。

Controller :

function add_attend()
{
$employee = $this->input->post('employee');
$location = $this->input->post('location');
$detail = $this->input->post('detail');
$note = $this->input->post('note');
$total = count($employee);
$filtered_detail = array_filter($detail);
if (empty($filtered_detail) === true) {
$errors['detail'] = 'please select one';
}

if (!empty($errors)){
$info['success'] = false;
$info['errors'] = $errors;
}

else {
for ($x=0; $x<$total; $x++){
$data = array(
'sn' => $employee[$x],
'lab' => $location[$x],
'stat' => (isset($detail[$x]) && !empty($detail[$x])) ? implode(",",$detail[$x]) : '',
'note' => $note[$x]
);
$this->m_human_capital->insert_attend($data);
}

$info['success'] = true;
}
$this->output->set_content_type('application/json')->set_output(json_encode($info));
}

关于php - 使用 Codeigniter 将选定的复选框从数组数据传递到数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48186208/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com