Mardino Ismail
Mardino Ismail

Reputation: 61

Passing selected checkbox from array data into database with Codeigniter

i'm newbie for codeigniter. i trying to passing data attendance into database.

this my view code

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

when i checked for 1 employee attendance (example 4630 is work), data can pass to database, but result like this (see image 2)

image 2

all data view input to database, not data when 1 checked before and remark WORK insert into row 1.

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

and this my model

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

i just want insert employee attendance who i checked. please help

thanks for anyone help.

sorry my bad english

Upvotes: 0

Views: 1204

Answers (1)

Hasta Dhana
Hasta Dhana

Reputation: 4719

Add an identifier on your employee attendance input name, so each employee have their own unique attendance dataset.

view :

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

Since the attendance input is a multidimensional array, for the empty validation, you could use array_filter to check the whole attendance array.

And because of you are inserting an array data type into a single column, you need to concatenate it, you could use implode() function.

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

Upvotes: 1

Related Questions