0

I am trying to insert multiple checkbox with different value into corresponding database column.

For example:

View:

I have 4 checkbox:

<input type="checkbox" name="approverAccess[]" value="LA" >
<input type="checkbox" name="approverAccess[]" value="OA" >
<input type="checkbox" name="approverAccess[]" value="PC" >
<input type="checkbox" name="approverAccess[]" value="TS" >

And the table is something like this:

enter image description here

So the scenario is, If the first checkbox is checked, C1 column will have a value of '1' and the rest is '0', if second checkbox is checked C2 column will have a value of '1' and the rest is '0' and so on. Whatever checkbox is check it should be show or add in the corresponding table column.

Controller:

foreach($approverAccess as $selected) {     
    // ***What I'm going to do here****  
}

$data = array(
'ID' => '',
'USERID' => $adminID,
'C1' => $selected,
'C2' => $selected,
'C3' => $selected,
'C4' => $selected

);

$this->dbquery->modInsertval('tblapprover',$data);

How can I proceed with this?

2
  • Why not just name them approverAccessC1, approverAccessC2, approverAccessC3, and approverAccessC4? In this case, I'm not sure using an array really provides any benefit. Commented May 16, 2019 at 17:39
  • The table is something like this: what is a table in this context Commented May 16, 2019 at 21:47

1 Answer 1

2
$data = array(
    'ID' => '',
    'USERID' => $adminID,
    'C1' => in_array('LA', $approverAccess) ? 1 : 0, 
    'C2' => in_array('OA', $approverAccess) ? 1 : 0,
    'C3' => in_array('PC', $approverAccess) ? 1 : 0,
    'C4' => in_array('TS', $approverAccess) ? 1 : 0
);

$this->dbquery->modInsertval('tblapprover',$data);
Sign up to request clarification or add additional context in comments.

Comments

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.