0

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

5
  • 1
    where is your note field ? because if its in the same line as your attendance list - your db structure doesnt make any sense ... you need a m:n relation for your attendances but only a 1:1 relation for your note - or should note be duplicated for each attendance ? Commented Jan 10, 2018 at 11:33
  • sorry for missing part picture, NOTE field at the right of the picture, and it's no problem if empty. i have tried with fill the NOTE and the problem still same. Commented Jan 10, 2018 at 17:43
  • no one can help me? Commented Jan 11, 2018 at 4:00
  • @MardinoIsmail can you print the array before inserting into the database add into the answer Commented Jan 11, 2018 at 7:32
  • @saurabhkamble see the image no. 1. like that? Commented Jan 11, 2018 at 11:43

1 Answer 1

1

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));
}
Sign up to request clarification or add additional context in comments.

3 Comments

like this <input type="hidden" name="employee[<?php echo $no-1 ?>][]" value="<?php echo $list_emp->sn; ?>"> ? i tried your code but i have error message.
NO.. it's just for detail input only
oh. ok. forgot my first comment. i have tried like your code. but now the data not insert into database, i got error message undefined offset in line 'stat'. i will show you the error detail if im back to office.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.