簡體   English   中英

使用Codeigniter將選定的復選框從數組數據傳遞到數據庫

[英]Passing selected checkbox from array data into database with Codeigniter

我是Codeigniter的新手。 我試圖將數據考勤傳遞到數據庫中。

這是我的查看代碼

<?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++;} ?>

我的觀點

當我檢查1個員工出勤(實施例4630是工作),數據可以傳遞到數據庫中,但導致這樣的(見圖像2)

圖片2

所有數據輸入查看到數據庫,而不是數據時1檢查前,備注工作插入第1行。

這是我的控制器

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>
    ...

由於出勤輸入是多維數組,因此對於empty驗證,您可以使用array_filter檢查整個出勤陣列。

由於將數組數據類型插入到單列中,因此需要對其進行串聯,因此可以使用implode()函數。

控制器:

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));
}

暫無
暫無

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

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